Search in sources :

Example 11 with ElementKind

use of javax.lang.model.element.ElementKind in project LoganSquare by bluelinelabs.

the class JsonFieldProcessor method ensureTypeConverterClassValid.

private boolean ensureTypeConverterClassValid(TypeMirror typeConverterClassMirror, Elements elements, Types types) {
    TypeElement typeConverterElement = elements.getTypeElement(typeConverterClassMirror.toString());
    if (typeConverterElement != null) {
        boolean isTypeConverterType = false;
        TypeElement element = typeConverterElement;
        while (!isTypeConverterType && element != null) {
            for (TypeMirror iface : element.getInterfaces()) {
                if (types.erasure(iface).toString().equals(TypeConverter.class.getCanonicalName())) {
                    isTypeConverterType = true;
                }
            }
            TypeMirror superClassMirror = element.getSuperclass();
            if (superClassMirror != null) {
                superClassMirror = types.erasure(superClassMirror);
                element = elements.getTypeElement(superClassMirror.toString());
            } else {
                element = null;
            }
        }
        if (!isTypeConverterType) {
            error(element, "TypeConverter elements must implement the TypeConverter interface or extend from one of the convenience helpers (ie StringBasedTypeConverter or DateTypeConverter).");
            return false;
        }
        boolean constructorIsDeclared = false;
        boolean hasAccessibleConstructor = false;
        List<? extends Element> enclosedElements = typeConverterElement.getEnclosedElements();
        for (Element enclosedElement : enclosedElements) {
            ElementKind enclosedElementKind = enclosedElement.getKind();
            if (enclosedElementKind == ElementKind.CONSTRUCTOR) {
                constructorIsDeclared = true;
                if (!enclosedElement.getModifiers().contains(Modifier.PRIVATE)) {
                    ExecutableElement executableElement = (ExecutableElement) enclosedElement;
                    if (executableElement.getParameters().size() == 0) {
                        hasAccessibleConstructor = true;
                    }
                }
            }
        }
        if (constructorIsDeclared && !hasAccessibleConstructor) {
            error(element, "TypeConverter classes must have a non-private zero argument constructor.");
            return false;
        }
    }
    return true;
}
Also used : TypeConverter(com.bluelinelabs.logansquare.typeconverters.TypeConverter) ElementKind(javax.lang.model.element.ElementKind) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 12 with ElementKind

use of javax.lang.model.element.ElementKind in project LoganSquare by bluelinelabs.

the class JsonObjectProcessor method addAllNonPrivateAccessors.

private void addAllNonPrivateAccessors(Element element, Elements elements, Types types, JsonObjectHolder objectHolder) {
    List<? extends Element> enclosedElements = element.getEnclosedElements();
    for (Element enclosedElement : enclosedElements) {
        ElementKind enclosedElementKind = enclosedElement.getKind();
        if (enclosedElementKind == ElementKind.FIELD) {
            Set<Modifier> modifiers = enclosedElement.getModifiers();
            if (modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.TRANSIENT) && !modifiers.contains(Modifier.STATIC)) {
                String getter = JsonFieldHolder.getGetter(enclosedElement, elements);
                String setter = JsonFieldHolder.getSetter(enclosedElement, elements);
                if (!TextUtils.isEmpty(getter) && !TextUtils.isEmpty(setter)) {
                    createOrUpdateFieldHolder(enclosedElement, elements, types, objectHolder);
                }
            }
        }
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Modifier(javax.lang.model.element.Modifier)

Example 13 with ElementKind

use of javax.lang.model.element.ElementKind in project buck by facebook.

the class TreeBackedElements method enterElement.

public TreeBackedElement enterElement(Element underlyingElement) {
    TreeBackedElement result = treeBackedElements.get(underlyingElement);
    if (result != null) {
        return result;
    }
    ElementKind kind = underlyingElement.getKind();
    switch(kind) {
        case PACKAGE:
            result = newTreeBackedPackage((PackageElement) underlyingElement);
            break;
        case ANNOTATION_TYPE:
        case CLASS:
        case ENUM:
        case INTERFACE:
            result = newTreeBackedType((TypeElement) underlyingElement);
            break;
        case TYPE_PARAMETER:
            result = newTreeBackedTypeParameter((TypeParameterElement) underlyingElement);
            break;
        case ENUM_CONSTANT:
        case FIELD:
        case PARAMETER:
            result = newTreeBackedVariable((VariableElement) underlyingElement);
            break;
        case CONSTRUCTOR:
        case METHOD:
            result = newTreeBackedExecutable((ExecutableElement) underlyingElement);
            break;
        // $CASES-OMITTED$
        default:
            throw new UnsupportedOperationException(String.format("Element kind %s NYI", kind));
    }
    treeBackedElements.put(underlyingElement, result);
    return result;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 14 with ElementKind

use of javax.lang.model.element.ElementKind in project tiger by google.

the class DependencyInformationCollectorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    if (annotations.isEmpty()) {
        return false;
    }
    boolean foundSomething = false;
    Element someElement = Iterables.getFirst(env.getElementsAnnotatedWith(Iterables.getFirst(annotations, null)), null);
    ElementKind elementKind = someElement.getKind();
    while (!(elementKind.equals(ElementKind.CLASS) || elementKind.equals(ElementKind.INTERFACE))) {
        someElement = someElement.getEnclosingElement();
        elementKind = someElement.getKind();
    }
    String suffix = ((TypeElement) someElement).getQualifiedName().toString().replace(".", "_");
    String uniqueSuffix = suffix + BaseEncoding.base64().encode(UUID.randomUUID().toString().getBytes());
    TypeSpec.Builder dependencyInfoCollectedBuilder = TypeSpec.classBuilder("DependencyInfo_" + uniqueSuffix).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    for (Element element : env.getElementsAnnotatedWith(Module.class)) {
        foundSomething = true;
        moduleStrings.add(element.toString());
    }
    String jsonString = gson.toJson(moduleStrings);
    dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_MODULES, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    for (Element element : env.getElementsAnnotatedWith(MembersInjector.class)) {
        foundSomething = true;
        membersInjectors.add(element.toString());
    }
    jsonString = gson.toJson(membersInjectors);
    dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_MEMBERS_INJECTORS, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    for (Element element : env.getElementsAnnotatedWith(ScopeDependency.class)) {
        foundSomething = true;
        scopeDependencies.add(element.toString());
    }
    if (!scopeDependencies.isEmpty()) {
        jsonString = gson.toJson(scopeDependencies);
        dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPE_DEPENDENCIES, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    }
    for (Element element : env.getElementsAnnotatedWith(ScopedComponentNames.class)) {
        foundSomething = true;
        scopedComponentNames.add(element.toString());
    }
    if (!scopedComponentNames.isEmpty()) {
        jsonString = gson.toJson(scopedComponentNames);
        dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPED_COMPONENT_NAMES, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    }
    for (Element element : env.getElementsAnnotatedWith(PackageForGenerated.class)) {
        foundSomething = true;
        jsonString = getPackageForGenerated((TypeElement) element);
        dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_PACKAGE_FOR_GENERATED, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    }
    for (Element element : env.getElementsAnnotatedWith(Inject.class)) {
        if (element.getKind().equals(ElementKind.CONSTRUCTOR)) {
            Element classElement = element.getEnclosingElement();
            if (Utils.getScopeType(classElement) != null) {
                foundSomething = true;
                ctorInjectedClassStrings.add(classElement.toString());
            }
        }
    }
    jsonString = gson.toJson(ctorInjectedClassStrings);
    dependencyInfoCollectedBuilder.addField(FieldSpec.builder(String.class, SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_CTOR_INJECTED_CLASSES, Modifier.PUBLIC, Modifier.FINAL).initializer("$S", jsonString).build());
    if (foundSomething) {
        writeJavaFile(SharedNames.DEPENDENCY_INFORMATION_PACKAGE_NAME, dependencyInfoCollectedBuilder.build());
    }
    return false;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 15 with ElementKind

use of javax.lang.model.element.ElementKind in project hibernate-orm by hibernate.

the class XmlMetaEntity method parseManyToOne.

private void parseManyToOne(ManyToOne manyToOne) {
    XmlMetaSingleAttribute attribute;
    ElementKind elementKind = getElementKind(manyToOne.getAccess());
    String type = getType(manyToOne.getName(), manyToOne.getTargetEntity(), elementKind);
    if (type != null) {
        attribute = new XmlMetaSingleAttribute(this, manyToOne.getName(), type);
        members.add(attribute);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind)

Aggregations

ElementKind (javax.lang.model.element.ElementKind)21 Element (javax.lang.model.element.Element)9 TypeElement (javax.lang.model.element.TypeElement)9 ExecutableElement (javax.lang.model.element.ExecutableElement)5 PackageElement (javax.lang.model.element.PackageElement)4 LinkedHashSet (java.util.LinkedHashSet)3 VariableElement (javax.lang.model.element.VariableElement)3 MetaModelGenerationException (org.hibernate.jpamodelgen.MetaModelGenerationException)3 HashSet (java.util.HashSet)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 Modifier (javax.lang.model.element.Modifier)2 TypeParameterElement (javax.lang.model.element.TypeParameterElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 TypeConverter (com.bluelinelabs.logansquare.typeconverters.TypeConverter)1 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)1 Function (com.google.common.base.Function)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1