Search in sources :

Example 51 with Type

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

the class Template method infer.

/**
   * Returns the inferred method type of the template based on the given actual argument types.
   *
   * @throws InferException if no instances of the specified type variables would allow the
   *         {@code actualArgTypes} to match the {@code expectedArgTypes}
   */
private Type infer(Warner warner, Inliner inliner, List<Type> freeTypeVariables, List<Type> expectedArgTypes, Type returnType, List<Type> actualArgTypes) throws InferException {
    Symtab symtab = inliner.symtab();
    Type methodType = new MethodType(expectedArgTypes, returnType, List.<Type>nil(), symtab.methodClass);
    if (!freeTypeVariables.isEmpty()) {
        methodType = new ForAll(freeTypeVariables, methodType);
    }
    Enter enter = inliner.enter();
    MethodSymbol methodSymbol = new MethodSymbol(0, inliner.asName("__m__"), methodType, symtab.unknownSymbol);
    Type site = symtab.methodClass.type;
    Env<AttrContext> env = enter.getTopLevelEnv(TreeMaker.instance(inliner.getContext()).TopLevel(List.<JCTree>nil()));
    // Set up the resolution phase:
    try {
        Field field = AttrContext.class.getDeclaredField("pendingResolutionPhase");
        field.setAccessible(true);
        field.set(env.info, newMethodResolutionPhase(autoboxing()));
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
    Object resultInfo;
    try {
        Class<?> resultInfoClass = Class.forName("com.sun.tools.javac.comp.Attr$ResultInfo");
        Constructor<?> resultInfoCtor = resultInfoClass.getDeclaredConstructor(Attr.class, KindSelector.class, Type.class);
        resultInfoCtor.setAccessible(true);
        resultInfo = resultInfoCtor.newInstance(Attr.instance(inliner.getContext()), KindSelector.PCK, Type.noType);
    } catch (ReflectiveOperationException e) {
        throw new LinkageError(e.getMessage(), e);
    }
    // Type inference sometimes produces diagnostics, so we need to catch them to avoid interfering
    // with the enclosing compilation.
    Log.DeferredDiagnosticHandler handler = new Log.DeferredDiagnosticHandler(Log.instance(inliner.getContext()));
    try {
        MethodType result = callCheckMethod(warner, inliner, resultInfo, actualArgTypes, methodSymbol, site, env);
        if (!handler.getDiagnostics().isEmpty()) {
            throw new InferException(handler.getDiagnostics());
        }
        return result;
    } finally {
        Log.instance(inliner.getContext()).popDiagnosticHandler(handler);
    }
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) Log(com.sun.tools.javac.util.Log) JCTree(com.sun.tools.javac.tree.JCTree) AttrContext(com.sun.tools.javac.comp.AttrContext) Symtab(com.sun.tools.javac.code.Symtab) Field(java.lang.reflect.Field) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Enter(com.sun.tools.javac.comp.Enter) ForAll(com.sun.tools.javac.code.Type.ForAll)

Example 52 with Type

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

use of com.sun.tools.javac.code.Type in project RxCache by VictorAlbertos.

the class GetProvidersClass method getMethods.

private List<ProvidersClass.Method> getMethods(Element classElement) throws ValidationException {
    List<? extends Element> enclosedElements = classElement.getEnclosedElements();
    List<ProvidersClass.Method> methods = new ArrayList<>();
    for (Element methodElement : enclosedElements) {
        if (!isAnnotatedWithActionable(methodElement))
            continue;
        if (methodElement.getKind() != ElementKind.METHOD)
            continue;
        Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) methodElement;
        String nameMethod = methodSymbol.getSimpleName().toString();
        Type returnType = methodSymbol.getReturnType();
        if (!returnType.tsym.toString().equals(TypeName.get(Observable.class).toString())) {
            throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
        }
        Type enclosingTypeObservable = returnType.getTypeArguments().get(0);
        if (!enclosingTypeObservable.tsym.toString().equals(TypeName.get(List.class).toString())) {
            throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
        }
        List<Symbol.VarSymbol> params = methodSymbol.getParameters();
        boolean hasEvictProvider = hasEvictProvider(params);
        boolean hasEvictDynamicKey = hasEvictDynamicKey(params);
        boolean hasEvictDynamicKeyGroup = hasEvictDynamicKeyGroup(params);
        if (!hasEvictProvider && !hasEvictDynamicKey && !hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictProvider && hasEvictDynamicKey) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictProvider && hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictDynamicKey && hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        boolean hasDynamicKey = hasDynamicKey(params);
        boolean hasDynamicKeyGroup = hasDynamicKeyGroup(params);
        methods.add(new ProvidersClass.Method(nameMethod, methodElement, enclosingTypeObservable, hasDynamicKey, hasDynamicKeyGroup));
    }
    return methods;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Type(com.sun.tools.javac.code.Type) List(java.util.List) ArrayList(java.util.ArrayList)

Example 54 with Type

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

the class FindTypeVarScanner method visitTypeVariable.

@Override
public Void visitTypeVariable(TypeVariable t, Void p) {
    Name name = null;
    try {
        name = ((Type) t).tsym.name;
    } catch (NullPointerException e) {
    }
    if (name != null)
        typeVariables.add(name.toString());
    subVisit(t.getLowerBound());
    subVisit(t.getUpperBound());
    return null;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) ErrorType(javax.lang.model.type.ErrorType) ExecutableType(javax.lang.model.type.ExecutableType) NoType(javax.lang.model.type.NoType) NullType(javax.lang.model.type.NullType) DeclaredType(javax.lang.model.type.DeclaredType) PrimitiveType(javax.lang.model.type.PrimitiveType) WildcardType(javax.lang.model.type.WildcardType) Type(com.sun.tools.javac.code.Type) Name(javax.lang.model.element.Name)

Example 55 with Type

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

the class JavacResolution method ifTypeIsIterableToComponent.

public static Type ifTypeIsIterableToComponent(Type type, JavacAST ast) {
    Types types = Types.instance(ast.getContext());
    Symtab syms = Symtab.instance(ast.getContext());
    Type boundType = ReflectiveAccess.Types_upperBound(types, type);
    //		Type boundType = types.upperBound(type);
    Type elemTypeIfArray = types.elemtype(boundType);
    if (elemTypeIfArray != null)
        return elemTypeIfArray;
    Type base = types.asSuper(boundType, syms.iterableType.tsym);
    if (base == null)
        return syms.objectType;
    List<Type> iterableParams = base.allparams();
    return iterableParams.isEmpty() ? syms.objectType : ReflectiveAccess.Types_upperBound(types, iterableParams.head);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) Types(com.sun.tools.javac.code.Types) ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type)

Aggregations

Type (com.sun.tools.javac.code.Type)254 Symbol (com.sun.tools.javac.code.Symbol)64 ClassType (com.sun.tools.javac.code.Type.ClassType)55 ArrayType (com.sun.tools.javac.code.Type.ArrayType)47 MethodType (com.sun.tools.javac.code.Type.MethodType)42 WildcardType (com.sun.tools.javac.code.Type.WildcardType)42 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)36 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)33 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)32 JCTree (com.sun.tools.javac.tree.JCTree)27 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)24 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)23 Types (com.sun.tools.javac.code.Types)22 ExpressionTree (com.sun.source.tree.ExpressionTree)21 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 ArrayList (java.util.ArrayList)17 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)16 Tree (com.sun.source.tree.Tree)14 Name (com.sun.tools.javac.util.Name)14 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)13