@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); }
public void testMethodVarInSwitch() throws IOException { String source = "class Test { " + " enum E { ONE, TWO };" + " void foo(E e) { " + " switch (e) {" + " case ONE: {" + " final Integer i = 1;" + " Runnable r = new Runnable() { " + " public void run() { int j = i + 1; } }; }}}}"; // Verify method var in r1.run() isn't mistakenly made a field in r1. CompilationUnit unit = translateType("Test", source); List<TypeDeclaration> types = unit.types(); TypeDeclaration r1 = types.get(2); assertEquals("Test_$1", NameTable.getFullName(r1)); boolean found = false; for (FieldDeclaration field : r1.getFields()) { List<VariableDeclarationFragment> vars = field.fragments(); for (VariableDeclaration var : vars) { if (var.getName().getIdentifier().equals("val$i")) { found = true; } } } assertTrue("required field not found", found); // Verify constructor takes both outer field and var. ObjectiveCImplementationGenerator.generate("Test.java", Language.OBJECTIVE_C, unit, source); String translation = getTranslatedFile("Test.m"); assertTranslation( translation, "r = [[[Test_$1 alloc] " + "initWithTest:self withJavaLangInteger:i] autorelease]"); }
public static SimpleName[] getProblemNodes( ASTNode methodNode, VariableDeclaration variableNode, TextEdit[] edits, TextChange change) { String key = variableNode.resolveBinding().getKey(); NameNodeVisitor visitor = new NameNodeVisitor(edits, change, key); methodNode.accept(visitor); return visitor.getProblemNodes(); }
/* * @see ASTVisitor#visit(LambdaExpression) */ @Override public boolean visit(LambdaExpression node) { boolean hasParentheses = node.hasParentheses(); if (hasParentheses) this.fBuffer.append('('); for (Iterator<? extends VariableDeclaration> it = node.parameters().iterator(); it.hasNext(); ) { VariableDeclaration v = it.next(); v.accept(this); if (it.hasNext()) { this.fBuffer.append(","); // $NON-NLS-1$ } } if (hasParentheses) this.fBuffer.append(')'); this.fBuffer.append(" -> "); // $NON-NLS-1$ node.getBody().accept(this); return false; }
public TempOccurrenceAnalyzer(VariableDeclaration tempDeclaration, boolean analyzeJavadoc) { Assert.isNotNull(tempDeclaration); fReferenceNodes = new HashSet(); fJavadocNodes = new HashSet(); fAnalyzeJavadoc = analyzeJavadoc; fTempDeclaration = tempDeclaration; fTempBinding = tempDeclaration.resolveBinding(); fIsInJavadoc = false; }
private void initReturnType(ImportRewrite rewriter) { AST ast = fEnclosingBodyDeclaration.getAST(); fReturnType = null; fReturnTypeBinding = null; switch (fReturnKind) { case ACCESS_TO_LOCAL: VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration); fReturnType = ASTNodeFactory.newType( ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter)); if (declaration.resolveBinding() != null) { fReturnTypeBinding = declaration.resolveBinding().getType(); } break; case EXPRESSION: Expression expression = (Expression) getFirstSelectedNode(); if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding(); } else { fExpressionBinding = expression.resolveTypeBinding(); } if (fExpressionBinding != null) { if (fExpressionBinding.isNullType()) { getStatus() .addFatalError( RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression)); } else { ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast); if (normalizedBinding != null) { ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter); fReturnType = rewriter.addImport(normalizedBinding, ast, context); fReturnTypeBinding = normalizedBinding; } } } else { fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ getStatus() .addError( RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression)); } break; case RETURN_STATEMENT_VALUE: LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode()); if (enclosingLambdaExpr != null) { fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null); IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding(); fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null; } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) { fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2(); fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null; } break; default: fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ } if (fReturnType == null) { fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ } }
public SimpleName[] getReferenceAndDeclarationNodes() { SimpleName[] nodes = (SimpleName[]) fReferenceNodes.toArray(new SimpleName[fReferenceNodes.size() + 1]); nodes[fReferenceNodes.size()] = fTempDeclaration.getName(); return nodes; }
public static void getMissingJavadocCommentProposals( IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException { ASTNode node = problem.getCoveringNode(context.getASTRoot()); if (node == null) { return; } BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node); if (declaration == null) { return; } ICompilationUnit cu = context.getCompilationUnit(); ITypeBinding binding = Bindings.getBindingOfParentType(declaration); if (binding == null) { return; } if (declaration instanceof MethodDeclaration) { MethodDeclaration methodDecl = (MethodDeclaration) declaration; IMethodBinding methodBinding = methodDecl.resolveBinding(); IMethodBinding overridden = null; if (methodBinding != null) { overridden = Bindings.findOverriddenMethod(methodBinding, true); } String string = CodeGeneration.getMethodComment( cu, binding.getName(), methodDecl, overridden, String.valueOf('\n')); if (string != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string)); } } else if (declaration instanceof AbstractTypeDeclaration) { String typeQualifiedName = Bindings.getTypeQualifiedName(binding); String[] typeParamNames; if (declaration instanceof TypeDeclaration) { List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters(); typeParamNames = new String[typeParams.size()]; for (int i = 0; i < typeParamNames.length; i++) { typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier(); } } else { typeParamNames = new String[0]; } String string = CodeGeneration.getTypeComment( cu, typeQualifiedName, typeParamNames, String.valueOf('\n')); if (string != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string)); } } else if (declaration instanceof FieldDeclaration) { String comment = "/**\n *\n */\n"; // $NON-NLS-1$ List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments(); if (fragments != null && fragments.size() > 0) { VariableDeclaration decl = fragments.get(0); String fieldName = decl.getName().getIdentifier(); String typeName = binding.getName(); comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n')); } if (comment != null) { String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment)); } } else if (declaration instanceof EnumConstantDeclaration) { EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration; String id = enumDecl.getName().getIdentifier(); String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n')); String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description; proposals.add( new AddJavadocCommentProposal( label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment)); } }
private void visitVD(VariableDeclaration node) { IVariableBinding binding = node.resolveBinding(); if (binding != null) knownVariableDeclarations.put(binding, bridge(node)); }