@Override public boolean visit(SingleMemberAnnotation node) { if (nestLevel != 1) return false; String typeFqn = node.resolveTypeBinding().getQualifiedName(); if ("org.apache.ibatis.annotations.Select".equals(typeFqn) || "org.apache.ibatis.annotations.Update".equals(typeFqn) || "org.apache.ibatis.annotations.Insert".equals(typeFqn) || "org.apache.ibatis.annotations.Delete".equals(typeFqn)) { Expression value = node.getValue(); int valueType = value.getNodeType(); if (valueType == ASTNode.STRING_LITERAL) { mapperMethod.setStatement(((StringLiteral) value).getLiteralValue()); } else if (valueType == ASTNode.ARRAY_INITIALIZER) { StringBuilder buffer = new StringBuilder(); @SuppressWarnings("unchecked") List<Expression> expressions = (List<Expression>) ((ArrayInitializer) value).expressions(); for (Expression expression : expressions) { int expressionType = expression.getNodeType(); if (expressionType == ASTNode.STRING_LITERAL) { if (buffer.length() > 0) buffer.append(' '); buffer.append(((StringLiteral) expression).getLiteralValue()); } else if (expressionType == ASTNode.INFIX_EXPRESSION) { buffer.append(parseInfixExpression((InfixExpression) expression)); } } mapperMethod.setStatement(buffer.toString()); } else if (valueType == ASTNode.INFIX_EXPRESSION) { mapperMethod.setStatement(parseInfixExpression((InfixExpression) value)); } } return false; }
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=388137 public void testbug388137() throws Exception { this.workingCopies = new ICompilationUnit[1]; IJavaProject project = createJavaProject("P1", new String[] {""}, new String[] {"CONVERTER_JCL15_LIB"}, "", "1.5"); try { String contents = "package p;\n" + "import java.util.List;\n" + "public class X {\n" + " public X(List list) {}\n" + " public static class ListHandler implements Handler {\n" + " List list = null;\n" + " public ListHandler(List list) {\n" + " this.list = list;\n" + " }\n" + " }\n" + "}\n" + "interface Handler {}\n"; addLibrary(project, "lib.jar", "src.zip", new String[] {"/P1/p/X.java", contents}, "1.5"); this.workingCopies[0] = getWorkingCopy("/P1/q/Y.java", true); contents = "package q;\n" + "import p.X.ListHandler;\n" + "public class Y {\n" + " public Object foo() {\n" + " ListHandler sortHandler = new ListHandler(null);\n" + " return sortHandler;" + " }\n" + "}\n"; ASTNode node = buildAST(contents, this.workingCopies[0], true); assertTrue("Should be a compilation unit", node instanceof CompilationUnit); CompilationUnit unit = (CompilationUnit) node; node = getASTNode(unit, 0, 0, 0); assertEquals( "Not an expression statement", ASTNode.VARIABLE_DECLARATION_STATEMENT, node.getNodeType()); VariableDeclarationStatement statement = (VariableDeclarationStatement) node; List fragments = statement.fragments(); assertEquals("Wrong size", 1, fragments.size()); VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0); Expression expression = fragment.getInitializer(); assertEquals( "Not a constructor invocation", ASTNode.CLASS_INSTANCE_CREATION, expression.getNodeType()); ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression; IMethodBinding binding = classInstanceCreation.resolveConstructorBinding(); JavaElement element = (JavaElement) binding.getJavaElement(); assertNotNull("Null Element info", element.getElementInfo()); } finally { deleteProject(project); } }
/** * Negates the provided expression and applies the provided copy operation on the returned * expression. * * @param expr the expression to negate * @param copy the copy operation to perform * @return the negated expression, copied according to the copy operation */ public Expression negate(Expression expr, Copy copy) { final Expression exprNoParen = removeParentheses(expr); if (exprNoParen.getNodeType() == PREFIX_EXPRESSION) { final PrefixExpression pe = (PrefixExpression) exprNoParen; if (hasOperator(pe, NOT)) { return copy.perform(this, removeParentheses(pe.getOperand())); } } return not(parenthesizeIfNeeded(copy.perform(this, expr))); }
// !!! -- same as in ExtractTempRefactoring private boolean isLiteralNodeSelected() throws JavaModelException { IExpressionFragment fragment = getSelectedExpression(); if (fragment == null) return false; Expression expression = fragment.getAssociatedExpression(); if (expression == null) return false; switch (expression.getNodeType()) { case ASTNode.BOOLEAN_LITERAL: case ASTNode.CHARACTER_LITERAL: case ASTNode.NULL_LITERAL: case ASTNode.NUMBER_LITERAL: return true; default: return false; } }
/* (non-Javadoc) * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ArrayAccess) */ public boolean visit(ArrayAccess node) { PTArrayAccess aa = InstantiationFactory.eINSTANCE.createPTArrayAccess(); List indexes = aa.getIndexes(); Expression arrayExp = node; while (arrayExp.getNodeType() == ASTNode.ARRAY_ACCESS) { // Visit the index to get the index expression. ArrayAccess array = (ArrayAccess) arrayExp; indexes.add( 0, perform( array .getIndex())); // We're trying to create the final expression from inside out, the // indexes are created in reverse order. arrayExp = array.getArray(); } aa.setArray(perform(arrayExp)); // Final arrayExp is the true expression. expression = aa; // Set the return expression for this visit. return 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$ } }
/** * Parenthesizes the provided expression if its type requires it. * * @param expr the expression to conditionally return parenthesized * @return the parenthesized expression of the provided expression to return or this expression * itself */ public Expression parenthesizeIfNeeded(Expression expr) { if (expr.getNodeType() == INFIX_EXPRESSION || expr.getNodeType() == INSTANCEOF_EXPRESSION) { return parenthesize(expr); } return expr; }
private void collectSideEffects(Expression expr, List<Expression> sideEffectExprs) { // local variable, parameter, enum constant, etc. // OR method starting with is*(), get*() // except atomic long, atomic integer, etc. switch (expr.getNodeType()) { case METHOD_INVOCATION: MethodInvocation mi = (MethodInvocation) expr; methodHasSideEffects(mi.resolveMethodBinding(), mi, sideEffectExprs); collectSideEffects(mi.getExpression(), sideEffectExprs); collectSideEffects(arguments(mi), sideEffectExprs); break; case SUPER_METHOD_INVOCATION: SuperMethodInvocation smi = (SuperMethodInvocation) expr; methodHasSideEffects(smi.resolveMethodBinding(), smi, sideEffectExprs); collectSideEffects(arguments(smi), sideEffectExprs); break; case CLASS_INSTANCE_CREATION: ClassInstanceCreation cic = (ClassInstanceCreation) expr; methodHasSideEffects(cic.resolveConstructorBinding(), cic, sideEffectExprs); collectSideEffects(cic.getExpression(), sideEffectExprs); collectSideEffects(arguments(cic), sideEffectExprs); break; case ARRAY_ACCESS: ArrayAccess aa = (ArrayAccess) expr; collectSideEffects(aa.getArray(), sideEffectExprs); collectSideEffects(aa.getIndex(), sideEffectExprs); break; case ARRAY_CREATION: ArrayCreation ac = (ArrayCreation) expr; collectSideEffects(ac.getInitializer(), sideEffectExprs); collectSideEffects(ac.dimensions(), sideEffectExprs); break; case ARRAY_INITIALIZER: ArrayInitializer ai = (ArrayInitializer) expr; collectSideEffects(expressions(ai), sideEffectExprs); break; case ASSIGNMENT: Assignment as = (Assignment) expr; collectSideEffects(as.getLeftHandSide(), sideEffectExprs); collectSideEffects(as.getRightHandSide(), sideEffectExprs); break; case CONDITIONAL_EXPRESSION: ConditionalExpression ce = (ConditionalExpression) expr; collectSideEffects(ce.getExpression(), sideEffectExprs); collectSideEffects(ce.getThenExpression(), sideEffectExprs); collectSideEffects(ce.getElseExpression(), sideEffectExprs); break; case FIELD_ACCESS: FieldAccess fa = (FieldAccess) expr; collectSideEffects(fa.getExpression(), sideEffectExprs); collectSideEffects(fa.getName(), sideEffectExprs); break; case SUPER_FIELD_ACCESS: SuperFieldAccess sfa = (SuperFieldAccess) expr; collectSideEffects(sfa.getQualifier(), sideEffectExprs); collectSideEffects(sfa.getName(), sideEffectExprs); break; case THIS_EXPRESSION: collectSideEffects(((ThisExpression) expr).getQualifier(), sideEffectExprs); break; case VARIABLE_DECLARATION_EXPRESSION: collectSideEffects((VariableDeclarationExpression) expr, sideEffectExprs); break; case INFIX_EXPRESSION: InfixExpression ie = (InfixExpression) expr; collectSideEffects(ie.getLeftOperand(), sideEffectExprs); collectSideEffects(ie.getRightOperand(), sideEffectExprs); collectSideEffects(extendedOperands(ie), sideEffectExprs); break; case CAST_EXPRESSION: collectSideEffects(((CastExpression) expr).getExpression(), sideEffectExprs); break; case INSTANCEOF_EXPRESSION: collectSideEffects(((InstanceofExpression) expr).getLeftOperand(), sideEffectExprs); break; case PARENTHESIZED_EXPRESSION: collectSideEffects(((ParenthesizedExpression) expr).getExpression(), sideEffectExprs); break; case POSTFIX_EXPRESSION: sideEffectExprs.add(expr); break; case PREFIX_EXPRESSION: PrefixExpression pe = (PrefixExpression) expr; PrefixExpression.Operator op = pe.getOperator(); if (PrefixExpression.Operator.INCREMENT.equals(op) | PrefixExpression.Operator.DECREMENT.equals(op)) { sideEffectExprs.add(pe); } else { collectSideEffects(pe.getOperand(), sideEffectExprs); } break; default: // literals // names } }