Search in sources :

Example 1 with TreeMaker

use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.

the class UUnary method inline.

@Override
public JCExpression inline(Inliner inliner) throws CouldNotResolveImportException {
    JCExpression expr = getExpression().inline(inliner);
    final TreeMaker maker = inliner.maker();
    if (getKind() == Kind.LOGICAL_COMPLEMENT) {
        return new TreeCopier<Void>(maker) {

            @SuppressWarnings("unchecked")
            // essentially depends on T being a superclass of JCExpression
            @Override
            public <T extends JCTree> T copy(T t, Void v) {
                if (t instanceof BinaryTree || t instanceof UnaryTree || t instanceof ConditionalExpressionTree) {
                    return super.copy(t, v);
                } else {
                    return (T) defaultNegation(t);
                }
            }

            public JCExpression defaultNegation(Tree expr) {
                return maker.Unary(JCTree.Tag.NOT, (JCExpression) expr);
            }

            @Override
            public JCExpression visitBinary(BinaryTree tree, Void v) {
                if (UBinary.DEMORGAN.containsKey(tree.getKind())) {
                    JCExpression negLeft = copy((JCExpression) tree.getLeftOperand());
                    JCExpression negRight = copy((JCExpression) tree.getRightOperand());
                    return maker.Binary(UBinary.OP_CODES.get(UBinary.DEMORGAN.get(tree.getKind())), negLeft, negRight);
                } else if (UBinary.NEGATION.containsKey(tree.getKind())) {
                    JCExpression left = (JCExpression) tree.getLeftOperand();
                    JCExpression right = (JCExpression) tree.getRightOperand();
                    return maker.Binary(UBinary.OP_CODES.get(UBinary.NEGATION.get(tree.getKind())), left, right);
                } else {
                    return defaultNegation(tree);
                }
            }

            @Override
            public JCExpression visitUnary(UnaryTree tree, Void v) {
                if (tree.getKind() == Kind.LOGICAL_COMPLEMENT) {
                    return (JCExpression) tree.getExpression();
                } else {
                    return defaultNegation(tree);
                }
            }

            @Override
            public JCConditional visitConditionalExpression(ConditionalExpressionTree tree, Void v) {
                return maker.Conditional((JCExpression) tree.getCondition(), copy((JCExpression) tree.getTrueExpression()), copy((JCExpression) tree.getFalseExpression()));
            }
        }.copy(expr);
    } else {
        return inliner.maker().Unary(UNARY_OP_CODES.get(getKind()), getExpression().inline(inliner));
    }
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) BinaryTree(com.sun.source.tree.BinaryTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) BinaryTree(com.sun.source.tree.BinaryTree) UnaryTree(com.sun.source.tree.UnaryTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.sun.source.tree.Tree) UnaryTree(com.sun.source.tree.UnaryTree)

Example 2 with TreeMaker

use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.

the class UVariableDecl method inline.

private JCVariableDecl inline(@Nullable UExpression type, Inliner inliner) throws CouldNotResolveImportException {
    Optional<LocalVarBinding> binding = inliner.getOptionalBinding(key());
    JCModifiers modifiers;
    Name name;
    TreeMaker maker = inliner.maker();
    if (binding.isPresent()) {
        modifiers = (JCModifiers) binding.get().getModifiers();
        name = binding.get().getName();
    } else {
        modifiers = maker.Modifiers(0L);
        name = getName().inline(inliner);
    }
    return maker.VarDef(modifiers, name, (type == null) ? null : type.inline(inliner), (getInitializer() == null) ? null : getInitializer().inline(inliner));
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) Name(com.sun.tools.javac.util.Name)

Example 3 with TreeMaker

use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.

the class TypeParameterQualifier method matchMemberSelect.

@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
    Symbol baseSym = ASTHelpers.getSymbol(tree.getExpression());
    if (baseSym == null || baseSym.getKind() != ElementKind.TYPE_PARAMETER) {
        return Description.NO_MATCH;
    }
    TreeMaker make = TreeMaker.instance(state.context).forToplevel((JCCompilationUnit) state.getPath().getCompilationUnit());
    JCExpression qual = make.QualIdent(ASTHelpers.getSymbol(tree));
    return describeMatch(tree, SuggestedFix.replace(tree, qual.toString()));
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Symbol(com.sun.tools.javac.code.Symbol)

Example 4 with TreeMaker

use of com.sun.tools.javac.tree.TreeMaker in project error-prone by google.

the class GuardedBySymbolResolver method attribIdent.

private Symbol attribIdent(String name) {
    Attr attr = Attr.instance(context);
    TreeMaker tm = TreeMaker.instance(context);
    return attr.attribIdent(tm.Ident(getName(name)), compilationUnit);
}
Also used : TreeMaker(com.sun.tools.javac.tree.TreeMaker) Attr(com.sun.tools.javac.comp.Attr)

Example 5 with TreeMaker

use of com.sun.tools.javac.tree.TreeMaker in project checker-framework by typetools.

the class DefaultReflectionResolver method resolveReflectiveMethod.

/**
 * Resolves a reflective method call and returns all possible corresponding method calls.
 *
 * @param tree the MethodInvocationTree node that is to be resolved (Method.invoke)
 * @return a (potentially empty) list of all resolved MethodInvocationTrees
 */
private List<MethodInvocationTree> resolveReflectiveMethod(MethodInvocationTree tree, AnnotatedTypeFactory reflectionFactory) {
    assert isReflectiveMethodInvocation(tree);
    JCMethodInvocation methodInvocation = (JCMethodInvocation) tree;
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    TreeMaker make = TreeMaker.instance(context);
    TreePath path = reflectionFactory.getPath(tree);
    JavacScope scope = (JavacScope) trees.getScope(path);
    Env<AttrContext> env = scope.getEnv();
    List<MethodInvocationTree> methods = new ArrayList<>();
    boolean unknown = isUnknownMethod(tree);
    AnnotationMirror estimate = getMethodVal(tree);
    if (estimate == null) {
        debugReflection("MethodVal is unknown for: " + tree);
        debugReflection("UnknownMethod annotation: " + unknown);
        return methods;
    }
    debugReflection("MethodVal type system annotations: " + estimate);
    List<String> listClassNames = AnnotationUtils.getElementValueArray(estimate, "className", String.class, true);
    List<String> listMethodNames = AnnotationUtils.getElementValueArray(estimate, "methodName", String.class, true);
    List<Integer> listParamLenghts = AnnotationUtils.getElementValueArray(estimate, "params", Integer.class, true);
    assert listClassNames.size() == listMethodNames.size() && listClassNames.size() == listParamLenghts.size();
    for (int i = 0; i < listClassNames.size(); ++i) {
        String className = listClassNames.get(i);
        String methodName = listMethodNames.get(i);
        int paramLength = listParamLenghts.get(i);
        // Get receiver, which is always the first argument of the invoke
        // method
        JCExpression receiver = methodInvocation.args.head;
        // The remaining list contains the arguments
        com.sun.tools.javac.util.List<JCExpression> args = methodInvocation.args.tail;
        // Resolve the Symbol(s) for the current method
        for (Symbol symbol : getMethodSymbolsfor(className, methodName, paramLength, env)) {
            if ((symbol.flags() & Flags.PUBLIC) > 0) {
                debugReflection("Resolved public method: " + symbol.owner + "." + symbol);
            } else {
                debugReflection("Resolved non-public method: " + symbol.owner + "." + symbol);
            }
            JCExpression method = make.Select(receiver, symbol);
            args = getCorrectedArgs(symbol, args);
            // Build method invocation tree depending on the number of
            // parameters
            JCMethodInvocation syntTree = paramLength > 0 ? make.App(method, args) : make.App(method);
            // add method invocation tree to the list of possible methods
            methods.add(syntTree);
        }
    }
    return methods;
}
Also used : Context(com.sun.tools.javac.util.Context) AttrContext(com.sun.tools.javac.comp.AttrContext) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ArrayList(java.util.ArrayList) JavacScope(com.sun.tools.javac.api.JavacScope) AttrContext(com.sun.tools.javac.comp.AttrContext) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TreeMaker(com.sun.tools.javac.tree.TreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TreePath(com.sun.source.util.TreePath) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Aggregations

TreeMaker (com.sun.tools.javac.tree.TreeMaker)7 Symbol (com.sun.tools.javac.code.Symbol)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 TreePath (com.sun.source.util.TreePath)2 JavacScope (com.sun.tools.javac.api.JavacScope)2 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)2 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)2 AttrContext (com.sun.tools.javac.comp.AttrContext)2 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)2 JCTree (com.sun.tools.javac.tree.JCTree)2 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)2 Context (com.sun.tools.javac.util.Context)2 ArrayList (java.util.ArrayList)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 BinaryTree (com.sun.source.tree.BinaryTree)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 Tree (com.sun.source.tree.Tree)1 UnaryTree (com.sun.source.tree.UnaryTree)1 Attr (com.sun.tools.javac.comp.Attr)1