Search in sources :

Example 21 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacHandlerUtil method injectField.

private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addGenerated) {
    JCClassDecl type = (JCClassDecl) typeNode.get();
    if (addGenerated) {
        addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
        addGenerated(field.mods, typeNode, field.pos, getGeneratedBy(field), typeNode.getContext());
    }
    List<JCTree> insertAfter = null;
    List<JCTree> insertBefore = type.defs;
    while (true) {
        boolean skip = false;
        if (insertBefore.head instanceof JCVariableDecl) {
            JCVariableDecl f = (JCVariableDecl) insertBefore.head;
            if (isEnumConstant(f) || isGenerated(f))
                skip = true;
        } else if (insertBefore.head instanceof JCMethodDecl) {
            if ((((JCMethodDecl) insertBefore.head).mods.flags & GENERATEDCONSTR) != 0)
                skip = true;
        }
        if (skip) {
            insertAfter = insertBefore;
            insertBefore = insertBefore.tail;
            continue;
        }
        break;
    }
    List<JCTree> fieldEntry = List.<JCTree>of(field);
    fieldEntry.tail = insertBefore;
    if (insertAfter == null) {
        type.defs = fieldEntry;
    } else {
        insertAfter.tail = fieldEntry;
    }
    return typeNode.add(field, Kind.FIELD);
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 22 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project lombok by rzwitserloot.

the class JavacHandlerUtil method findGetter.

private static GetterMethod findGetter(JavacNode field) {
    JCVariableDecl decl = (JCVariableDecl) field.get();
    JavacNode typeNode = field.up();
    for (String potentialGetterName : toAllGetterNames(field)) {
        for (JavacNode potentialGetter : typeNode.down()) {
            if (potentialGetter.getKind() != Kind.METHOD)
                continue;
            JCMethodDecl method = (JCMethodDecl) potentialGetter.get();
            if (!method.name.toString().equalsIgnoreCase(potentialGetterName))
                continue;
            /** static getX() methods don't count. */
            if ((method.mods.flags & Flags.STATIC) != 0)
                continue;
            /** Nor do getters with a non-empty parameter list. */
            if (method.params != null && method.params.size() > 0)
                continue;
            return new GetterMethod(method.name, method.restype);
        }
    }
    // Check if the field has a @Getter annotation.
    boolean hasGetterAnnotation = false;
    for (JavacNode child : field.down()) {
        if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
            AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
            //Definitely WONT have a getter.
            if (ann.getInstance().value() == AccessLevel.NONE)
                return null;
            hasGetterAnnotation = true;
        }
    }
    if (!hasGetterAnnotation && new HandleGetter().fieldQualifiesForGetterGeneration(field)) {
        //Check if the class has @Getter or @Data annotation.
        JavacNode containingType = field.up();
        if (containingType != null)
            for (JavacNode child : containingType.down()) {
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Data.class, child))
                    hasGetterAnnotation = true;
                if (child.getKind() == Kind.ANNOTATION && annotationTypeMatches(Getter.class, child)) {
                    AnnotationValues<Getter> ann = createAnnotation(Getter.class, child);
                    //Definitely WONT have a getter.
                    if (ann.getInstance().value() == AccessLevel.NONE)
                        return null;
                    hasGetterAnnotation = true;
                }
            }
    }
    if (hasGetterAnnotation) {
        String getterName = toGetterName(field);
        if (getterName == null)
            return null;
        return new GetterMethod(field.toName(getterName), decl.vartype);
    }
    return null;
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) Getter(lombok.Getter) AnnotationValues(lombok.core.AnnotationValues) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 23 with JCMethodDecl

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

the class FindIdentifiers method findIdent.

/** Finds a declaration with the given name that is in scope at the current location. */
public static Symbol findIdent(String name, VisitorState state) {
    ClassType enclosingClass = ASTHelpers.getType(state.findEnclosing(ClassTree.class));
    if (enclosingClass == null || enclosingClass.tsym == null) {
        return null;
    }
    Env<AttrContext> env = Enter.instance(state.context).getClassEnv(enclosingClass.tsym);
    MethodTree enclosingMethod = state.findEnclosing(MethodTree.class);
    if (enclosingMethod != null) {
        env = MemberEnter.instance(state.context).getMethodEnv((JCMethodDecl) enclosingMethod, env);
    }
    try {
        Method method = Resolve.class.getDeclaredMethod("findIdent", Env.class, Name.class, KindSelector.class);
        method.setAccessible(true);
        Symbol result = (Symbol) method.invoke(Resolve.instance(state.context), env, state.getName(name), KindSelector.VAR);
        return result.exists() ? result : null;
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
}
Also used : JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) MethodTree(com.sun.source.tree.MethodTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) Method(java.lang.reflect.Method) ClassType(com.sun.tools.javac.code.Type.ClassType) AttrContext(com.sun.tools.javac.comp.AttrContext)

Example 24 with JCMethodDecl

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

the class CompilerBasedTest method compile.

protected void compile(JavaFileObject fileObject) {
    final ImmutableMap.Builder<String, JCMethodDecl> methodsBuilder = ImmutableMap.builder();
    final ImmutableList.Builder<JCCompilationUnit> compilationUnitsBuilder = ImmutableList.builder();
    compile(new TreeScanner() {

        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            if (!TreeInfo.isConstructor(tree)) {
                methodsBuilder.put(tree.getName().toString(), tree);
            }
        }

        @Override
        public void visitTopLevel(JCCompilationUnit tree) {
            compilationUnitsBuilder.add(tree);
            super.visitTopLevel(tree);
        }
    }, fileObject);
    this.methods = methodsBuilder.build();
    this.compilationUnits = compilationUnitsBuilder.build();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) TreeScanner(com.sun.tools.javac.tree.TreeScanner) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 25 with JCMethodDecl

use of com.sun.tools.javac.tree.JCTree.JCMethodDecl in project ceylon-compiler by ceylon.

the class CeylonTransformer method makeDefs.

private List<JCTree> makeDefs(CompilationUnit t) {
    final ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
    t.visit(new SourceDeclarationVisitor() {

        @Override
        public void loadFromSource(Declaration decl) {
            if (!checkNative(decl))
                return;
            long flags = decl instanceof Tree.AnyInterface ? Flags.INTERFACE : 0;
            String name = Naming.toplevelClassName("", decl);
            defs.add(makeClassDef(decl, flags, name, WantedDeclaration.Normal));
            if (decl instanceof Tree.AnyInterface) {
                String implName = Naming.getImplClassName(name);
                defs.add(makeClassDef(decl, 0, implName, WantedDeclaration.Normal));
            }
            // only do it for Bootstrap where we control the annotations, because it's so dodgy ATM
            if (options.get(OptionName.BOOTSTRAPCEYLON) != null && decl instanceof Tree.AnyClass && TreeUtil.hasAnnotation(decl.getAnnotationList(), "annotation", decl.getUnit())) {
                String annotationName = Naming.suffixName(Suffix.$annotation$, name);
                defs.add(makeClassDef(decl, Flags.ANNOTATION, annotationName, WantedDeclaration.Annotation));
                for (Tree.StaticType sat : ((Tree.AnyClass) decl).getSatisfiedTypes().getTypes()) {
                    if (sat instanceof Tree.BaseType && ((Tree.BaseType) sat).getIdentifier().getText().equals("SequencedAnnotation")) {
                        String annotationsName = Naming.suffixName(Suffix.$annotations$, name);
                        defs.add(makeClassDef(decl, Flags.ANNOTATION, annotationsName, WantedDeclaration.AnnotationSequence));
                    }
                }
            }
        }

        private JCTree makeClassDef(Declaration decl, long flags, String name, WantedDeclaration wantedDeclaration) {
            ListBuffer<JCTree.JCTypeParameter> typarams = new ListBuffer<JCTree.JCTypeParameter>();
            if (decl instanceof Tree.ClassOrInterface) {
                Tree.ClassOrInterface classDecl = (ClassOrInterface) decl;
                if (classDecl.getTypeParameterList() != null) {
                    for (Tree.TypeParameterDeclaration typeParamDecl : classDecl.getTypeParameterList().getTypeParameterDeclarations()) {
                        // we don't need a valid name, just a name, and making it BOGUS helps us find it later if it turns out
                        // we failed to reset everything properly
                        typarams.add(make().TypeParameter(names().fromString("BOGUS-" + typeParamDecl.getIdentifier().getText()), List.<JCExpression>nil()));
                    }
                }
            }
            return make().ClassDef(make().Modifiers(flags | Flags.PUBLIC), names().fromString(name), typarams.toList(), null, List.<JCExpression>nil(), makeClassBody(decl, wantedDeclaration));
        }

        private List<JCTree> makeClassBody(Declaration decl, WantedDeclaration wantedDeclaration) {
            // only do it for Bootstrap where we control the annotations, because it's so dodgy ATM
            if (wantedDeclaration == WantedDeclaration.Annotation) {
                ListBuffer<JCTree> body = new ListBuffer<JCTree>();
                for (Tree.Parameter param : ((Tree.ClassDefinition) decl).getParameterList().getParameters()) {
                    String name;
                    JCExpression type = make().TypeArray(make().Type(syms().stringType));
                    if (param instanceof Tree.InitializerParameter)
                        name = ((Tree.InitializerParameter) param).getIdentifier().getText();
                    else if (param instanceof Tree.ParameterDeclaration) {
                        Tree.TypedDeclaration typedDeclaration = ((Tree.ParameterDeclaration) param).getTypedDeclaration();
                        name = typedDeclaration.getIdentifier().getText();
                        type = getAnnotationTypeFor(typedDeclaration.getType());
                    } else
                        name = "ERROR";
                    JCMethodDecl method = make().MethodDef(make().Modifiers(Flags.PUBLIC), names().fromString(name), type, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
                    body.append(method);
                }
                return body.toList();
            }
            if (wantedDeclaration == WantedDeclaration.AnnotationSequence) {
                String name = Naming.toplevelClassName("", decl);
                String annotationName = Naming.suffixName(Suffix.$annotation$, name);
                JCExpression type = make().TypeArray(make().Ident(names().fromString(annotationName)));
                JCMethodDecl method = make().MethodDef(make().Modifiers(Flags.PUBLIC), names().fromString("value"), type, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null);
                return List.<JCTree>of(method);
            }
            return List.<JCTree>nil();
        }

        private JCExpression getAnnotationTypeFor(Tree.Type type) {
            if (type instanceof Tree.BaseType) {
                String name = ((Tree.BaseType) type).getIdentifier().getText();
                if (name.equals("String") || name.equals("Declaration"))
                    return make().Type(syms().stringType);
                if (name.equals("Boolean"))
                    return make().Type(syms().booleanType);
                if (name.equals("Integer"))
                    return make().Type(syms().longType);
                if (name.equals("Float"))
                    return make().Type(syms().doubleType);
                if (name.equals("Byte"))
                    return make().Type(syms().byteType);
                if (name.equals("Character"))
                    return make().Type(syms().charType);
                if (name.equals("Declaration") || name.equals("ClassDeclaration") || name.equals("InterfaceDeclaration") || name.equals("ClassOrInterfaceDeclaration"))
                    return make().Type(syms().stringType);
            }
            if (type instanceof Tree.SequencedType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.SequencedType) type).getType()));
            }
            if (type instanceof Tree.SequenceType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.SequenceType) type).getElementType()));
            }
            if (type instanceof Tree.IterableType) {
                return make().TypeArray(getAnnotationTypeFor(((Tree.IterableType) type).getElementType()));
            }
            if (type instanceof Tree.TupleType) {
                // can only be one, must be a SequencedType
                Tree.Type sequencedType = ((Tree.TupleType) type).getElementTypes().get(0);
                return getAnnotationTypeFor(sequencedType);
            }
            System.err.println("Unknown Annotation type: " + type);
            return make().TypeArray(make().Type(syms().stringType));
        }

        @Override
        public void loadFromSource(ModuleDescriptor that) {
        // don't think we care about these
        }

        @Override
        public void loadFromSource(PackageDescriptor that) {
        // don't think we care about these
        }
    });
    return defs.toList();
}
Also used : ClassOrInterface(com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassOrInterface) ListBuffer(com.sun.tools.javac.util.ListBuffer) PackageDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.PackageDescriptor) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SourceDeclarationVisitor(com.redhat.ceylon.compiler.java.loader.SourceDeclarationVisitor) List(com.sun.tools.javac.util.List) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.Declaration) ClassOrInterface(com.redhat.ceylon.compiler.typechecker.tree.Tree.ClassOrInterface) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ModuleDescriptor(com.redhat.ceylon.compiler.typechecker.tree.Tree.ModuleDescriptor) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter)

Aggregations

JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)45 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)31 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)20 Name (com.sun.tools.javac.util.Name)17 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)16 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)16 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)15 ListBuffer (com.sun.tools.javac.util.ListBuffer)14 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)13 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)13 JavacNode (lombok.javac.JavacNode)13 JCTree (com.sun.tools.javac.tree.JCTree)12 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)5 Type (com.sun.tools.javac.code.Type)5 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)5 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)5 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)4 JavacTreeMaker (lombok.javac.JavacTreeMaker)4 JCExpressionStatement (com.sun.tools.javac.tree.JCTree.JCExpressionStatement)3