Search in sources :

Example 11 with PackageElement

use of javax.lang.model.element.PackageElement in project react4j by react4j.

the class ReactProcessor method parse.

@Nonnull
private ComponentDescriptor parse(@Nonnull final TypeElement typeElement) {
    final String name = deriveComponentName(typeElement);
    final PackageElement packageElement = processingEnv.getElementUtils().getPackageOf(typeElement);
    final ComponentDescriptor descriptor = new ComponentDescriptor(name, packageElement, typeElement);
    determineComponentType(descriptor, typeElement);
    determineLifecycleMethods(typeElement, descriptor);
    determineRenderMethod(typeElement, descriptor);
    determineCallbacks(descriptor);
    determineProps(descriptor);
    determineDefaultPropsMethods(descriptor);
    determineDefaultPropsFields(descriptor);
    determineStateValues(descriptor);
    for (final PropDescriptor prop : descriptor.getProps()) {
        if (!isPropRequired(prop)) {
            prop.markAsOptional();
        }
    }
    /*
     * Sorting must occur after @PropDefault has been processed to ensure the sorting
     * correctly sorts optional props after required props.
     */
    descriptor.sortProps();
    verifyNoUnexpectedAbstractMethod(descriptor);
    return descriptor;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Nonnull(javax.annotation.Nonnull)

Example 12 with PackageElement

use of javax.lang.model.element.PackageElement in project butterknife by JakeWharton.

the class ButterKnifeProcessor method parseCompiledR.

private void parseCompiledR(PackageElement respectivePackageName, TypeElement rClass, Set<String> referenced) {
    for (Element element : rClass.getEnclosedElements()) {
        String innerClassName = element.getSimpleName().toString();
        if (SUPPORTED_TYPES.contains(innerClassName)) {
            for (Element enclosedElement : element.getEnclosedElements()) {
                if (enclosedElement instanceof VariableElement) {
                    String fqName = elementUtils.getPackageOf(enclosedElement).getQualifiedName().toString() + ".R." + innerClassName + "." + enclosedElement.toString();
                    if (referenced.contains(fqName)) {
                        VariableElement variableElement = (VariableElement) enclosedElement;
                        Object value = variableElement.getConstantValue();
                        if (value instanceof Integer) {
                            int id = (Integer) value;
                            ClassName rClassName = ClassName.get(elementUtils.getPackageOf(variableElement).toString(), "R", innerClassName);
                            String resourceName = variableElement.getSimpleName().toString();
                            QualifiedId qualifiedId = new QualifiedId(respectivePackageName, id);
                            symbols.put(qualifiedId, new Id(id, rClassName, resourceName));
                        }
                    }
                }
            }
        }
    }
}
Also used : 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) ClassName(com.squareup.javapoet.ClassName) BindString(butterknife.BindString) VariableElement(javax.lang.model.element.VariableElement)

Example 13 with PackageElement

use of javax.lang.model.element.PackageElement in project butterknife by JakeWharton.

the class ButterKnifeProcessor method scanForRClasses.

private void scanForRClasses(RoundEnvironment env) {
    if (trees == null)
        return;
    RClassScanner scanner = new RClassScanner();
    for (Class<? extends Annotation> annotation : getSupportedAnnotations()) {
        for (Element element : env.getElementsAnnotatedWith(annotation)) {
            JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation));
            if (tree != null) {
                // tree can be null if the references are compiled types and not source
                scanner.setCurrentPackage(elementUtils.getPackageOf(element));
                tree.accept(scanner);
            }
        }
    }
    for (Map.Entry<PackageElement, Set<Symbol.ClassSymbol>> packageNameToRClassSet : scanner.getRClasses().entrySet()) {
        PackageElement respectivePackageName = packageNameToRClassSet.getKey();
        for (Symbol.ClassSymbol rClass : packageNameToRClassSet.getValue()) {
            parseRClass(respectivePackageName, rClass, scanner.getReferenced());
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BitSet(java.util.BitSet) Symbol(com.sun.tools.javac.code.Symbol) 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) JCTree(com.sun.tools.javac.tree.JCTree) PackageElement(javax.lang.model.element.PackageElement) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processDefinitions.

private boolean processDefinitions(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String defintionClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        Map<String, String> baseTypes = processingContext.getDefinitionAnnotations().getBaseTypes();
        TypeElement parentElement = getDefinitionInheritedType(classElement);
        if (null != parentElement && !baseTypes.containsKey(defintionClassName)) {
            PackageElement basePackageElement = (PackageElement) parentElement.getEnclosingElement();
            String baseClassName = basePackageElement.getQualifiedName().toString() + "." + parentElement.getSimpleName();
            baseTypes.put(defintionClassName, baseClassName);
        }
        // Id field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_ID, processingContext.getDefinitionAnnotations().getIdFieldNames(), false);
        // Category field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_CATEGORY, processingContext.getDefinitionAnnotations().getCategoryFieldNames(), true);
        // Title field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_TITLE, processingContext.getDefinitionAnnotations().getTitleFieldNames(), false);
        // Description field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DESCRIPTION, processingContext.getDefinitionAnnotations().getDescriptionFieldNames(), false);
        // Labels field.
        processFieldName(classElement, defintionClassName, ANNOTATION_DEFINITION_LABELS, processingContext.getDefinitionAnnotations().getLabelsFieldNames(), true);
        // Builder class.
        processDefinitionModelBuilder(e, defintionClassName, processingContext.getDefinitionAnnotations().getBuilderFieldNames());
        // Graph element.
        Definition definitionAnn = e.getAnnotation(Definition.class);
        TypeMirror mirror = null;
        try {
            Class<?> graphClass = definitionAnn.graphFactory();
        } catch (MirroredTypeException mte) {
            mirror = mte.getTypeMirror();
        }
        if (null == mirror) {
            throw new RuntimeException("No graph factory class specified for the @Definition.");
        }
        String fqcn = mirror.toString();
        processingContext.getDefinitionAnnotations().getGraphFactoryFieldNames().put(defintionClassName, fqcn);
        // PropertySets fields.
        Map<String, Element> propertySetElements = getFieldNames(classElement, ANNOTATION_PROPERTY_SET);
        if (null != propertySetElements && !propertySetElements.isEmpty()) {
            processingContext.getPropertySetElements().addAll(propertySetElements.values());
            processingContext.getDefinitionAnnotations().getPropertySetFieldNames().put(defintionClassName, new LinkedHashSet<>(propertySetElements.keySet()));
        } else {
            note("Definition for tye [" + defintionClassName + "] have no Property Set members.");
        }
        // Properties fields.
        Map<String, Element> propertyElements = getFieldNames(classElement, ANNOTATION_PROPERTY);
        if (null != propertyElements && !propertyElements.isEmpty()) {
            processingContext.getPropertyElements().addAll(propertyElements.values());
            processingContext.getDefinitionAnnotations().getPropertyFieldNames().put(defintionClassName, new LinkedHashSet<>(propertyElements.keySet()));
        } else {
            note("Definition for tye [" + defintionClassName + "] have no Property members.");
        }
        // -- Morphing annotations --
        MorphBase morphBaseAnn = e.getAnnotation(MorphBase.class);
        Morph morphAnn = e.getAnnotation(Morph.class);
        if (null != morphBaseAnn && null != morphAnn) {
            TypeElement superElement = getAnnotationInTypeInheritance(classElement, MorphBase.class.getName());
            final String packageName = packageElement.getQualifiedName().toString();
            String morphBaseClassName = packageName + "." + superElement.getSimpleName().toString();
            Map<String, String> defaultTypesMap = processingContext.getMorphingAnnotations().getBaseDefaultTypes();
            if (null == defaultTypesMap.get(morphBaseClassName)) {
                TypeMirror morphDefaultTypeMirror = null;
                try {
                    Class<?> defaultTypeClass = morphBaseAnn.defaultType();
                } catch (MirroredTypeException mte) {
                    morphDefaultTypeMirror = mte.getTypeMirror();
                }
                if (null == morphDefaultTypeMirror) {
                    throw new RuntimeException("No default type class specifyed for the @MorphBase.");
                }
                String morphDefaultTypeClassName = morphDefaultTypeMirror.toString();
                processingContext.getMorphingAnnotations().getBaseDefaultTypes().put(morphBaseClassName, morphDefaultTypeClassName);
                // MorphBase - targets
                List<? extends TypeMirror> morphTargetMirrors = null;
                try {
                    Class<?>[] defsClasses = morphBaseAnn.targets();
                } catch (MirroredTypesException mte) {
                    morphTargetMirrors = mte.getTypeMirrors();
                }
                if (null != morphTargetMirrors) {
                    Set<String> morphTargetMirrorClasses = new LinkedHashSet<>();
                    for (TypeMirror morphTargetMirror : morphTargetMirrors) {
                        String morphTargetMirrorClassName = morphTargetMirror.toString();
                        morphTargetMirrorClasses.add(morphTargetMirrorClassName);
                    }
                    processingContext.getMorphingAnnotations().getBaseTargets().put(morphBaseClassName, morphTargetMirrorClasses);
                }
                // Morph Properties.
                processMorphProperties(superElement, morphBaseClassName);
            }
            TypeMirror morphBaseTypeMirror = null;
            try {
                Class<?> defaultTypeClass = morphAnn.base();
            } catch (MirroredTypeException mte) {
                morphBaseTypeMirror = mte.getTypeMirror();
            }
            if (null == morphBaseTypeMirror) {
                throw new RuntimeException("No base type class specifyed for the @MorphBase.");
            }
            String morphBaseTypeClassName = morphBaseTypeMirror.toString();
            Set<String> currentTargets = processingContext.getMorphingAnnotations().getBaseTargets().get(morphBaseTypeClassName);
            if (null == currentTargets) {
                currentTargets = new LinkedHashSet<>();
                processingContext.getMorphingAnnotations().getBaseTargets().put(morphBaseTypeClassName, currentTargets);
            }
            currentTargets.add(defintionClassName);
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MirroredTypesException(javax.lang.model.type.MirroredTypesException) 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) Definition(org.kie.workbench.common.stunner.core.definition.annotation.Definition) MorphBase(org.kie.workbench.common.stunner.core.definition.annotation.morph.MorphBase) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) Morph(org.kie.workbench.common.stunner.core.definition.annotation.morph.Morph) PackageElement(javax.lang.model.element.PackageElement)

Example 15 with PackageElement

use of javax.lang.model.element.PackageElement in project kie-wb-common by kiegroup.

the class MainProcessor method processPropertySets.

private boolean processPropertySets(final Set<? extends TypeElement> set, final Element e, final RoundEnvironment roundEnv) throws Exception {
    final boolean isClass = e.getKind() == ElementKind.CLASS;
    if (isClass) {
        TypeElement classElement = (TypeElement) e;
        PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
        String propertyClassName = packageElement.getQualifiedName().toString() + "." + classElement.getSimpleName();
        // Name fields.
        processFieldName(classElement, propertyClassName, ANNOTATION_NAME, processingContext.getPropertySetAnnotations().getNameFieldNames(), false);
        // Properties fields.
        Map<String, Element> propertyElements = getFieldNames(classElement, ANNOTATION_PROPERTY);
        if (null != propertyElements) {
            processingContext.getPropertyElements().addAll(propertyElements.values());
            processingContext.getPropertySetAnnotations().getPropertiesFieldNames().put(propertyClassName, new LinkedHashSet<>(propertyElements.keySet()));
            if (propertyElements.isEmpty()) {
                note("Property Set for tye [" + propertyClassName + "] have no Property members.");
            }
        }
    }
    return false;
}
Also used : 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) PackageElement(javax.lang.model.element.PackageElement)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)133 TypeElement (javax.lang.model.element.TypeElement)92 Element (javax.lang.model.element.Element)51 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8