if (((JCMethodInvocation) methodInvocationTree).getMethodSelect() instanceof JCIdent) { return null; expr = ((JCMethodInvocation) expr).getMethodSelect(); if (expr instanceof JCFieldAccess) { expr = ((JCFieldAccess) expr).getExpression();
JCExpression methodSelect = invocationTree.getMethodSelect(); Type rcvrType = (methodSelect instanceof JCFieldAccess)
private static boolean buildChain(ExpressionTree expr, VisitorState state, List<Symbol> chain) { while (expr instanceof JCMethodInvocation) { expr = ((JCMethodInvocation) expr).getMethodSelect(); // if the method isn't an immutable protobuf getter, return false if (!IS_IMMUTABLE_PROTO_GETTER.matches(expr, state)) { return false; } if (expr instanceof JCFieldAccess) { expr = ((JCFieldAccess) expr).getExpression(); } chain.add(ASTHelpers.getSymbol(expr)); } return true; }
@Override public void printStat(JCTree tree) throws IOException { if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) { JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression(); JCExpression select = invocation.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitComment")) { String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue(); print("// " + commentLiteral); return; } } super.printStat(tree); }
@Override public void visitApply(JCMethodInvocation tree) { JCExpression select = tree.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitCommentBefore")) { String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue(); JCExpression expr = tree.getArguments().get(1); try { print("/* " + commentLiteral + " */ "); } catch (IOException e) { throw new RuntimeException(e); } expr.accept(this); } else { super.visitApply(tree); } }
/** * Don't match the method that is invoked through {@code Mockito.verify(t)} or {@code * doReturn(val).when(t)}. */ private static boolean mockitoInvocation(Tree tree, VisitorState state) { if (!(tree instanceof JCMethodInvocation)) { return false; } JCMethodInvocation invocation = (JCMethodInvocation) tree; if (!(invocation.getMethodSelect() instanceof JCFieldAccess)) { return false; } ExpressionTree receiver = ASTHelpers.getReceiver(invocation); return MOCKITO_MATCHER.matches(receiver, state); } }
private boolean isStructuralMethod( JCTree.JCMethodInvocation tree ) { JCExpression methodSelect = tree.getMethodSelect(); if( methodSelect instanceof JCTree.JCFieldAccess ) { JCTree.JCFieldAccess m = (JCTree.JCFieldAccess)methodSelect; if( m.sym != null && !m.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC ) ) { JCExpression thisArg = m.selected; return TypeUtil.isStructuralInterface( _tp, thisArg.type.tsym ); } } return false; }
private boolean isStructuralMethod( JCTree.JCMethodInvocation tree ) { JCExpression methodSelect = tree.getMethodSelect(); if( methodSelect instanceof JCTree.JCFieldAccess ) { JCTree.JCFieldAccess m = (JCTree.JCFieldAccess)methodSelect; if( m.sym != null && !m.sym.getModifiers().contains( javax.lang.model.element.Modifier.STATIC ) ) { JCExpression thisArg = m.selected; return TypeUtil.isStructuralInterface( _tp, thisArg.type.tsym ); } } return false; }
@Override public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { JCMethodInvocation sourceNode = (JCMethodInvocation) node; MethodSymbol callee = (MethodSymbol) TreeInfo.symbol(sourceNode.getMethodSelect());
Type returnType = getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());
return Description.NO_MATCH; if (!MockAnswerStrategyScanner.scan(call.getMethodSelect(), state, badMocks)) { return Description.NO_MATCH;
ASTHelpers.getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());
/** * Returns the type of a receiver of a method call expression. Precondition: the expressionTree * corresponds to a method call. * * <p>Examples: * * <pre>{@code * a.b.foo() ==> type of a.b * a.bar().foo() ==> type of a.bar() * this.foo() ==> type of this * foo() ==> type of this * TheClass.aStaticMethod() ==> TheClass * aStaticMethod() ==> type of class in which method is defined * }</pre> */ public static Type getReceiverType(ExpressionTree expressionTree) { if (expressionTree instanceof JCFieldAccess) { JCFieldAccess methodSelectFieldAccess = (JCFieldAccess) expressionTree; return methodSelectFieldAccess.selected.type; } else if (expressionTree instanceof JCIdent) { JCIdent methodCall = (JCIdent) expressionTree; return methodCall.sym.owner.type; } else if (expressionTree instanceof JCMethodInvocation) { return getReceiverType(((JCMethodInvocation) expressionTree).getMethodSelect()); } else if (expressionTree instanceof JCMemberReference) { return ((JCMemberReference) expressionTree).getQualifierExpression().type; } throw new IllegalArgumentException( "Expected a JCFieldAccess or JCIdent from expression " + expressionTree); }
/** * Extracts the {@link Type} of the return value for {@link MethodInvocationTree} or {@link * MemberReferenceTree}, then checks it against {@link #capturedTypeAllowed(Type, VisitorState)}. */ private boolean isValidReturnValueType(ExpressionTree tree, VisitorState state) { Type returnType = null; if (tree instanceof MethodInvocationTree) { returnType = getReturnType(((JCMethodInvocation) tree).getMethodSelect()); } else if (tree instanceof MemberReferenceTree) { // Get the return type of the target referenced interface returnType = state.getTypes().findDescriptorType(((JCMemberReference) tree).type).getReturnType(); } if (returnType != null) { return capturedTypeAllowed(returnType, state); } return true; }
/** * Don't match the method that is invoked through {@code Mockito.verify(t)} or {@code * doReturn(val).when(t)}. */ private static boolean mockitoInvocation(Tree tree, VisitorState state) { if (!(tree instanceof JCMethodInvocation)) { return false; } JCMethodInvocation invocation = (JCMethodInvocation) tree; if (!(invocation.getMethodSelect() instanceof JCFieldAccess)) { return false; } ExpressionTree receiver = ASTHelpers.getReceiver(invocation); return MOCKITO_MATCHER.matches(receiver, state); } }
/** * Gives the return type of an ExpressionTree that represents a method select. * * <p>TODO(eaftan): Are there other places this could be used? */ public static Type getReturnType(ExpressionTree expressionTree) { if (expressionTree instanceof JCFieldAccess) { JCFieldAccess methodCall = (JCFieldAccess) expressionTree; return methodCall.type.getReturnType(); } else if (expressionTree instanceof JCIdent) { JCIdent methodCall = (JCIdent) expressionTree; return methodCall.type.getReturnType(); } else if (expressionTree instanceof JCMethodInvocation) { return getReturnType(((JCMethodInvocation) expressionTree).getMethodSelect()); } else if (expressionTree instanceof JCMemberReference) { return ((JCMemberReference) expressionTree).sym.type.getReturnType(); } throw new IllegalArgumentException("Expected a JCFieldAccess or JCIdent"); }
/** * Don't match the method that is invoked through {@code Mockito.verify(t)} or {@code * doReturn(val).when(t)}. */ private static boolean mockitoInvocation(Tree tree, VisitorState state) { if (!(tree instanceof JCMethodInvocation)) { return false; } JCMethodInvocation invocation = (JCMethodInvocation) tree; if (!(invocation.getMethodSelect() instanceof JCFieldAccess)) { return false; } ExpressionTree receiver = ASTHelpers.getReceiver(invocation); return MOCKITO_MATCHER.matches(receiver, state); } }
@Override public void visitApply(JCMethodInvocation tree) { JCExpression select = tree.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitCommentBefore")) { String commentLiteral = (String) ((JCLiteral) tree.getArguments().get(0)).getValue(); JCExpression expr = tree.getArguments().get(1); try { print("/* " + commentLiteral + " */ "); } catch (IOException e) { throw new RuntimeException(e); } expr.accept(this); } else { super.visitApply(tree); } }
@Override public void printStat(JCTree tree) throws IOException { if (tree instanceof JCExpressionStatement && ((JCExpressionStatement) tree).getExpression() instanceof JCMethodInvocation) { JCMethodInvocation invocation = (JCMethodInvocation) ((JCExpressionStatement) tree).getExpression(); JCExpression select = invocation.getMethodSelect(); if (select != null && select.toString().equals("Refaster.emitComment")) { String commentLiteral = (String) ((JCLiteral) invocation.getArguments().get(0)).getValue(); print("// " + commentLiteral); return; } } super.printStat(tree); }
public String getName() { return ((JCFieldAccess) node.getMethodSelect()).name.toString(); } }