Search in sources :

Example 41 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project error-prone by google.

the class ASTHelpers method hasAnnotation.

/**
   * Determines whether a symbol has an annotation of the given type. This includes annotations
   * inherited from superclasses due to {@code @Inherited}.
   *
   * @param annotationClass the binary class name of the annotation (e.g.
   *     "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
   */
public static boolean hasAnnotation(Symbol sym, String annotationClass, VisitorState state) {
    Name annotationName = state.getName(annotationClass);
    Symbol annotationSym;
    synchronized (state.context) {
        annotationSym = state.getSymtab().enterClass(annotationName);
    }
    try {
        annotationSym.complete();
    } catch (CompletionFailure e) {
    // @Inherited won't work if the annotation isn't on the classpath, but we can still check
    // if it's present directly
    }
    Symbol inheritedSym = state.getSymtab().inheritedType.tsym;
    if ((sym == null) || (annotationSym == null)) {
        return false;
    }
    if ((sym instanceof ClassSymbol) && (annotationSym.attribute(inheritedSym) != null)) {
        while (sym != null) {
            if (sym.attribute(annotationSym) != null) {
                return true;
            }
            sym = ((ClassSymbol) sym).getSuperclass().tsym;
        }
        return false;
    } else {
        return sym.attribute(annotationSym) != null;
    }
}
Also used : 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) CompletionFailure(com.sun.tools.javac.code.Symbol.CompletionFailure) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Name(com.sun.tools.javac.util.Name)

Example 42 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project lombok by rzwitserloot.

the class JavacHandlerUtil method fixMethodMirror.

private static void fixMethodMirror(Context context, Element typeMirror, long access, Name methodName, List<Type> paramTypes, Type returnType) {
    if (typeMirror == null || paramTypes == null || returnType == null)
        return;
    ClassSymbol cs = (ClassSymbol) typeMirror;
    MethodSymbol methodSymbol = new MethodSymbol(access, methodName, new MethodType(paramTypes, returnType, List.<Type>nil(), Symtab.instance(context).methodClass), cs);
    ClassSymbolMembersField.enter(cs, methodSymbol);
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) NullCheckExceptionType(lombok.core.configuration.NullCheckExceptionType) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Example 43 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project lombok by rzwitserloot.

the class HandleSetter method createSetterForField.

public void createSetterForField(AccessLevel level, JavacNode fieldNode, JavacNode sourceNode, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
    if (fieldNode.getKind() != Kind.FIELD) {
        fieldNode.addError("@Setter is only supported on a class or a field.");
        return;
    }
    JCVariableDecl fieldDecl = (JCVariableDecl) fieldNode.get();
    String methodName = toSetterName(fieldNode);
    if (methodName == null) {
        fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
        return;
    }
    if ((fieldDecl.mods.flags & Flags.FINAL) != 0) {
        fieldNode.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.");
        return;
    }
    for (String altName : toAllSetterNames(fieldNode)) {
        switch(methodExists(altName, fieldNode, false, 1)) {
            case EXISTS_BY_LOMBOK:
                return;
            case EXISTS_BY_USER:
                if (whineIfExists) {
                    String altNameExpl = "";
                    if (!altName.equals(methodName))
                        altNameExpl = String.format(" (%s)", altName);
                    fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
                }
                return;
            default:
            case NOT_EXISTS:
        }
    }
    long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
    JCMethodDecl createdSetter = createSetter(access, fieldNode, fieldNode.getTreeMaker(), sourceNode, onMethod, onParam);
    Type fieldType = getMirrorForFieldType(fieldNode);
    Type returnType;
    if (shouldReturnThis(fieldNode)) {
        ClassSymbol sym = ((JCClassDecl) fieldNode.up().get()).sym;
        returnType = sym == null ? null : sym.type;
    } else {
        returnType = Javac.createVoidType(fieldNode.getSymbolTable(), CTC_VOID);
    }
    injectMethod(fieldNode.up(), createdSetter, fieldType == null ? null : List.of(fieldType), returnType);
}
Also used : Type(com.sun.tools.javac.code.Type) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 44 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project lombok by rzwitserloot.

the class HandleConstructor method generateConstructor.

public void generateConstructor(JavacNode typeNode, AccessLevel level, List<JCAnnotation> onConstructor, List<JavacNode> fields, boolean allToDefault, String staticName, SkipIfConstructorExists skipIfConstructorExists, JavacNode source) {
    boolean staticConstrRequired = staticName != null && !staticName.equals("");
    if (skipIfConstructorExists != SkipIfConstructorExists.NO && constructorExists(typeNode) != MemberExistsResult.NOT_EXISTS)
        return;
    if (skipIfConstructorExists != SkipIfConstructorExists.NO) {
        for (JavacNode child : typeNode.down()) {
            if (child.getKind() == Kind.ANNOTATION) {
                boolean skipGeneration = annotationTypeMatches(NoArgsConstructor.class, child) || annotationTypeMatches(AllArgsConstructor.class, child) || annotationTypeMatches(RequiredArgsConstructor.class, child);
                if (!skipGeneration && skipIfConstructorExists == SkipIfConstructorExists.YES) {
                    skipGeneration = annotationTypeMatches(Builder.class, child);
                }
                if (skipGeneration) {
                    if (staticConstrRequired) {
                        // @Data has asked us to generate a constructor, but we're going to skip this instruction, as an explicit 'make a constructor' annotation
                        // will take care of it. However, @Data also wants a specific static name; this will be ignored; the appropriate way to do this is to use
                        // the 'staticName' parameter of the @XArgsConstructor you've stuck on your type.
                        // We should warn that we're ignoring @Data's 'staticConstructor' param.
                        source.addWarning("Ignoring static constructor name: explicit @XxxArgsConstructor annotation present; its `staticName` parameter will be used.");
                    }
                    return;
                }
            }
        }
    }
    JCMethodDecl constr = createConstructor(staticConstrRequired ? AccessLevel.PRIVATE : level, onConstructor, typeNode, fields, allToDefault, source);
    ListBuffer<Type> argTypes = new ListBuffer<Type>();
    for (JavacNode fieldNode : fields) {
        Type mirror = getMirrorForFieldType(fieldNode);
        if (mirror == null) {
            argTypes = null;
            break;
        }
        argTypes.append(mirror);
    }
    List<Type> argTypes_ = argTypes == null ? null : argTypes.toList();
    injectMethod(typeNode, constr, argTypes_, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID));
    if (staticConstrRequired) {
        ClassSymbol sym = ((JCClassDecl) typeNode.get()).sym;
        Type returnType = sym == null ? null : sym.type;
        JCMethodDecl staticConstr = createStaticConstructor(staticName, level, typeNode, allToDefault ? List.<JavacNode>nil() : fields, source.get());
        injectMethod(typeNode, staticConstr, argTypes_, returnType);
    }
}
Also used : Type(com.sun.tools.javac.code.Type) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Builder(lombok.Builder) ListBuffer(com.sun.tools.javac.util.ListBuffer) RequiredArgsConstructor(lombok.RequiredArgsConstructor)

Example 45 with ClassSymbol

use of com.sun.tools.javac.code.Symbol.ClassSymbol in project ceylon-compiler by ceylon.

the class Attr method attribBounds.

void attribBounds(List<JCTypeParameter> typarams) {
    for (JCTypeParameter typaram : typarams) {
        Type bound = typaram.type.getUpperBound();
        if (bound != null && bound.tsym instanceof ClassSymbol) {
            ClassSymbol c = (ClassSymbol) bound.tsym;
            if ((c.flags_field & COMPOUND) != 0) {
                Assert.check((c.flags_field & UNATTRIBUTED) != 0, c);
                attribClass(typaram.pos(), c);
            }
        }
    }
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

Aggregations

ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)49 Symbol (com.sun.tools.javac.code.Symbol)20 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)20 Type (com.sun.tools.javac.code.Type)17 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)9 ClassType (com.sun.tools.javac.code.Type.ClassType)9 ClassTree (com.sun.source.tree.ClassTree)8 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)8 MethodTree (com.sun.source.tree.MethodTree)6 Tree (com.sun.source.tree.Tree)6 ArrayType (com.sun.tools.javac.code.Type.ArrayType)5 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)4 VariableTree (com.sun.source.tree.VariableTree)4 Name (com.sun.tools.javac.util.Name)4 ArrayList (java.util.ArrayList)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ImmutableAnalysis.Violation)3 BlockTree (com.sun.source.tree.BlockTree)3 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)3 ForLoopTree (com.sun.source.tree.ForLoopTree)3