Search in sources :

Example 1 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project lombok by rzwitserloot.

the class HandleDelegate method handle.

@Override
public void handle(AnnotationValues<Delegate> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.DELEGATE_FLAG_USAGE, "@Delegate");
    @SuppressWarnings("deprecation") Class<? extends Annotation> oldDelegate = lombok.Delegate.class;
    deleteAnnotationIfNeccessary(annotationNode, Delegate.class, oldDelegate);
    Type delegateType;
    Name delegateName = annotationNode.toName(annotationNode.up().getName());
    DelegateReceiver delegateReceiver;
    JavacResolution reso = new JavacResolution(annotationNode.getContext());
    JCTree member = annotationNode.up().get();
    if (annotationNode.up().getKind() == Kind.FIELD) {
        if ((((JCVariableDecl) member).mods.flags & Flags.STATIC) != 0) {
            annotationNode.addError(LEGALITY_OF_DELEGATE);
            return;
        }
        delegateReceiver = DelegateReceiver.FIELD;
        delegateType = member.type;
        if (delegateType == null)
            reso.resolveClassMember(annotationNode.up());
        delegateType = member.type;
    } else if (annotationNode.up().getKind() == Kind.METHOD) {
        if (!(member instanceof JCMethodDecl)) {
            annotationNode.addError(LEGALITY_OF_DELEGATE);
            return;
        }
        JCMethodDecl methodDecl = (JCMethodDecl) member;
        if (!methodDecl.params.isEmpty() || (methodDecl.mods.flags & Flags.STATIC) != 0) {
            annotationNode.addError(LEGALITY_OF_DELEGATE);
            return;
        }
        delegateReceiver = DelegateReceiver.METHOD;
        delegateType = methodDecl.restype.type;
        if (delegateType == null)
            reso.resolveClassMember(annotationNode.up());
        delegateType = methodDecl.restype.type;
    } else {
        // As the annotation is legal on fields and methods only, javac itself will take care of printing an error message for this.
        return;
    }
    List<Object> delegateTypes = annotation.getActualExpressions("types");
    List<Object> excludeTypes = annotation.getActualExpressions("excludes");
    List<Type> toDelegate = new ArrayList<Type>();
    List<Type> toExclude = new ArrayList<Type>();
    if (delegateTypes.isEmpty()) {
        if (delegateType != null)
            toDelegate.add(delegateType);
    } else {
        for (Object dt : delegateTypes) {
            if (dt instanceof JCFieldAccess && ((JCFieldAccess) dt).name.toString().equals("class")) {
                Type type = ((JCFieldAccess) dt).selected.type;
                if (type == null)
                    reso.resolveClassMember(annotationNode);
                type = ((JCFieldAccess) dt).selected.type;
                if (type != null)
                    toDelegate.add(type);
            }
        }
    }
    for (Object et : excludeTypes) {
        if (et instanceof JCFieldAccess && ((JCFieldAccess) et).name.toString().equals("class")) {
            Type type = ((JCFieldAccess) et).selected.type;
            if (type == null)
                reso.resolveClassMember(annotationNode);
            type = ((JCFieldAccess) et).selected.type;
            if (type != null)
                toExclude.add(type);
        }
    }
    List<MethodSig> signaturesToDelegate = new ArrayList<MethodSig>();
    List<MethodSig> signaturesToExclude = new ArrayList<MethodSig>();
    Set<String> banList = new HashSet<String>();
    banList.addAll(METHODS_IN_OBJECT);
    try {
        for (Type t : toExclude) {
            if (t instanceof ClassType) {
                ClassType ct = (ClassType) t;
                addMethodBindings(signaturesToExclude, ct, annotationNode.getTypesUtil(), banList);
            } else {
                annotationNode.addError("@Delegate can only use concrete class types, not wildcards, arrays, type variables, or primitives.");
                return;
            }
        }
        for (MethodSig sig : signaturesToExclude) {
            banList.add(printSig(sig.type, sig.name, annotationNode.getTypesUtil()));
        }
        for (Type t : toDelegate) {
            if (t instanceof ClassType) {
                ClassType ct = (ClassType) t;
                addMethodBindings(signaturesToDelegate, ct, annotationNode.getTypesUtil(), banList);
            } else {
                annotationNode.addError("@Delegate can only use concrete class types, not wildcards, arrays, type variables, or primitives.");
                return;
            }
        }
        for (MethodSig sig : signaturesToDelegate) generateAndAdd(sig, annotationNode, delegateName, delegateReceiver);
    } catch (DelegateRecursion e) {
        annotationNode.addError(String.format(RECURSION_NOT_ALLOWED, e.member, e.type));
    }
}
Also used : JavacResolution(lombok.javac.JavacResolution) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) ClassType(com.sun.tools.javac.code.Type.ClassType) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Name(com.sun.tools.javac.util.Name) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ExecutableType(javax.lang.model.type.ExecutableType) Delegate(lombok.experimental.Delegate) HashSet(java.util.HashSet)

Example 2 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class UTypeVarIdentTest method inline.

@Test
public void inline() {
    ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL);
    Symtab symtab = Symtab.instance(context);
    Type listType = symtab.listType;
    bind(new UTypeVar.Key("E"), TypeWithExpression.create(new ClassType(listType, List.<Type>of(symtab.stringType), listType.tsym)));
    assertInlines("List<String>", UTypeVarIdent.create("E"));
    assertEquals(ImmutableSet.of("java.util.List"), inliner.getImportsToAdd());
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ClassType(com.sun.tools.javac.code.Type.ClassType) Test(org.junit.Test)

Example 3 with ClassType

use of com.sun.tools.javac.code.Type.ClassType 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 4 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class UClassType method inline.

@Override
public ClassType inline(Inliner inliner) throws CouldNotResolveImportException {
    ClassSymbol classSymbol = inliner.resolveClass(fullyQualifiedClass());
    boolean isNonStaticInnerClass = classSymbol.owner instanceof ClassSymbol && (classSymbol.flags() & STATIC) == 0;
    Type owner = isNonStaticInnerClass ? classSymbol.owner.type : Type.noType;
    return new ClassType(owner, inliner.<Type>inlineList(typeArguments()), classSymbol);
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 5 with ClassType

use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.

the class MutableMethodReturnType method getCommonImmutableTypeForAllReturnStatementsTypes.

private static Optional<String> getCommonImmutableTypeForAllReturnStatementsTypes(ImmutableSet<ClassType> returnStatementsTypes) {
    checkState(!returnStatementsTypes.isEmpty());
    ClassType arbitraryClassType = returnStatementsTypes.asList().get(0);
    ImmutableList<String> superTypes = getImmutableSuperTypesForClassType(arbitraryClassType);
    return superTypes.stream().filter(areAllReturnStatementsAssignable(returnStatementsTypes)).findFirst();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType)

Aggregations

ClassType (com.sun.tools.javac.code.Type.ClassType)34 Type (com.sun.tools.javac.code.Type)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)10 ArrayType (com.sun.tools.javac.code.Type.ArrayType)10 Symbol (com.sun.tools.javac.code.Symbol)9 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)9 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)9 WildcardType (com.sun.tools.javac.code.Type.WildcardType)9 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)8 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)7 ArrayList (java.util.ArrayList)5 ClassTree (com.sun.source.tree.ClassTree)4 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)3 MethodTree (com.sun.source.tree.MethodTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)3 AttrContext (com.sun.tools.javac.comp.AttrContext)3