Search in sources :

Example 61 with Types

use of javax.lang.model.util.Types in project react4j by react4j.

the class ReactProcessor method determineLifecycleMethods.

private void determineLifecycleMethods(@Nonnull final TypeElement typeElement, @Nonnull final ComponentDescriptor descriptor) {
    /*
     * Get the list of lifecycle methods that have been overridden by typeElement
     * a parent class, or by a default method method implemented by typeElement or
     * a parent class.
     */
    final Collection<ExecutableElement> lifecycleMethods = getComponentLifecycleMethods().values();
    final Elements elementUtils = processingEnv.getElementUtils();
    final Types typeUtils = processingEnv.getTypeUtils();
    final TypeElement componentType = elementUtils.getTypeElement(Constants.COMPONENT_CLASSNAME);
    final List<MethodDescriptor> overriddenLifecycleMethods = // Get all methods on type parent classes, and default methods from interfaces
    ProcessorUtil.getMethods(typeElement, processingEnv.getTypeUtils()).stream().filter(m -> lifecycleMethods.stream().anyMatch(l -> elementUtils.overrides(m, l, typeElement))).filter(m -> m.getEnclosingElement() != componentType).map(m -> new MethodDescriptor(m, (ExecutableType) typeUtils.asMemberOf(descriptor.getDeclaredType(), m))).collect(Collectors.toList());
    descriptor.setLifecycleMethods(overriddenLifecycleMethods);
}
Also used : Arrays(java.util.Arrays) PackageElement(javax.lang.model.element.PackageElement) AbstractProcessor(javax.annotation.processing.AbstractProcessor) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) SupportedSourceVersion(javax.annotation.processing.SupportedSourceVersion) Kind(javax.tools.Diagnostic.Kind) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintWriter(java.io.PrintWriter) ElementKind(javax.lang.model.element.ElementKind) ExecutableType(javax.lang.model.type.ExecutableType) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) TypeSpec(com.squareup.javapoet.TypeSpec) Processor(javax.annotation.processing.Processor) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeKind(javax.lang.model.type.TypeKind) JavaFile(com.squareup.javapoet.JavaFile) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) AutoService(com.google.auto.service.AutoService) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements)

Example 62 with Types

use of javax.lang.model.util.Types in project checker-framework by typetools.

the class CFAbstractStore method canAlias.

/**
 * Can the objects {@code a} and {@code b} be aliases? Returns a conservative answer (i.e.,
 * returns {@code true} if not enough information is available to determine aliasing).
 */
@Override
public boolean canAlias(FlowExpressions.Receiver a, FlowExpressions.Receiver b) {
    TypeMirror tb = b.getType();
    TypeMirror ta = a.getType();
    Types types = analysis.getTypes();
    return types.isSubtype(ta, tb) || types.isSubtype(tb, ta);
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror)

Example 63 with Types

use of javax.lang.model.util.Types in project checker-framework by typetools.

the class DefaultTypeHierarchy method castedAsSuper.

/**
 * Calls asSuper and casts the result to the same type as the input supertype
 *
 * @param subtype subtype to be transformed to supertype
 * @param supertype supertype that subtype is transformed to
 * @param <T> the type of supertype and return type
 * @return subtype as an instance of supertype
 */
@SuppressWarnings("unchecked")
public static <T extends AnnotatedTypeMirror> T castedAsSuper(final AnnotatedTypeMirror subtype, final T supertype) {
    final Types types = subtype.atypeFactory.getProcessingEnv().getTypeUtils();
    final Elements elements = subtype.atypeFactory.getProcessingEnv().getElementUtils();
    if (subtype.getKind() == TypeKind.NULL) {
        // Make a copy of the supertype so that if supertype is a composite type, the
        // returned type will be fully annotated.  (For example, if sub is @C null and super is
        // @A List<@B String>, then the returned type is @C List<@B String>.)
        T copy = (T) supertype.deepCopy();
        copy.replaceAnnotations(subtype.getAnnotations());
        return copy;
    }
    final T asSuperType = AnnotatedTypes.asSuper(subtype.atypeFactory, subtype, supertype);
    fixUpRawTypes(subtype, asSuperType, supertype, types);
    // @1 Enum<@2 E>
    if (asSuperType != null && isEnum(asSuperType) && isDeclarationOfJavaLangEnum(types, elements, supertype)) {
        final AnnotatedDeclaredType resultAtd = ((AnnotatedDeclaredType) supertype).deepCopy();
        resultAtd.clearAnnotations();
        resultAtd.addAnnotations(asSuperType.getAnnotations());
        final AnnotatedDeclaredType asSuperAdt = (AnnotatedDeclaredType) asSuperType;
        if (resultAtd.getTypeArguments().size() > 0 && asSuperAdt.getTypeArguments().size() > 0) {
            final AnnotatedTypeMirror sourceTypeArg = asSuperAdt.getTypeArguments().get(0);
            final AnnotatedTypeMirror resultTypeArg = resultAtd.getTypeArguments().get(0);
            resultTypeArg.clearAnnotations();
            resultTypeArg.addAnnotations(sourceTypeArg.getAnnotations());
            return (T) resultAtd;
        }
    }
    return asSuperType;
}
Also used : Types(javax.lang.model.util.Types) AnnotatedTypes(org.checkerframework.framework.util.AnnotatedTypes) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) Elements(javax.lang.model.util.Elements)

Example 64 with Types

use of javax.lang.model.util.Types in project checker-framework by typetools.

the class IrrelevantTypeAnnotator method scan.

@Override
protected Void scan(AnnotatedTypeMirror type, Void aVoid) {
    switch(type.getKind()) {
        case TYPEVAR:
        case WILDCARD:
        case EXECUTABLE:
        case INTERSECTION:
        case UNION:
        case NULL:
        case NONE:
        case PACKAGE:
        case VOID:
            return super.scan(type, aVoid);
        default:
    }
    Types types = typeFactory.getContext().getTypeUtils();
    TypeMirror typeMirror = type.getUnderlyingType();
    if (TypesUtils.isPrimitive(typeMirror)) {
        typeMirror = types.boxedClass((PrimitiveType) typeMirror).asType();
    }
    boolean shouldAnnotate = true;
    if (allFoundRelevantTypes.contains(typeMirror)) {
        shouldAnnotate = false;
    } else if (typeMirror.getKind() == TypeKind.DECLARED) {
        for (TypeMirror supportedType : relevantTypes) {
            if (types.isSubtype(typeMirror, supportedType)) {
                shouldAnnotate = false;
                allFoundRelevantTypes.add(typeMirror);
                break;
            }
        }
    } else if (typeMirror.getKind() == TypeKind.ARRAY) {
        shouldAnnotate = arraysAreRelevant;
        if (arraysAreRelevant) {
            allFoundRelevantTypes.add(typeMirror);
        }
    }
    if (shouldAnnotate) {
        type.addMissingAnnotations(annotations);
    }
    return super.scan(type, aVoid);
}
Also used : Types(javax.lang.model.util.Types) RelevantJavaTypes(org.checkerframework.framework.qual.RelevantJavaTypes) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Example 65 with Types

use of javax.lang.model.util.Types in project graal by oracle.

the class MatchProcessor method processMatchableNode.

private void processMatchableNode(Element element, TypeElement topDeclaringType, MatchableNode matchable, AnnotationMirror mirror) throws GraalError {
    logMessage("processMatchableNode %s %s %s\n", topDeclaringType, element, matchable);
    String nodeClass;
    String nodePackage;
    TypeMirror nodeClassMirror = null;
    try {
        matchable.nodeClass();
    } catch (MirroredTypeException e) {
        nodeClassMirror = e.getTypeMirror();
    }
    if (nodeClassMirror == null) {
        throw new GraalError("Can't get mirror for node class %s", element);
    }
    if (nodeClassMirror.toString().equals(MatchableNode.class.getName())) {
        nodeClass = topDeclaringType.getQualifiedName().toString();
    } else {
        nodeClass = nodeClassMirror.toString();
    }
    TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(nodeClass);
    if (typeElement == null) {
        errorMessage(element, mirror, "Class \"%s\" cannot be resolved to a type", nodeClass);
        return;
    }
    nodePackage = findPackage(typeElement);
    assert nodeClass.startsWith(nodePackage);
    nodeClass = nodeClass.substring(nodePackage.length() + 1);
    assert nodeClass.endsWith("Node");
    String shortName = nodeClass.substring(0, nodeClass.length() - 4);
    Types typeUtils = processingEnv.getTypeUtils();
    TypeElement nodeClassElement = (TypeElement) typeUtils.asElement(nodeClassMirror);
    for (String input : matchable.inputs()) {
        boolean ok = false;
        TypeElement current = nodeClassElement;
        while (!ok && current != null) {
            for (Element fieldElement : ElementFilter.fieldsIn(current.getEnclosedElements())) {
                if (fieldElement.getSimpleName().toString().equals(input)) {
                    ok = true;
                    break;
                }
            }
            TypeMirror theSuper = current.getSuperclass();
            current = (TypeElement) typeUtils.asElement(theSuper);
        }
        if (!ok) {
            errorMessage(element, mirror, "Input named \"%s\" doesn't exist in %s", input, nodeClassElement.getSimpleName());
        }
    }
    declareType(nodeClassMirror, shortName, nodeClass, nodePackage, matchable.inputs(), matchable.commutative(), matchable.shareable(), element);
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) MatchableNode(org.graalvm.compiler.core.match.MatchableNode) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) GraalError(org.graalvm.compiler.debug.GraalError) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

Types (javax.lang.model.util.Types)113 TypeMirror (javax.lang.model.type.TypeMirror)77 TypeElement (javax.lang.model.element.TypeElement)52 Elements (javax.lang.model.util.Elements)48 ExecutableElement (javax.lang.model.element.ExecutableElement)34 Element (javax.lang.model.element.Element)30 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)27 DeclaredType (javax.lang.model.type.DeclaredType)26 ArrayList (java.util.ArrayList)25 Map (java.util.Map)24 VariableElement (javax.lang.model.element.VariableElement)24 List (java.util.List)21 AnnotationMirror (javax.lang.model.element.AnnotationMirror)20 TypeKind (javax.lang.model.type.TypeKind)18 Set (java.util.Set)16 Collection (java.util.Collection)15 HashMap (java.util.HashMap)13 ElementKind (javax.lang.model.element.ElementKind)13 Modifier (javax.lang.model.element.Modifier)13 IOException (java.io.IOException)12