@SuppressWarnings("unchecked") private void createUnobservedInitStmt(final int logRecNo, final Block methodBlock, final AST ast) { // NOTE: PLAIN INIT: has always one non-null param // TODO: use primitives final int oid = this.log.objectIds.get(logRecNo); final String type = this.log.oidClassNames.get(this.log.oidRecMapping.get(oid)); final Object value = this.log.params.get(logRecNo)[0]; this.isXStreamNeeded = true; final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment(); // handling because there must always be a new instantiation statement for pseudo inits this.oidToVarMapping.remove(oid); vd.setName(ast.newSimpleName(this.createNewVarName(oid, type))); final MethodInvocation methodInvocation = ast.newMethodInvocation(); final Name name = ast.newSimpleName("XSTREAM"); methodInvocation.setExpression(name); methodInvocation.setName(ast.newSimpleName("fromXML")); final StringLiteral xmlParam = ast.newStringLiteral(); xmlParam.setLiteralValue((String) value); methodInvocation.arguments().add(xmlParam); final CastExpression castExpr = ast.newCastExpression(); castExpr.setType(this.createAstType(type, ast)); castExpr.setExpression(methodInvocation); vd.setInitializer(castExpr); final VariableDeclarationStatement vs = ast.newVariableDeclarationStatement(vd); vs.setType(this.createAstType(type, ast)); methodBlock.statements().add(vs); }
private static boolean needsOuterParantheses(ASTNode nodeToCast) { ASTNode parent = nodeToCast.getParent(); if (parent instanceof MethodInvocation) { if (((MethodInvocation) parent).getExpression() == nodeToCast) { return true; } } else if (parent instanceof QualifiedName) { if (((QualifiedName) parent).getQualifier() == nodeToCast) { return true; } } else if (parent instanceof FieldAccess) { if (((FieldAccess) parent).getExpression() == nodeToCast) { return true; } } return false; }
@Override public Expression apply(Object object, MethodInvocation whenMethodInvocation) { MethodInvocation methodInvocation = methodInvocation(object, thenThrow).get(); List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { MethodInvocation mockedMethodInvocation = (MethodInvocation) whenMethodInvocation.arguments().get(0); Expression toBeThrown = argumentAsExpression(arguments.get(0)); Expression throwingClosure = groovyClosureBuilder.aClosure() .withBodyStatement(nodeFactory.throwStatement(toBeThrown)) .build() .asExpression(); return nodeFactory.infixExpression(RIGHT_SHIFT_SIGNED, mockedMethodWithMatchers(mockedMethodInvocation), throwingClosure); } throw new UnsupportedOperationException("Supported only 1-arity thenThrow invocation"); }
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) { AST ast= rewrite.getAST(); ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite); if (fCastType != null) { return importRewrite.addImport(fCastType, ast,context, TypeLocation.CAST); } ASTNode node= fNodeToCast; ASTNode parent= node.getParent(); if (parent instanceof CastExpression) { node= parent; parent= parent.getParent(); } while (parent instanceof ParenthesizedExpression) { node= parent; parent= parent.getParent(); } if (parent instanceof MethodInvocation) { MethodInvocation invocation= (MethodInvocation) node.getParent(); if (invocation.getExpression() == node) { IBinding targetContext= ASTResolving.getParentMethodOrTypeBinding(node); ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext); if (bindings.length > 0) { ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding()); Type newTypeNode= importRewrite.addImport(first, ast, context, TypeLocation.CAST); return newTypeNode; } } } Type newCastType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$ return newCastType; }
private IStatus checkIteratorCondition() { List<Expression> initializers = getForStatement().initializers(); if (initializers.size() != 1) return SEMANTIC_CHANGE_WARNING_STATUS; Expression expression = initializers.get(0); if (!(expression instanceof VariableDeclarationExpression)) return SEMANTIC_CHANGE_WARNING_STATUS; VariableDeclarationExpression declaration = (VariableDeclarationExpression) expression; List<VariableDeclarationFragment> variableDeclarationFragments = declaration.fragments(); if (variableDeclarationFragments.size() != 1) return SEMANTIC_CHANGE_WARNING_STATUS; VariableDeclarationFragment declarationFragment = variableDeclarationFragments.get(0); Expression initializer = declarationFragment.getInitializer(); if (!(initializer instanceof MethodInvocation)) return SEMANTIC_CHANGE_WARNING_STATUS; MethodInvocation methodInvocation = (MethodInvocation) initializer; String methodName = methodInvocation.getName().getIdentifier(); if (!"iterator".equals(methodName) || methodInvocation.arguments().size() != 0) // $NON-NLS-1$ return SEMANTIC_CHANGE_WARNING_STATUS; return StatusInfo.OK_STATUS; }
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) { switch (invocation.getNodeType()) { case ASTNode.METHOD_INVOCATION: return MethodInvocation.ARGUMENTS_PROPERTY; case ASTNode.SUPER_METHOD_INVOCATION: return SuperMethodInvocation.ARGUMENTS_PROPERTY; case ASTNode.CONSTRUCTOR_INVOCATION: return ConstructorInvocation.ARGUMENTS_PROPERTY; case ASTNode.SUPER_CONSTRUCTOR_INVOCATION: return SuperConstructorInvocation.ARGUMENTS_PROPERTY; case ASTNode.CLASS_INSTANCE_CREATION: return ClassInstanceCreation.ARGUMENTS_PROPERTY; case ASTNode.ENUM_CONSTANT_DECLARATION: return EnumConstantDeclaration.ARGUMENTS_PROPERTY; default: throw new IllegalArgumentException(invocation.toString()); } }
private ITypeBinding getExpressionType(MethodInvocation invocation) { Expression expression = invocation.getExpression(); ITypeBinding typeBinding = null; if (expression == null) { typeBinding = invocation.resolveMethodBinding().getDeclaringClass(); } else { typeBinding = expression.resolveTypeBinding(); } if (typeBinding != null && typeBinding.isTypeVariable()) { ITypeBinding[] typeBounds = typeBinding.getTypeBounds(); if (typeBounds.length > 0) { for (ITypeBinding typeBound : typeBounds) { ITypeBinding expressionType = getExpressionType(invocation, typeBound); if (expressionType != null) { return expressionType; } } typeBinding = typeBounds[0].getTypeDeclaration(); } else { typeBinding = invocation.getAST().resolveWellKnownType("java.lang.Object"); // $NON-NLS-1$ } } Assert.isNotNull( typeBinding, "Type binding of target expression may not be null"); // $NON-NLS-1$ return typeBinding; }
private ITypeBinding getExpressionType( final MethodInvocation invocation, ITypeBinding typeBinding) { if (typeBinding == null) return null; for (IMethodBinding iMethodBinding : typeBinding.getDeclaredMethods()) { if (invocation.resolveMethodBinding() == iMethodBinding) return typeBinding.getTypeDeclaration(); } ITypeBinding expressionType = getExpressionType(invocation, typeBinding.getSuperclass()); if (expressionType != null) { return expressionType; } for (ITypeBinding interfaceBinding : typeBinding.getInterfaces()) { expressionType = getExpressionType(invocation, interfaceBinding); if (expressionType != null) { return expressionType; } } return null; }
private MethodInvocation createInvocation(AST ast, Expression operand, String operator) { Expression receiver = getReceiver(operand); MethodInvocation invocation = ast.newMethodInvocation(); invocation.setName(ast.newSimpleName(fSetter)); if (receiver != null) invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver)); InfixExpression argument = ast.newInfixExpression(); invocation.arguments().add(argument); if ("++".equals(operator)) { // $NON-NLS-1$ argument.setOperator(InfixExpression.Operator.PLUS); } else if ("--".equals(operator)) { // $NON-NLS-1$ argument.setOperator(InfixExpression.Operator.MINUS); } else { Assert.isTrue(false, "Should not happen"); // $NON-NLS-1$ } MethodInvocation getter = ast.newMethodInvocation(); getter.setName(ast.newSimpleName(fGetter)); if (receiver != null) getter.setExpression((Expression) fRewriter.createCopyTarget(receiver)); argument.setLeftOperand(getter); argument.setRightOperand(ast.newNumberLiteral("1")); // $NON-NLS-1$ fReferencingGetter = true; fReferencingSetter = true; return invocation; }
/** * If we are creating a conservative slice, we treat both * objects on which we call methods as well as arguments * for method calls as dependencies. This makes the * assumption that the method will modify the caller * and the arguments. * @param node * @return */ @Override public boolean visit(MethodInvocation node){ if(this.options.contains(Slicer.Options.CONSERVATIVE)){ /* The expression part (eg. 'result.setFast(arg1, arg2)' expression = 'result' */ Expression expression = node.getExpression(); this.visitExpression(expression, new NoBindingsMethodVisitor(this.aliases)); if(this.result) return false; /* The argument (eg. 'result.setFast(arg1, arg2)' arguments = {'arg1', 'arg2'}. */ List<Expression> arguments = node.arguments(); for(Expression argument : arguments){ this.visitExpression(argument, new NoBindingsAssignmentVisitor(this.aliases)); } } return false; }
/** * Generates the initializer for an iterator based <code>for</code> loop, which declares and * initializes the variable to loop over. * * @param rewrite the instance of {@link ASTRewrite} * @param loopVariableName the proposed name of the loop variable * @return a {@link VariableDeclarationExpression} to use as initializer */ private VariableDeclarationExpression getIteratorBasedForInitializer( ASTRewrite rewrite, SimpleName loopVariableName) { AST ast = rewrite.getAST(); IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy( fExpressionType, "iterator", new ITypeBinding[] {}); // $NON-NLS-1$ // initializing fragment VariableDeclarationFragment varDeclarationFragment = ast.newVariableDeclarationFragment(); varDeclarationFragment.setName(loopVariableName); MethodInvocation iteratorExpression = ast.newMethodInvocation(); iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName())); iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression)); varDeclarationFragment.setInitializer(iteratorExpression); // declaration VariableDeclarationExpression varDeclarationExpression = ast.newVariableDeclarationExpression(varDeclarationFragment); varDeclarationExpression.setType( getImportRewrite() .addImport( iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite()))); return varDeclarationExpression; }
@SuppressWarnings("unchecked") @Override public void createCollectionInitStmt(final CaptureLog log, final int logRecNo) { final int oid = log.objectIds.get(logRecNo); final Object[] params = log.params.get(logRecNo); String collTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid)); final Class<?> collType = getClassForName(collTypeName); final String varName; // -- determine if an alternative collection must be used for code generation final boolean isPrivate = java.lang.reflect.Modifier.isPrivate(collType.getModifiers()); if(isPrivate || ! hasDefaultConstructor(collType)) { if(Set.class.isAssignableFrom(collType)) { collTypeName = HashSet.class.getName(); } else if (List.class.isAssignableFrom(collType)) { collTypeName = ArrayList.class.getName(); } else if(Queue.class.isAssignableFrom(collType)) { collTypeName = ArrayDeque.class.getName(); } else { throw new RuntimeException("Collection " + collType + " is not supported"); } } // -- create code for instantiating collection varName = this.createNewVarName(oid, collTypeName); final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment(); final SimpleName varNameExpr = ast.newSimpleName(varName); vd.setName(varNameExpr); final ClassInstanceCreation ci = ast.newClassInstanceCreation(); ci.setType(this.createAstType(collTypeName, ast)); vd.setInitializer(ci); final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd); stmt.setType(this.createAstType(collTypeName, ast)); methodBlock.statements().add(stmt); // --- create code for filling the collection Integer paramOID; MethodInvocation mi; for(int i = 0; i < params.length; i++) { mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("add")); paramOID = (Integer) params[i]; if(paramOID == null) { mi.arguments().add(ast.newNullLiteral()); } else { mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(paramOID))); } methodBlock.statements().add(mi); } }
@Override public void createMapInitStmt(final CaptureLog log, final int logRecNo) { final int oid = log.objectIds.get(logRecNo); final Object[] params = log.params.get(logRecNo); String collTypeName = log.oidClassNames.get(log.oidRecMapping.get(oid)); final Class<?> collType = getClassForName(collTypeName); final String varName; // -- determine if an alternative collection must be used for code generation final boolean isPrivate = java.lang.reflect.Modifier.isPrivate(collType.getModifiers()); if(isPrivate || ! hasDefaultConstructor(collType)) { collTypeName = HashMap.class.getName(); } // -- create code for instantiating collection varName = this.createNewVarName(oid, collTypeName); final VariableDeclarationFragment vd = ast.newVariableDeclarationFragment(); final SimpleName varNameExpr = ast.newSimpleName(varName); vd.setName(varNameExpr); final ClassInstanceCreation ci = ast.newClassInstanceCreation(); ci.setType(this.createAstType(collTypeName, ast)); vd.setInitializer(ci); final VariableDeclarationStatement stmt = ast.newVariableDeclarationStatement(vd); stmt.setType(this.createAstType(collTypeName, ast)); methodBlock.statements().add(stmt); // --- create code for filling the collection Integer valueOID; Integer keyOID; MethodInvocation mi; for(int i = 0; i + 1< params.length; i+=2) { mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("put")); keyOID = (Integer) params[i]; mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(keyOID))); valueOID = (Integer) params[i + 1]; if(valueOID == null) { mi.arguments().add(ast.newNullLiteral()); } else { mi.arguments().add(ast.newSimpleName(this.oidToVarMapping.get(valueOID))); } methodBlock.statements().add(mi); } }
@Override public Expression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return argumentAsExpression(arguments.get(0)); } if (arguments.size() == 2) { return argumentAsExpression(arguments.get(1)); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertTrue invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 2) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(1)), argumentAsExpression(arguments.get(0))); } if (arguments.size() == 3) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(2)), argumentAsExpression(arguments.get(1))); } throw new UnsupportedOperationException("Supported only 2-, 3-arity assertEquals/assertArrayEquals invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(0)), astNodeFactory.nullLiteral()); } if (arguments.size() == 2) { return astNodeFactory.infixExpression(EQUALS, argumentAsExpression(arguments.get(1)), astNodeFactory.nullLiteral()); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertNull invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return astNodeFactory.infixExpression(NOT_EQUALS, argumentAsExpression(arguments.get(0)), astNodeFactory.nullLiteral()); } if (arguments.size() == 2) { return astNodeFactory.infixExpression(NOT_EQUALS, argumentAsExpression(arguments.get(1)), astNodeFactory.nullLiteral()); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertNotNull invocation"); }
@Override public Expression apply(Object object, MethodInvocation methodInvocation) { List arguments = methodInvocation.arguments(); if (arguments.size() == 1) { return astNodeFactory.prefixExpression(NOT, argumentAsExpression(arguments.get(0))); } if (arguments.size() == 2) { return astNodeFactory.prefixExpression(NOT, argumentAsExpression(arguments.get(1))); } throw new UnsupportedOperationException("Supported only 1-, 2-arity assertFalse invocation"); }
@Override public InfixExpression apply(Object object, MethodInvocation verifyMethodInvocation) { List arguments = verifyMethodInvocation.arguments(); return nodeFactory.infixExpression(TIMES, nodeFactory.numberLiteral("0"), nodeFactory.fieldAccess("_", nodeFactory.clone((Expression) arguments.get(0)))); }
@Override public InfixExpression apply(Object object, MethodInvocation verifyMethodInvocation) { List arguments = verifyMethodInvocation.arguments(); MethodInvocation parentMethodInvocation = (MethodInvocation) verifyMethodInvocation.getParent(); return nodeFactory.infixExpression(TIMES, cardinality(arguments.size() == 2 ? Optional.of(arguments.get(1)) : empty()), nodeFactory.methodInvocation(parentMethodInvocation.getName().getFullyQualifiedName(), (List<Expression>) parentMethodInvocation.arguments().stream() .map(matcherHandler::applyMatchers).collect(toList()), nodeFactory.clone((Expression) arguments.get(0)))); }
@Override public final boolean visit(MethodInvocation node) { IMethodBinding methodBinding = node.resolveMethodBinding(); if (methodBinding != null) { handleTypeBinding(node, methodBinding.getDeclaringClass(), false); handleMethodBinding(node, methodBinding); } return true; }
@Override public boolean visit(MethodInvocation node) { List<?> arguments = node.arguments(); for(int i = 0; i < arguments.size(); i++) { Object arg = arguments.get(i); if(arg instanceof SimpleName) { VariableInfo varInfo = current.getVariable(arg.toString()); if(varInfo != null) varInfo.addOperation(VariableOperation.Type.PARAM, node.getName().toString(), arguments.size(), i); } } return true; }
public MethodInvocation createStaticMethodInvocation(AST ast, String className, String methodName) { SimpleName typeName = ast.newSimpleName(className); MethodInvocation methodInvocation = ast.newMethodInvocation(); methodInvocation.setName(ast.newSimpleName(methodName)); methodInvocation.setExpression(typeName); return methodInvocation; }
public Block createReturnBlock(AST ast, TypeDeclaration builderType, String withName, String parameterName) { Block builderMethodBlock = ast.newBlock(); ReturnStatement returnStatement = ast.newReturnStatement(); ClassInstanceCreation newClassInstanceCreation = ast.newClassInstanceCreation(); newClassInstanceCreation.setType(ast.newSimpleType(ast.newName(builderType.getName().toString()))); MethodInvocation withMethodInvocation = ast.newMethodInvocation(); withMethodInvocation.setExpression(newClassInstanceCreation); withMethodInvocation.setName(ast.newSimpleName(withName)); withMethodInvocation.arguments().add(ast.newSimpleName(parameterName)); returnStatement.setExpression(withMethodInvocation); builderMethodBlock.statements().add(returnStatement); return builderMethodBlock; }
/** * Returns whether an expression at the given location needs explicit boxing. * * @param expression the expression * @return <code>true</code> iff an expression at the given location needs explicit boxing * @since 3.6 */ private static boolean needsExplicitBoxing(Expression expression) { StructuralPropertyDescriptor locationInParent= expression.getLocationInParent(); if (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) { return needsExplicitBoxing((ParenthesizedExpression) expression.getParent()); } if (locationInParent == ClassInstanceCreation.EXPRESSION_PROPERTY || locationInParent == FieldAccess.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.EXPRESSION_PROPERTY) { return true; } return false; }
/** * Returns the receiver's type binding of the given method invocation. * * @param invocation method invocation to resolve type of * @return the type binding of the receiver */ public static ITypeBinding getReceiverTypeBinding(MethodInvocation invocation) { ITypeBinding result= null; Expression exp= invocation.getExpression(); if(exp != null) { return exp.resolveTypeBinding(); } else { AbstractTypeDeclaration type= (AbstractTypeDeclaration)getParent(invocation, AbstractTypeDeclaration.class); if (type != null) { return type.resolveBinding(); } } return result; }
@Override public boolean visit(MethodInvocation node) { Expression receiver = node.getExpression(); if (receiver == null) { SimpleName name = node.getName(); if (fIgnoreBinding == null || !Bindings.equals(fIgnoreBinding, name.resolveBinding())) { node.getName().accept(this); } } else { receiver.accept(this); } accept(node.arguments()); return false; }
@Override public boolean visit(MethodInvocation node) { Expression exp = node.getExpression(); if (exp != null) { fIgnore.add(node.getName()); } return true; }
private static boolean needsOuterParantheses(ASTNode nodeToCast) { ASTNode parent= nodeToCast.getParent(); if (parent instanceof MethodInvocation) { if (((MethodInvocation)parent).getExpression() == nodeToCast) { return true; } } else if (parent instanceof QualifiedName) { if (((QualifiedName)parent).getQualifier() == nodeToCast) { return true; } } else if (parent instanceof FieldAccess) { if (((FieldAccess)parent).getExpression() == nodeToCast) { return true; } } return false; }
private Type evaluateVariableType(AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext, TypeLocation location) { if (fOriginalNode.getParent() instanceof MethodInvocation) { MethodInvocation parent= (MethodInvocation) fOriginalNode.getParent(); if (parent.getExpression() == fOriginalNode) { // _x_.foo() -> guess qualifier type by looking for a type with method 'foo' ITypeBinding[] bindings= ASTResolving.getQualifierGuess(fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext); if (bindings.length > 0) { return imports.addImport(bindings[0], ast, importRewriteContext, location); } } } ITypeBinding binding= ASTResolving.guessBindingForReference(fOriginalNode); if (binding != null) { if (binding.isWildcardType()) { binding= ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast); if (binding == null) { // only null binding applies binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$ } } return imports.addImport(binding, ast, importRewriteContext, location); } // no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode); if (type != null) { return type; } if (fVariableKind == CONST_FIELD) { return ast.newSimpleType(ast.newSimpleName("String")); //$NON-NLS-1$ } return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$ }
public ExpressionVisitor(ASTNode enclosingDeclartion, BaseTraceability trace) { AstNode expression = trace.getExpression(); if(expression instanceof ast.ClassInstanceCreation){ expressionType = ExpressionType.CLASS_INSTANCE_CREATION; expressionToFind =((ast.ClassInstanceCreation) expression).complexExpression; }else if(expression instanceof ast.FieldWrite){ expressionType = ExpressionType.FIELD_WRITE; expressionToFind = ((ast.FieldWrite) expression).complexExpression; }else if(expression instanceof ast.FieldAccess){ expressionType = ExpressionType.FIELD_ACCESS; expressionToFind = ((ast.FieldAccess) expression).complexExpression; }else if(expression instanceof ast.MethodInvocation){ expressionToFind =((ast.MethodInvocation) expression).complexExpression; expressionType = ExpressionType.METHOD_INVOCATION; } enclosingDeclaration = enclosingDeclartion; enclosingDeclaration.accept(this); }
@Override public boolean visit(MethodInvocation loc) { if (currentMethod == null) return false; if (loc.resolveMethodBinding() == null) return false; if (loc.resolveMethodBinding().getMethodDeclaration() == null || loc.resolveMethodBinding().getMethodDeclaration().getJavaElement() == null) return true; if (loc.resolveMethodBinding().getMethodDeclaration().getJavaElement().isReadOnly()) return true; addCalledMethod(loc.resolveMethodBinding().getMethodDeclaration()); return true; }
@Override public boolean visit(MethodInvocation methodInvocation) { IMethodBinding methodBinding = methodInvocation.resolveMethodBinding(); if (methodBinding != null) { IJavaElement javaElement = methodBinding.getJavaElement(); if (javaElement == null) MigrateSkeletalImplementationToInterfaceRefactoringProcessor .logWarning("Could not get Java element from binding: " + methodBinding + " while processing: " + methodInvocation); else if (javaElement.equals(accessedMethod)) { Expression expression = methodInvocation.getExpression(); expression = (Expression) Util.stripParenthesizedExpressions(expression); // FIXME: It's not really that the expression is a `this` // expression but that the type of the expression comes from // a // `this` expression. In other words, we may need to climb // the // AST. if (expression == null || expression.getNodeType() == ASTNode.THIS_EXPRESSION) this.encounteredThisReceiver = true; } } return super.visit(methodInvocation); }
public boolean visit(MethodInvocation node) { IMethodBinding mmtb = node.resolveMethodBinding(); if (this.mtbStack.isEmpty()) { return true; } try { if (node.getExpression() != null) { if (mmtb.getDeclaringClass().getQualifiedName().startsWith("java.awt.geom.Path2D")) { Expression e = node.getExpression(); ITypeBinding itb = e.resolveTypeBinding(); this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding)this.mtbStack.peek()), getQualifiedName(itb) + "#" + getSimpleName(mmtb))); break label179; } } this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding)this.mtbStack.peek()), getQualifiedName(mmtb))); } catch (Exception localException) { System.err.println("Cannot resolve method invocation \"" + node.getName().toString() + "\""); } label179: return true; }
private RefactoringStatus rewriteInfixExpression(ASTRewrite astRewrite, ImportRewrite importRewrite, InfixExpression ie, String fullyQualifiedTypeName) { final RefactoringStatus status = new RefactoringStatus(); final AST ast = ie.getAST(); final Expression leftExpCopy = (Expression) ASTNode.copySubtree(ast, ie.getLeftOperand()); final Expression rightExpCopy = (Expression) ASTNode.copySubtree(ast, ie.getRightOperand()); final NumberLiteral zero = ast.newNumberLiteral(); astRewrite.replace(ie.getRightOperand(), zero, null); final MethodInvocation newInvocation = ast.newMethodInvocation(); newInvocation.setExpression(leftExpCopy); newInvocation.setName(ast.newSimpleName("compareTo")); //$NON-NLS-1$ newInvocation.arguments().add(rightExpCopy); astRewrite.replace(ie.getLeftOperand(), newInvocation, null); if (((ASTNode) newInvocation.arguments().get(0)).getNodeType() == ASTNode.SIMPLE_NAME && this.fieldsToRefactor.contains(((SimpleName) ie .getRightOperand()).resolveBinding().getJavaElement())) this.rewriteReference(astRewrite, importRewrite, (SimpleName) newInvocation.arguments().get(0), fullyQualifiedTypeName); if (((ASTNode) newInvocation.getExpression()).getNodeType() == ASTNode.SIMPLE_NAME && this.fieldsToRefactor.contains(((SimpleName) ie .getLeftOperand()).resolveBinding().getJavaElement())) this.rewriteReference(astRewrite, importRewrite, (SimpleName) newInvocation.getExpression(), fullyQualifiedTypeName); return status; }
@Override public boolean visit(MethodInvocation node) { Expression receiver = node.getExpression(); if (receiver == null) { SimpleName name = node.getName(); if (fIgnoreBinding == null || !Bindings.equals(fIgnoreBinding, name.resolveBinding())) node.getName().accept(this); } else { receiver.accept(this); } accept(node.arguments()); return false; }
@Override public boolean visit(MethodInvocation node) { IMethodBinding binding = node.resolveMethodBinding(); if (binding != null && !JdtFlags.isStatic(binding) && node.getExpression() == null && Bindings.isSuperType(binding.getDeclaringClass(), fFunctionalInterface, false)) throw new AbortSearchException(); return true; }
/** {@inheritDoc} */ @Override public boolean visit(MethodInvocation node) { if (!fFindUnqualifiedMethodAccesses && !fFindUnqualifiedStaticMethodAccesses) return true; if (node.getExpression() != null) return true; IBinding binding = node.getName().resolveBinding(); if (!(binding instanceof IMethodBinding)) return true; handleMethod(node.getName(), (IMethodBinding) binding); return true; }
/** {@inheritDoc} */ @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { TextEditGroup group = createTextEditGroup( FixMessages.CodeStyleFix_ChangeAccessUsingDeclaring_description, cuRewrite); if (fQualifier instanceof MethodInvocation || fQualifier instanceof ClassInstanceCreation) extractQualifier(fQualifier, cuRewrite, group); Type type = importType( fDeclaringTypeBinding, fQualifier, cuRewrite.getImportRewrite(), cuRewrite.getRoot()); cuRewrite.getASTRewrite().replace(fQualifier, type, group); }