private VariableDeclarationStatement createDeclaration( IVariableBinding binding, Expression intilizer) { VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration()); VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment(); fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName())); fragment.setInitializer(intilizer); VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment); result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original))); result.setType( ASTNodeFactory.newType( fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter))); return result; }
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) { VariableDeclaration original = ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration()); VariableDeclarationFragment fragment = fAST.newVariableDeclarationFragment(); fragment.setName((SimpleName) ASTNode.copySubtree(fAST, original.getName())); fragment.setInitializer(intilizer); VariableDeclarationStatement result = fAST.newVariableDeclarationStatement(fragment); result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original))); result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter))); return result; }
/** * Returns the type node for the given declaration. * * @param declaration the declaration * @return the type node or <code>null</code> if the given declaration represents a type * inferred parameter in lambda expression */ public static Type getType(VariableDeclaration declaration) { if (declaration instanceof SingleVariableDeclaration) { return ((SingleVariableDeclaration)declaration).getType(); } else if (declaration instanceof VariableDeclarationFragment) { ASTNode parent= ((VariableDeclarationFragment)declaration).getParent(); if (parent instanceof VariableDeclarationExpression) { return ((VariableDeclarationExpression)parent).getType(); } else if (parent instanceof VariableDeclarationStatement) { return ((VariableDeclarationStatement)parent).getType(); } else if (parent instanceof FieldDeclaration) { return ((FieldDeclaration)parent).getType(); } else if (parent instanceof LambdaExpression) { return null; } } Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$ return null; }
public static int getDimensions(VariableDeclaration declaration) { int dim= declaration.getExtraDimensions(); if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) { LambdaExpression lambda= (LambdaExpression) declaration.getParent(); IMethodBinding methodBinding= lambda.resolveMethodBinding(); if (methodBinding != null) { ITypeBinding[] parameterTypes= methodBinding.getParameterTypes(); int index= lambda.parameters().indexOf(declaration); ITypeBinding typeBinding= parameterTypes[index]; return typeBinding.getDimensions(); } } else { Type type= getType(declaration); if (type instanceof ArrayType) { dim+= ((ArrayType) type).getDimensions(); } } return dim; }
private void initializeParameterInfos() { IVariableBinding[] arguments = fAnalyzer.getArguments(); fParameterInfos = new ArrayList<>(arguments.length); ASTNode root = fAnalyzer.getEnclosingBodyDeclaration(); ParameterInfo vararg = null; for (int i = 0; i < arguments.length; i++) { IVariableBinding argument = arguments[i]; if (argument == null) { continue; } VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root); boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false; ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i); if (isVarargs) { vararg = info; } else { fParameterInfos.add(info); } } if (vararg != null) { fParameterInfos.add(vararg); } }
@Override public OOGContext transfer(SourceVariableDeclaration instr, OOGContext value) { // Get the source variable that is being declared SourceVariable declaredVariable = instr.getDeclaredVariable(); // Get the set of qualifiers of the variable that is being declared Set<OType> declVarSet = this.tm.getAnalysisResult(declaredVariable); // Check if the declared variable is the left-hand side of a new expression, call T-New for its ser // And remove the qualfiers that contain 'n.PD' VariableDeclaration node = instr.getNode(); ASTNode parent = node.getParent(); if(parent instanceof VariableDeclarationStatement){ if(node.getInitializer()!=null && node.getInitializer() instanceof ClassInstanceCreation){ TypeConstraints.tNewNoOthersPublicDomain(declVarSet); } } return value; }
public static boolean matchMethodParams(List<ast.VariableDeclaration> paramsToFind,ITypeBinding[] paramsFound ) { if(paramsToFind.size() != paramsFound.length){ return false; }else{ for(int i = 0; i < paramsToFind.size(); i ++){ ast.VariableDeclaration variableDeclaration = paramsToFind.get(i); ITypeBinding iTypeBinding = paramsFound[i]; String fullyQualifiedName = variableDeclaration.varType.getFullyQualifiedName(); String qualifiedName = iTypeBinding.getQualifiedName(); if(fullyQualifiedName.compareTo(qualifiedName)!= 0){ return false; } } } return true; }
public static IJavaElement getIJavaElement(ASTNode node){ IJavaElement javaElement = null; // Find IJavaElement corresponding to the ASTNode if (node instanceof MethodDeclaration) { javaElement = ((MethodDeclaration) node).resolveBinding() .getJavaElement(); } else if (node instanceof VariableDeclaration) { javaElement = ((VariableDeclaration) node).resolveBinding() .getJavaElement(); }else if(node instanceof TypeDeclaration){ javaElement = ((TypeDeclaration)node).resolveBinding() .getJavaElement(); }else if(node instanceof ClassInstanceCreation){ javaElement = ((ClassInstanceCreation)node).resolveConstructorBinding().getJavaElement(); } return javaElement; }
@Override protected void changeParamgumentName(ParameterInfo info) { VariableDeclaration param = getParameter(info.getOldIndex()); if (!info.getOldName().equals(param.getName().getIdentifier())) return; // don't change if original parameter name != name in rippleMethod String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_update_parameter_references; TextEditGroup description = fCuRewrite.createGroupDescription(msg); TempOccurrenceAnalyzer analyzer = new TempOccurrenceAnalyzer(param, false); analyzer.perform(); SimpleName[] paramOccurrences = analyzer .getReferenceAndDeclarationNodes(); // @param tags are updated in changeJavaDocTags() for (int j = 0; j < paramOccurrences.length; j++) { SimpleName occurence = paramOccurrences[j]; getASTRewrite() .set(occurence, SimpleName.IDENTIFIER_PROPERTY, info.getNewName(), description); } }
@Override protected void changeParamgumentType(ParameterInfo info) { VariableDeclaration oldParam = getParameter(info.getOldIndex()); if (oldParam instanceof SingleVariableDeclaration) { getASTRewrite() .set( oldParam, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.valueOf(info.isNewVarargs()), fDescription); SingleVariableDeclaration oldSVDParam = (SingleVariableDeclaration) oldParam; replaceTypeNode( oldSVDParam.getType(), ParameterInfo.stripEllipsis(info.getNewTypeName()), info.getNewTypeBinding()); removeExtraDimensions(oldSVDParam); } }
protected void checkIfDeletedParametersUsed() { for (Iterator<ParameterInfo> iter = getDeletedInfos().iterator(); iter.hasNext(); ) { ParameterInfo info = iter.next(); VariableDeclaration paramDecl = getParameter(info.getOldIndex()); TempOccurrenceAnalyzer analyzer = new TempOccurrenceAnalyzer(paramDecl, false); analyzer.perform(); SimpleName[] paramRefs = analyzer.getReferenceNodes(); if (paramRefs.length > 0) { RefactoringStatusContext context = JavaStatusContext.create(fCuRewrite.getCu(), paramRefs[0]); String typeName = getFullTypeName(); Object[] keys = new String[] { BasicElementLabels.getJavaElementName(paramDecl.getName().getIdentifier()), BasicElementLabels.getJavaElementName(getMethod().getElementName()), BasicElementLabels.getJavaElementName(typeName) }; String msg = Messages.format( RefactoringCoreMessages.ChangeSignatureRefactoring_parameter_used, keys); fResult.addError(msg, context); } } }
@Override protected VariableDeclaration createNewParamgument( ParameterInfo info, List<ParameterInfo> parameterInfos, List<VariableDeclaration> nodes) { List<VariableDeclaration> parameters = fLambdaDecl.parameters(); if (!parameters.isEmpty() && parameters.get(0) instanceof SingleVariableDeclaration) { return createNewSingleVariableDeclaration(info); } VariableDeclarationFragment newP = getASTRewrite().getAST().newVariableDeclarationFragment(); newP.setName(getASTRewrite().getAST().newSimpleName(info.getNewName())); return newP; }
/** * @return <code>null</code> if the selection is invalid or does not cover a temp declaration or * reference. */ public static VariableDeclaration findTempDeclaration( CompilationUnit cu, int selectionOffset, int selectionLength) { TempSelectionAnalyzer analyzer = new TempSelectionAnalyzer(selectionOffset, selectionLength); cu.accept(analyzer); ASTNode[] selected = analyzer.getSelectedNodes(); if (selected == null || selected.length != 1) return null; ASTNode selectedNode = selected[0]; if (selectedNode instanceof VariableDeclaration) return (VariableDeclaration) selectedNode; if (selectedNode instanceof Name) { Name reference = (Name) selectedNode; IBinding binding = reference.resolveBinding(); if (binding == null) return null; ASTNode declaringNode = cu.findDeclaringNode(binding); if (declaringNode instanceof VariableDeclaration) return (VariableDeclaration) declaringNode; else return null; } else if (selectedNode instanceof VariableDeclarationStatement) { VariableDeclarationStatement vds = (VariableDeclarationStatement) selectedNode; if (vds.fragments().size() != 1) return null; return (VariableDeclaration) vds.fragments().get(0); } return null; }
@Override public boolean visit(SimpleName node) { VariableDeclaration decl = getVariableDeclaration(node); if (decl == null) return super.visit(node); IVariableBinding binding = decl.resolveBinding(); if (binding == null) return super.visit(node); boolean keysEqual = fKey.equals(binding.getKey()); boolean rangeInSet = fRanges.contains(new Region(node.getStartPosition(), node.getLength())); if (keysEqual && !rangeInSet) fProblemNodes.add(node); if (!keysEqual && rangeInSet) fProblemNodes.add(node); /* * if (!keyEquals && !rangeInSet) * ok, different local variable. * * if (keyEquals && rangeInSet) * ok, renamed local variable & has been renamed. */ return super.visit(node); }
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 1); // $NON-NLS-1$ RefactoringStatus result = Checks.validateModifiesFiles( ResourceUtil.getFiles(new ICompilationUnit[] {fCu}), getValidationContext()); if (result.hasFatalError()) return result; VariableDeclaration declaration = getVariableDeclaration(); result.merge(checkSelection(declaration)); if (result.hasFatalError()) return result; result.merge(checkInitializer(declaration)); return result; } finally { pm.done(); } }
private void removeTemp(CompilationUnitRewrite cuRewrite) { VariableDeclaration variableDeclaration = getVariableDeclaration(); TextEditGroup groupDesc = cuRewrite.createGroupDescription( RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name); ASTNode parent = variableDeclaration.getParent(); ASTRewrite rewrite = cuRewrite.getASTRewrite(); TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer(); rewrite.setTargetSourceRangeComputer(sourceRangeComputer); if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) { sourceRangeComputer.addTightSourceNode(parent); rewrite.remove(parent, groupDesc); } else { sourceRangeComputer.addTightSourceNode(variableDeclaration); rewrite.remove(variableDeclaration, groupDesc); } }
/** * Creates a new promote temp to field refactoring. * * @param declaration the variable declaration node to convert to a field */ public PromoteTempToFieldRefactoring(VariableDeclaration declaration) { Assert.isTrue(declaration != null); fTempDeclarationNode = declaration; IVariableBinding resolveBinding = declaration.resolveBinding(); Assert.isTrue( resolveBinding != null && !resolveBinding.isParameter() && !resolveBinding.isField()); ASTNode root = declaration.getRoot(); Assert.isTrue(root instanceof CompilationUnit); fCompilationUnitNode = (CompilationUnit) root; IJavaElement input = fCompilationUnitNode.getJavaElement(); Assert.isTrue(input instanceof ICompilationUnit); fCu = (ICompilationUnit) input; fSelectionStart = declaration.getStartPosition(); fSelectionLength = declaration.getLength(); fFieldName = ""; // $NON-NLS-1$ fVisibility = Modifier.PRIVATE; fDeclareStatic = false; fDeclareFinal = false; fInitializeIn = INITIALIZE_IN_METHOD; fLinkedProposalModel = null; }
private void initializeParameterInfos() { IVariableBinding[] arguments = fAnalyzer.getArguments(); fParameterInfos = new ArrayList<ParameterInfo>(arguments.length); ASTNode root = fAnalyzer.getEnclosingBodyDeclaration(); ParameterInfo vararg = null; for (int i = 0; i < arguments.length; i++) { IVariableBinding argument = arguments[i]; if (argument == null) continue; VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root); boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false; ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i); if (isVarargs) { vararg = info; } else { fParameterInfos.add(info); } } if (vararg != null) { fParameterInfos.add(vararg); } }
@Override public boolean consumes(SemanticToken token) { SimpleName node = token.getNode(); StructuralPropertyDescriptor location = node.getLocationInParent(); if (location == VariableDeclarationFragment.NAME_PROPERTY || location == SingleVariableDeclaration.NAME_PROPERTY) { ASTNode parent = node.getParent(); if (parent instanceof VariableDeclaration) { parent = parent.getParent(); return parent == null || !(parent instanceof FieldDeclaration); } } return false; }
private void initializeParameterInfos() { IVariableBinding[] arguments= fAnalyzer.getArguments(); fParameterInfos= new ArrayList<ParameterInfo>(arguments.length); ASTNode root= fAnalyzer.getEnclosingBodyDeclaration(); ParameterInfo vararg= null; for (int i= 0; i < arguments.length; i++) { IVariableBinding argument= arguments[i]; if (argument == null) continue; VariableDeclaration declaration= ASTNodes.findVariableDeclaration(argument, root); boolean isVarargs= declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration)declaration).isVarargs() : false; ParameterInfo info= new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i); if (isVarargs) { vararg= info; } else { fParameterInfos.add(info); } } if (vararg != null) { fParameterInfos.add(vararg); } }
/** * Checks whether the new target name conflicts with an already existing * method parameter. * * @param monitor * the progress monitor to display progress * @param status * the status of the condition checking * @throws JavaModelException * if the method declaration of the method to move could not be * found */ protected void checkConflictingTarget(final IProgressMonitor monitor, final RefactoringStatus status) throws JavaModelException { Assert.isNotNull(monitor); Assert.isNotNull(status); final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(fMethod, fSourceRewrite.getRoot()); VariableDeclaration variable= null; final List<SingleVariableDeclaration> parameters= declaration.parameters(); try { monitor.beginTask("", parameters.size()); //$NON-NLS-1$ monitor.setTaskName(RefactoringCoreMessages.MoveInstanceMethodProcessor_checking); for (final Iterator<SingleVariableDeclaration> iterator= parameters.iterator(); iterator.hasNext();) { variable= iterator.next(); if (fTargetName.equals(variable.getName().getIdentifier())) { status.merge(RefactoringStatus.createErrorStatus(RefactoringCoreMessages.MoveInstanceMethodProcessor_target_name_already_used, JavaStatusContext.create(fMethod))); break; } monitor.worked(1); } } finally { monitor.done(); } }
/** * Returns the reserved identifiers in the method to move. * * @return the reserved identifiers * @throws JavaModelException * if the method declaration could not be found */ protected String[] computeReservedIdentifiers() throws JavaModelException { final List<String> names= new ArrayList<String>(); final MethodDeclaration declaration= ASTNodeSearchUtil.getMethodDeclarationNode(fMethod, fSourceRewrite.getRoot()); if (declaration != null) { final List<SingleVariableDeclaration> parameters= declaration.parameters(); VariableDeclaration variable= null; for (int index= 0; index < parameters.size(); index++) { variable= parameters.get(index); names.add(variable.getName().getIdentifier()); } final Block body= declaration.getBody(); if (body != null) { final IBinding[] bindings= new ScopeAnalyzer(fSourceRewrite.getRoot()).getDeclarationsAfter(body.getStartPosition(), ScopeAnalyzer.VARIABLES); for (int index= 0; index < bindings.length; index++) names.add(bindings[index].getName()); } } final String[] result= new String[names.size()]; names.toArray(result); return result; }
/** * Creates a new promote temp to field refactoring. * @param declaration the variable declaration node to convert to a field */ public PromoteTempToFieldRefactoring(VariableDeclaration declaration) { Assert.isTrue(declaration != null); fTempDeclarationNode= declaration; IVariableBinding resolveBinding= declaration.resolveBinding(); Assert.isTrue(resolveBinding != null && !resolveBinding.isParameter() && !resolveBinding.isField()); ASTNode root= declaration.getRoot(); Assert.isTrue(root instanceof CompilationUnit); fCompilationUnitNode= (CompilationUnit) root; IJavaElement input= fCompilationUnitNode.getJavaElement(); Assert.isTrue(input instanceof ICompilationUnit); fCu= (ICompilationUnit) input; fSelectionStart= declaration.getStartPosition(); fSelectionLength= declaration.getLength(); fFieldName= ""; //$NON-NLS-1$ fVisibility= Modifier.PRIVATE; fDeclareStatic= false; fDeclareFinal= false; fInitializeIn= INITIALIZE_IN_METHOD; fLinkedProposalModel= null; }
@Override protected void changeParamgumentName(ParameterInfo info) { VariableDeclaration param= getParameter(info.getOldIndex()); if (!info.getOldName().equals(param.getName().getIdentifier())) return; //don't change if original parameter name != name in rippleMethod String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_update_parameter_references; TextEditGroup description= fCuRewrite.createGroupDescription(msg); TempOccurrenceAnalyzer analyzer= new TempOccurrenceAnalyzer(param, false); analyzer.perform(); SimpleName[] paramOccurrences= analyzer.getReferenceAndDeclarationNodes(); // @param tags are updated in changeJavaDocTags() for (int j= 0; j < paramOccurrences.length; j++) { SimpleName occurence= paramOccurrences[j]; getASTRewrite().set(occurence, SimpleName.IDENTIFIER_PROPERTY, info.getNewName(), description); } }
@Override public boolean visit(SimpleName node){ if (node.getParent() instanceof VariableDeclaration){ if (((VariableDeclaration)node.getParent()).getName() == node) return true; //don't include declaration } if (fTempBinding != null && fTempBinding == node.resolveBinding()) { if (fIsInJavadoc) fJavadocNodes.add(node); else fReferenceNodes.add(node); } return true; }
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 1); //$NON-NLS-1$ RefactoringStatus result= Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[]{fCu}), getValidationContext()); if (result.hasFatalError()) return result; VariableDeclaration declaration= getVariableDeclaration(); result.merge(checkSelection(declaration)); if (result.hasFatalError()) return result; result.merge(checkInitializer(declaration)); return result; } finally { pm.done(); } }
private VariableDeclarationStatement createDeclaration(IVariableBinding binding, Expression intilizer) { VariableDeclaration original= ASTNodes.findVariableDeclaration(binding, fAnalyzer.getEnclosingBodyDeclaration()); VariableDeclarationFragment fragment= fAST.newVariableDeclarationFragment(); fragment.setName((SimpleName)ASTNode.copySubtree(fAST, original.getName())); fragment.setInitializer(intilizer); VariableDeclarationStatement result= fAST.newVariableDeclarationStatement(fragment); result.modifiers().addAll(ASTNode.copySubtrees(fAST, ASTNodes.getModifiers(original))); result.setType(ASTNodeFactory.newType(fAST, original, fImportRewriter, new ContextSensitiveImportRewriteContext(original, fImportRewriter))); return result; }
/** * Returns the type node for the given declaration. * * @param declaration the declaration * @return the type node or <code>null</code> if the given declaration represents a type * inferred parameter in lambda expression */ public static Type getType(VariableDeclaration declaration) { if (declaration instanceof SingleVariableDeclaration) { return ((SingleVariableDeclaration)declaration).getType(); } else if (declaration instanceof VariableDeclarationFragment) { ASTNode parent= ((VariableDeclarationFragment)declaration).getParent(); if (parent instanceof VariableDeclarationExpression) return ((VariableDeclarationExpression)parent).getType(); else if (parent instanceof VariableDeclarationStatement) return ((VariableDeclarationStatement)parent).getType(); else if (parent instanceof FieldDeclaration) return ((FieldDeclaration)parent).getType(); else if (parent instanceof LambdaExpression) return null; } Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$ return null; }
/** * List of VariableDeclaration of variables which are read in <code>selectedNodes</code>. * * @param maxVariableId Maximum number of variable declarations block * @param selectedNodes The selectedNodes * @return List of VariableDeclaration */ protected List<VariableDeclaration> getVariableDeclarationReadsInside(Statement[] selectedNodes, int maxVariableId) { ArrayList<VariableDeclaration> result= new ArrayList<VariableDeclaration>(); if (!fIsNewContext) return result; IVariableBinding[] reads= getReads(selectedNodes, maxVariableId); for (int i= 0; i < reads.length; i++) { IVariableBinding read= reads[i]; if (!read.isField()) { ASTNode readDecl= getRootNode().findDeclaringNode(read); if (readDecl instanceof VariableDeclaration) { result.add((VariableDeclaration) readDecl); } } } return result; }
private static boolean getInlineLocalProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException { if (!(node instanceof SimpleName)) return false; SimpleName name= (SimpleName) node; IBinding binding= name.resolveBinding(); if (!(binding instanceof IVariableBinding)) return false; IVariableBinding varBinding= (IVariableBinding) binding; if (varBinding.isField() || varBinding.isParameter()) return false; ASTNode decl= context.getASTRoot().findDeclaringNode(varBinding); if (!(decl instanceof VariableDeclarationFragment) || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY) return false; if (proposals == null) { return true; } InlineTempRefactoring refactoring= new InlineTempRefactoring((VariableDeclaration) decl); if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) { String label= CorrectionMessages.QuickAssistProcessor_inline_local_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, context.getCompilationUnit(), refactoring, 5, image); proposal.setCommandId(INLINE_LOCAL_ID); proposals.add(proposal); } return true; }
private static boolean getConvertLocalToFieldProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException { if (!(node instanceof SimpleName)) return false; SimpleName name= (SimpleName) node; IBinding binding= name.resolveBinding(); if (!(binding instanceof IVariableBinding)) return false; IVariableBinding varBinding= (IVariableBinding) binding; if (varBinding.isField() || varBinding.isParameter()) return false; ASTNode decl= context.getASTRoot().findDeclaringNode(varBinding); if (decl == null || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY) return false; if (proposals == null) { return true; } PromoteTempToFieldRefactoring refactoring= new PromoteTempToFieldRefactoring((VariableDeclaration) decl); if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) { String label= CorrectionMessages.QuickAssistProcessor_convert_local_to_field_description; Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); LinkedProposalModel linkedProposalModel= new LinkedProposalModel(); refactoring.setLinkedProposalModel(linkedProposalModel); RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, context.getCompilationUnit(), refactoring, IProposalRelevance.CONVERT_LOCAL_TO_FIELD, image); proposal.setLinkedProposalModel(linkedProposalModel); proposal.setCommandId(CONVERT_LOCAL_TO_FIELD_ID); proposals.add(proposal); } return true; }
/** * Returns the type node for the given declaration. * @param declaration the declaration * @return the type node */ public static Type getType(VariableDeclaration declaration) { if (declaration instanceof SingleVariableDeclaration) { return ((SingleVariableDeclaration)declaration).getType(); } else if (declaration instanceof VariableDeclarationFragment) { ASTNode parent= ((VariableDeclarationFragment)declaration).getParent(); if (parent instanceof VariableDeclarationExpression) return ((VariableDeclarationExpression)parent).getType(); else if (parent instanceof VariableDeclarationStatement) return ((VariableDeclarationStatement)parent).getType(); else if (parent instanceof FieldDeclaration) return ((FieldDeclaration)parent).getType(); } Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$ return null; }
private RefactoringStatus checkSelection(VariableDeclaration decl) { ASTNode parent= decl.getParent(); if (parent instanceof MethodDeclaration) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter); } if (parent instanceof CatchClause) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared); } if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers); } if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources); } if (decl.getInitializer() == null) { String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier())); return RefactoringStatus.createFatalErrorStatus(message); } return checkAssignments(decl); }