List of usage examples for org.eclipse.jdt.core.dom MethodInvocation getAST
public final AST getAST()
From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java
License:Open Source License
@Override public boolean visit(MethodInvocation node) { if (node.getExpression() != null) { node.getExpression().accept(this); this.fBuffer.append(".");//$NON-NLS-1$ }/*w ww . j av a 2 s . c om*/ if (node.getAST().apiLevel() >= JLS3) { if (!node.typeArguments().isEmpty()) { this.fBuffer.append("<");//$NON-NLS-1$ for (Iterator<Type> it = node.typeArguments().iterator(); it.hasNext();) { Type t = it.next(); t.accept(this); if (it.hasNext()) { this.fBuffer.append(",");//$NON-NLS-1$ } } this.fBuffer.append(">");//$NON-NLS-1$ } } node.getName().accept(this); this.fBuffer.append("(");//$NON-NLS-1$ for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext();) { Expression e = it.next(); e.accept(this); if (it.hasNext()) { this.fBuffer.append(",");//$NON-NLS-1$ } } this.fBuffer.append(")");//$NON-NLS-1$ return false; }
From source file:ch.acanda.eclipse.pmd.java.resolution.design.EqualsNullQuickFix.java
License:Open Source License
/** * Replaces {@code x.equals(null)} with {@code x == null}. *///from w w w . j a v a2s .c o m @Override protected boolean apply(final MethodInvocation node) { final AST ast = node.getAST(); final InfixExpression infix = (InfixExpression) ast.createInstance(InfixExpression.class); infix.setOperator(Operator.EQUALS); infix.setLeftOperand(ASTUtil.copy(node.getExpression())); infix.setRightOperand((NullLiteral) ast.createInstance(NullLiteral.class)); ASTUtil.replace(node, infix); return true; }
From source file:ch.acanda.eclipse.pmd.java.resolution.optimization.SimplifyStartsWithQuickFix.java
License:Open Source License
/** * Rewrites <code>s.startsWith("a")</code> as <code>s.charAt(0) == 'a'</code>. *//*from w ww. java2s . c o m*/ @Override @SuppressWarnings("unchecked") protected boolean apply(final MethodInvocation node) { final AST ast = node.getAST(); final MethodInvocation charAt = ast.newMethodInvocation(); charAt.setExpression(copy(node.getExpression())); charAt.setName(ast.newSimpleName("charAt")); charAt.arguments().add(ast.newNumberLiteral("0")); final CharacterLiteral character = ast.newCharacterLiteral(); final StringLiteral s = (StringLiteral) node.arguments().get(0); character.setEscapedValue(s.getEscapedValue().replace('"', '\'')); final InfixExpression eq = ast.newInfixExpression(); eq.setOperator(Operator.EQUALS); eq.setLeftOperand(charAt); eq.setRightOperand(character); replace(node, eq); return true; }
From source file:ch.acanda.eclipse.pmd.java.resolution.stringandstringbuffer.UnnecessaryCaseChangeQuickFix.java
License:Open Source License
/** * Removes the <code>.toString()</code> from <code>"foo".toString()</code> if the expression is only a part of an * statement. Removes the expression completely if it is the whole statement. *//*from w w w. ja va2s . c om*/ @Override @SuppressWarnings("unchecked") protected boolean apply(final MethodInvocation node) { final AST ast = node.getAST(); final MethodInvocation invocation = ast.newMethodInvocation(); if (node.getExpression().getNodeType() == ASTNode.METHOD_INVOCATION) { invocation.setExpression(removeCaseChange((MethodInvocation) node.getExpression())); invocation.setName(ast.newSimpleName("equalsIgnoreCase")); invocation.arguments().add(copy((Expression) node.arguments().get(0))); return replace(node, invocation); } return false; }
From source file:ch.acanda.eclipse.pmd.java.resolution.stringandstringbuffer.UseIndexOfCharQuickFix.java
License:Open Source License
/** * Replaces the string literal <code>"a"</code> in <code>s.indexOf("a")</code> with the character literal * <code>'a'</code>.//from w w w .jav a 2 s . co m */ @Override protected boolean apply(final MethodInvocation node) { @SuppressWarnings("unchecked") final List<Expression> arguments = node.arguments(); if (arguments.size() == 1 && arguments.get(0) instanceof StringLiteral) { final CharacterLiteral character = node.getAST().newCharacterLiteral(); final StringLiteral string = (StringLiteral) arguments.get(0); character.setEscapedValue(toCharValue(string.getEscapedValue())); replace(string, character); return true; } return false; }
From source file:coloredide.utils.CopiedNaiveASTFlattener.java
License:Open Source License
public boolean visit(MethodInvocation node) { if (node.getExpression() != null) { node.getExpression().accept(this); this.buffer.append(".");//$NON-NLS-1$ }//w w w . ja va 2 s . co m if (node.getAST().apiLevel() >= AST.JLS3) { if (!node.typeArguments().isEmpty()) { this.buffer.append("<");//$NON-NLS-1$ for (Iterator it = node.typeArguments().iterator(); it.hasNext();) { Type t = (Type) it.next(); t.accept(this); if (it.hasNext()) { this.buffer.append(",");//$NON-NLS-1$ } } this.buffer.append(">");//$NON-NLS-1$ } } node.getName().accept(this); this.buffer.append("(");//$NON-NLS-1$ for (Iterator it = node.arguments().iterator(); it.hasNext();) { Expression e = (Expression) it.next(); e.accept(this); if (it.hasNext()) { this.buffer.append(",");//$NON-NLS-1$ } } this.buffer.append(")");//$NON-NLS-1$ return false; }
From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from www .ja va 2 s . c om*/ public boolean visit(MethodInvocation node) { invocations.push(node); String methodName = NameTable.getName(node.getName()); IMethodBinding binding = Types.getMethodBinding(node); assert binding != null; // Object receiving the message, or null if it's a method in this class. Expression receiver = node.getExpression(); ITypeBinding receiverType = receiver != null ? Types.getTypeBinding(receiver) : null; buffer.append(' '); if ((receiverType != null) && (receiver instanceof SimpleName)) { buffer.append(((SimpleName) receiver).getIdentifier()).append('.'); } if (Types.isFunction(binding)) { buffer.append(methodName); buffer.append("("); for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext();) { it.next().accept(this); if (it.hasNext()) { buffer.append(", "); } } buffer.append(")"); } else { boolean castAttempted = false; boolean castReturnValue = false; if (node.getParent() instanceof Expression || node.getParent() instanceof ReturnStatement || node.getParent() instanceof VariableDeclarationFragment) { ITypeBinding actualType = binding.getMethodDeclaration().getReturnType(); if (actualType.isArray()) { actualType = Types.resolveArrayType(actualType.getComponentType()); } ITypeBinding expectedType; if (node.getParent() instanceof VariableDeclarationFragment) { expectedType = Types.getTypeBinding(node.getParent()); } else { expectedType = binding.getReturnType(); } if (expectedType.isArray()) { expectedType = Types.resolveArrayType(expectedType.getComponentType()); } if (!actualType.isAssignmentCompatible(expectedType)) { if (!actualType.isEqualTo(node.getAST().resolveWellKnownType("void"))) { // Since type parameters aren't passed to Obj-C, add cast for it. // However, this is only needed with nested invocations. if (invocations.size() > 0) { // avoid a casting again below, and know to print a closing ')' // after the method invocation. castReturnValue = printCast(expectedType); castAttempted = true; } } } } ITypeBinding typeBinding = binding.getDeclaringClass(); if (receiver != null) { boolean castPrinted = false; IMethodBinding methodReceiver = Types.getMethodBinding(receiver); if (methodReceiver != null) { if (methodReceiver.isConstructor()) { // gcc sometimes fails to discern the constructor's type when // chaining, so add a cast. if (!castAttempted) { castPrinted = printCast(typeBinding); castAttempted = true; } } else { ITypeBinding receiverReturnType = methodReceiver.getReturnType(); if (receiverReturnType.isInterface()) { // Add interface cast, so Obj-C knows the type node's receiver is. if (!castAttempted) { castPrinted = printCast(receiverReturnType); castAttempted = true; } } } } else { IVariableBinding var = Types.getVariableBinding(receiver); if (var != null) { if (Types.variableHasCast(var)) { castPrinted = printCast(Types.getCastForVariable(var)); } } } // printNilCheck(receiver, !castPrinted); if (castPrinted) { buffer.append(')'); } } else { // if ((binding.getModifiers() & Modifier.STATIC) > 0) { // buffer.append(NameTable.getFullName(typeBinding)); // } else { // buffer.append("self"); // } } if (binding instanceof IOSMethodBinding) { buffer.append(binding.getName()); } else { buffer.append(methodName); } buffer.append("("); printArguments(binding, node.arguments()); buffer.append(")"); if (castReturnValue) { buffer.append(')'); } } invocations.pop(); return false; }
From source file:com.google.devtools.j2cpp.translate.GwtConverter.java
License:Open Source License
@Override public boolean visit(MethodInvocation node) { IMethodBinding method = Types.getMethodBinding(node); @SuppressWarnings("unchecked") List<Expression> args = node.arguments(); if (method.getName().equals("create") && method.getDeclaringClass().getQualifiedName().equals(GWT_CLASS) && args.size() == 1) { // Convert GWT.create(Foo.class) to Foo.class.newInstance(). AST ast = node.getAST(); SimpleName name = ast.newSimpleName("newInstance"); node.setName(name);//from w w w .j a v a 2 s .c o m Expression clazz = NodeCopier.copySubtree(ast, args.get(0)); args.remove(0); node.setExpression(clazz); GeneratedMethodBinding newBinding = new GeneratedMethodBinding("newInstance", 0, ast.resolveWellKnownType("java.lang.Object"), ast.resolveWellKnownType("java.lang.Class"), false, false, false); Types.addBinding(name, newBinding); Types.addBinding(node, newBinding); } else if (isGwtTest(node)) { J2ObjC.error(node, "GWT.isScript() detected in boolean expression, which is not supported"); } return true; }
From source file:com.google.devtools.j2cpp.translate.JavaToIOSTypeConverter.java
License:Open Source License
@Override public boolean visit(MethodInvocation node) { Expression receiver = node.getExpression(); if (receiver instanceof SimpleName) { String name = ((SimpleName) receiver).getIdentifier(); String newName = Types.mapSimpleTypeName(name); if (name != newName) { // identity test SimpleName nameNode = node.getAST().newSimpleName(newName); Types.addBinding(nameNode, Types.getBinding(node)); node.setExpression(nameNode); }/*from w w w. j a va2 s .co m*/ } return super.visit(node); }
From source file:com.google.devtools.j2cpp.translate.Rewriter.java
License:Open Source License
/** * Rewrites System.out and System.err println calls as NSLog calls. * * @return true if the node was rewritten *//*w w w .j av a2 s . c o m*/ // TODO(user): remove when there is iOS console support. @SuppressWarnings("unchecked") private boolean rewriteSystemOut(MethodInvocation node) { Expression expression = node.getExpression(); if (expression instanceof Name) { Name expr = (Name) node.getExpression(); IBinding binding = expr.resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding varBinding = (IVariableBinding) binding; ITypeBinding type = varBinding.getDeclaringClass(); if (type == null) { return false; } String clsName = type.getQualifiedName(); String varName = varBinding.getName(); if (clsName.equals("java.lang.System") && (varName.equals("out") || varName.equals("err"))) { // Change System.out.* or System.err.* to NSLog AST ast = node.getAST(); MethodInvocation newInvocation = ast.newMethodInvocation(); IMethodBinding methodBinding = new IOSMethodBinding("NSLog", Types.getMethodBinding(node), null); Types.addBinding(newInvocation, methodBinding); Types.addFunction(methodBinding); newInvocation.setName(ast.newSimpleName("NSLog")); Types.addBinding(newInvocation.getName(), methodBinding); newInvocation.setExpression(null); // Insert NSLog format argument List<Expression> args = node.arguments(); if (args.size() == 1) { Expression arg = args.get(0); arg.accept(this); String format = getFormatArgument(arg); StringLiteral literal = ast.newStringLiteral(); literal.setLiteralValue(format); Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String")); newInvocation.arguments().add(literal); // JDT won't let nodes be re-parented, so copy and map. ASTNode newArg = NodeCopier.copySubtree(ast, arg); if (arg instanceof MethodInvocation) { IMethodBinding argBinding = ((MethodInvocation) arg).resolveMethodBinding(); if (!argBinding.getReturnType().isPrimitive()) { IOSMethodBinding newBinding = new IOSMethodBinding("format", argBinding, Types.getNSString()); Types.addMappedInvocation((MethodInvocation) newArg, newBinding); } } newInvocation.arguments().add(newArg); } else if (args.size() > 1 && node.getName().getIdentifier().equals("printf")) { newInvocation.arguments().addAll(NodeCopier.copySubtrees(ast, args)); } else if (args.size() == 0) { // NSLog requires a format string. StringLiteral literal = ast.newStringLiteral(); literal.setLiteralValue(""); Types.addBinding(literal, ast.resolveWellKnownType("java.lang.String")); newInvocation.arguments().add(literal); } // Replace old invocation with new. ASTNode parent = node.getParent(); if (parent instanceof ExpressionStatement) { ExpressionStatement stmt = (ExpressionStatement) parent; stmt.setExpression(newInvocation); } else { throw new AssertionError("unknown parent type: " + parent.getClass().getSimpleName()); } return true; } } } return false; }