Search in sources :

Example 46 with ElementKind

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

the class TigerDaggerGeneratorProcessor method generateWrapperComponentBuilder.

/**
 * Subcomponents has 1 ctor parameter, which is the parent (sub)component. Components has 0 ctor
 * parameter and 0 or more dependecies.
 */
private void generateWrapperComponentBuilder(TypeElement component, @Nullable TypeElement parentComponent, List<TypeElement> sortedPassedModules, TypeSpec.Builder componentBuilder, CoreInjectorInfo coreInjector, @Nullable TypeElement expliciteBuilder) {
    int pos = 0;
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, component: " + component + " explicit: " + (expliciteBuilder != null));
    Preconditions.checkArgument(utils.isComponent(component) != utils.isSubcomponent(component));
    boolean explicit = expliciteBuilder != null;
    boolean isSubcomponent = utils.isSubcomponent(component);
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 1");
    String packageString = utils.getPackageString(component);
    String generatedComponentSimpleName = utils.getComponentImplementationSimpleNameFromInterface(component);
    ClassName componentClassName = ClassName.get(packageString, generatedComponentSimpleName);
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 2");
    // Generate class header.
    String builderName = explicit ? expliciteBuilder.getSimpleName().toString() : "Builder";
    TypeSpec.Builder builderBuilder = TypeSpec.classBuilder(builderName).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 3");
    if (explicit) {
        ElementKind kind = expliciteBuilder.getKind();
        ClassName superName = ClassName.get(component).nestedClass(builderName);
        if (kind.equals(ElementKind.INTERFACE)) {
            builderBuilder.addSuperinterface(superName);
        } else {
            Preconditions.checkState(kind.equals(ElementKind.CLASS), TAG + " unexpected kind for builder: " + expliciteBuilder);
            builderBuilder.superclass(superName);
        }
    }
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 4");
    // ctor for subcomponent.
    if (isSubcomponent) {
        TypeName parentComponentTypeName = TypeName.get(componentToParentMap.get(component).asType());
        String parentComponentSourceCodeName = utils.getSourceCodeName(parentComponentTypeName);
        builderBuilder.addField(FieldSpec.builder(parentComponentTypeName, parentComponentSourceCodeName, Modifier.PRIVATE).build());
        MethodSpec.Builder ctorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
        ctorBuilder.addParameter(parentComponentTypeName, parentComponentSourceCodeName).addCode("this.$N = $N;", parentComponentSourceCodeName, parentComponentSourceCodeName);
        builderBuilder.addMethod(ctorBuilder.build());
    } else {
        // setter for parent component.
        ClassName dependencyClassName = null;
        if (explicit && parentComponent != null) {
            dependencyClassName = (ClassName) ClassName.get(parentComponent.asType());
            String methodName = explicit ? utils.getBuilderSetterName(types, elements, expliciteBuilder, parentComponent) : null;
            if (methodName != null || !explicit) {
                utils.addSetMethod(types, elements, componentClassName, builderBuilder, dependencyClassName, methodName, builderName);
            }
        }
    }
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 5");
    /**
     * Set deps methods.
     */
    for (TypeElement m : utils.sortByFullName(componentToComponentDependencyMap.get(component))) {
        String methodName = explicit ? Preconditions.checkNotNull(utils.getBuilderSetterName(types, elements, expliciteBuilder, m)) : null;
        messager.printMessage(Kind.NOTE, TAG + " generateWrapperComponentBuilder deps " + m);
        utils.addSetMethod(types, elements, componentClassName, builderBuilder, (ClassName) ClassName.get(m.asType()), methodName, builderName);
    }
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 6");
    /**
     * Set @BindsInstance methods.
     */
    /**
     * TODO: refactor this. see {@link #collectComponentToBindsInstanceMap()}
     */
    java.util.Set<BindingKey> instanceDeps = Sets.newHashSet(componentToBindsInstanceMap.get(component));
    // if (isSubcomponent) {
    // instanceDeps.remove(BindingKey.get(componentToParentMap.get(component)));
    // } else {
    // if (dependencyComponent != null) {
    // instanceDeps.remove(BindingKey.get(dependencyComponent));
    // }
    // }
    // for (TypeElement typeElement :componentToComponentDependencyMap.get(component)) {
    // instanceDeps.remove(BindingKey.get(typeElement));
    // }
    // for (TypeElement typeElement :sortedPassedModules) {
    // instanceDeps.remove(BindingKey.get(typeElement));
    // }
    messager.printMessage(Kind.NOTE, TAG + ".generateWrapperComponentBuilder instanceDeps" + instanceDeps);
    for (BindingKey key : utils.sortBindingKeys(instanceDeps)) {
        String methodName = explicit ? Preconditions.checkNotNull(utils.getBuilderSetterName(types, elements, expliciteBuilder, key)) : null;
        ClassName builderParentClassName = componentClassName;
        if (explicit) {
            ExecutableElement setter = utils.getBuilderSetter(types, elements, expliciteBuilder, key);
            if (setter.getReturnType().getKind() == TypeKind.VOID) {
                builderParentClassName = null;
            }
        }
        messager.printMessage(Kind.NOTE, TAG + " generateWrapperComponentBuilder @BindsInstance " + key);
        utils.addSetMethod(types, elements, builderParentClassName, builderBuilder, key.getTypeName(), methodName, builderName);
    }
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, pos: 7");
    /**
     * Set module methods.
     */
    for (TypeElement m : sortedPassedModules) {
        String methodName = explicit ? Preconditions.checkNotNull(utils.getBuilderSetterName(types, elements, expliciteBuilder, m)) : null;
        messager.printMessage(Kind.NOTE, TAG + " generateWrapperComponentBuilder module " + m);
        utils.addSetMethod(types, elements, componentClassName, builderBuilder, (ClassName) ClassName.get(m.asType()), methodName, builderName);
    }
    // build() method.
    String methodName = explicit ? utils.getBuildMethod(processingEnv.getTypeUtils(), elements, expliciteBuilder).getSimpleName().toString() : "build";
    MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).returns(componentClassName);
    StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
    boolean needLeadingComma = false;
    if (parentComponent != null) {
        needLeadingComma = true;
        returnCodeBuilder.append(utils.getSourceCodeName(parentComponent));
    }
    // return statement deps params
    Set<TypeElement> deps = componentToComponentDependencyMap.get(component);
    if (!deps.isEmpty()) {
        if (needLeadingComma) {
            returnCodeBuilder.append(", ");
        }
        needLeadingComma = true;
        for (TypeElement dep : utils.sortByFullName(deps)) {
            returnCodeBuilder.append(utils.getSourceCodeName(dep)).append(", ");
        }
        returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
    }
    // return statement @BindsInstance params
    Set<BindingKey> bindsInstances = componentToBindsInstanceMap.get(component);
    if (!bindsInstances.isEmpty()) {
        if (needLeadingComma) {
            returnCodeBuilder.append(", ");
        }
        needLeadingComma = true;
        for (BindingKey key : utils.sortBindingKeys(bindsInstances)) {
            returnCodeBuilder.append(utils.getSourceCodeNameHandlingBox(key, dependencies)).append(", ");
        }
        returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
    }
    // return statement module params
    if (!sortedPassedModules.isEmpty()) {
        if (needLeadingComma) {
            returnCodeBuilder.append(", ");
        }
        needLeadingComma = true;
        for (TypeElement module : sortedPassedModules) {
            String moduleFiledName = utils.getSourceCodeName(module);
            returnCodeBuilder.append(moduleFiledName).append(", ");
        }
        returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
    }
    returnCodeBuilder.append(");");
    messager.printMessage(Kind.NOTE, "generateWrapperComponentBuilder, return string: " + returnCodeBuilder.toString());
    buildMethodBuilder.addCode(returnCodeBuilder.toString(), componentClassName);
    builderBuilder.addMethod(buildMethodBuilder.build());
    componentBuilder.addType(builderBuilder.build());
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 47 with ElementKind

use of javax.lang.model.element.ElementKind in project spring-framework by spring-projects.

the class StandardStereotypesProvider method getStereotypes.

@Override
public Set<String> getStereotypes(Element element) {
    Set<String> stereotypes = new LinkedHashSet<>();
    ElementKind kind = element.getKind();
    if (kind != ElementKind.CLASS && kind != ElementKind.INTERFACE) {
        return stereotypes;
    }
    for (AnnotationMirror annotation : this.typeHelper.getAllAnnotationMirrors(element)) {
        String type = this.typeHelper.getType(annotation);
        if (type.startsWith("jakarta.")) {
            stereotypes.add(type);
        }
    }
    return stereotypes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ElementKind(javax.lang.model.element.ElementKind) AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Example 48 with ElementKind

use of javax.lang.model.element.ElementKind in project neo4j by neo4j.

the class PublicApiAnnotationProcessor method processType.

/**
 * Processing type elements. Class, interface, enum.
 */
private void processType(TypeElement typeElement) {
    // Dummy check for public modifier
    if (!typeElement.getModifiers().contains(Modifier.PUBLIC)) {
        error("Class marked as public is not actually public", typeElement);
    }
    // Add self to public API
    StringBuilder sb = new StringBuilder();
    addTypeName(sb, typeElement);
    addModifiers(sb, typeElement);
    addKindIdentifier(sb, typeElement);
    addSuperClass(sb, typeElement);
    addInterfaces(sb, typeElement);
    publicElements.add(sb.toString());
    // Traverse visible child elements
    for (Element element : typeElement.getEnclosedElements()) {
        Set<Modifier> modifiers = element.getModifiers();
        if (modifiers.contains(Modifier.PUBLIC) || modifiers.contains(Modifier.PROTECTED)) {
            ElementKind kind = element.getKind();
            switch(kind) {
                case ENUM:
                case INTERFACE:
                case CLASS:
                    pushScope("." + element.getSimpleName());
                    processType((TypeElement) element);
                    break;
                case ENUM_CONSTANT:
                case FIELD:
                    pushScope("#" + element);
                    processField((VariableElement) element);
                    break;
                case CONSTRUCTOR:
                case METHOD:
                    pushScope("::" + element);
                    processMethod((ExecutableElement) element);
                    break;
                default:
                    throw new AssertionError("???: " + kind);
            }
            popScope();
        }
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Modifier(javax.lang.model.element.Modifier)

Example 49 with ElementKind

use of javax.lang.model.element.ElementKind in project graal by oracle.

the class ElementUtils method getBinaryName.

public static String getBinaryName(TypeElement provider) {
    if (provider instanceof GeneratedElement) {
        String packageName = getPackageName(provider);
        Element enclosing = provider.getEnclosingElement();
        StringBuilder b = new StringBuilder();
        b.append(provider.getSimpleName().toString());
        while (enclosing != null) {
            ElementKind kind = enclosing.getKind();
            if ((kind.isClass() || kind.isInterface()) && enclosing instanceof TypeElement) {
                b.insert(0, enclosing.getSimpleName().toString() + "$");
            } else {
                break;
            }
            enclosing = enclosing.getEnclosingElement();
        }
        b.insert(0, packageName + ".");
        return b.toString();
    } else {
        return ProcessorContext.getInstance().getEnvironment().getElementUtils().getBinaryName(provider).toString();
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) GeneratedElement(com.oracle.truffle.dsl.processor.java.model.GeneratedElement) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) GeneratedElement(com.oracle.truffle.dsl.processor.java.model.GeneratedElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 50 with ElementKind

use of javax.lang.model.element.ElementKind in project graal by oracle.

the class AbstractCompiler method emitDeprecationWarning.

@Override
public final void emitDeprecationWarning(ProcessingEnvironment environment, Element element) {
    if (!emitDeprecationWarningImpl(environment, element)) {
        CharSequence ownerQualifiedName = "";
        Element enclosingElement = element.getEnclosingElement();
        if (enclosingElement != null) {
            ElementKind kind = enclosingElement.getKind();
            if (kind.isClass() || kind.isInterface() || kind == ElementKind.PACKAGE) {
                ownerQualifiedName = ((QualifiedNameable) enclosingElement).getQualifiedName();
            }
        }
        environment.getMessager().printMessage(Diagnostic.Kind.WARNING, String.format("%s in %s has been deprecated", element.getSimpleName(), ownerQualifiedName), element);
    }
}
Also used : ElementKind(javax.lang.model.element.ElementKind) Element(javax.lang.model.element.Element)

Aggregations

ElementKind (javax.lang.model.element.ElementKind)50 TypeElement (javax.lang.model.element.TypeElement)27 Element (javax.lang.model.element.Element)25 ExecutableElement (javax.lang.model.element.ExecutableElement)23 VariableElement (javax.lang.model.element.VariableElement)13 TypeMirror (javax.lang.model.type.TypeMirror)13 PackageElement (javax.lang.model.element.PackageElement)9 ArrayList (java.util.ArrayList)8 AnnotationMirror (javax.lang.model.element.AnnotationMirror)7 LinkedHashSet (java.util.LinkedHashSet)6 ClassName (com.squareup.javapoet.ClassName)5 MethodSpec (com.squareup.javapoet.MethodSpec)5 TypeName (com.squareup.javapoet.TypeName)5 HashSet (java.util.HashSet)5 Modifier (javax.lang.model.element.Modifier)5 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TypeParameterElement (javax.lang.model.element.TypeParameterElement)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3