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());
}
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;
}
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();
}
}
}
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();
}
}
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);
}
}
Aggregations