use of javax.lang.model.element.VariableElement in project camel by apache.
the class SpringAnnotationProcessor method processElement.
private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, XmlElementRef elementRef, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
Elements elementUtils = processingEnv.getElementUtils();
String fieldName;
fieldName = fieldElement.getSimpleName().toString();
if (element != null || elementRef != null) {
String kind = "element";
String name = element != null ? element.name() : elementRef.name();
if (isNullOrEmpty(name) || "##default".equals(name)) {
name = fieldName;
}
name = prefix + name;
TypeMirror fieldType = fieldElement.asType();
String fieldTypeName = fieldType.toString();
TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
if (isNullOrEmpty(docComment)) {
Metadata metadata = fieldElement.getAnnotation(Metadata.class);
docComment = metadata != null ? metadata.description() : null;
}
boolean required = element != null ? element.required() : elementRef.required();
// metadata may overrule element required
required = findRequired(fieldElement, required);
// gather enums
Set<String> enums = new LinkedHashSet<String>();
boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
if (isEnum) {
TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
// find all the enum constants which has the possible enum value that can be used
List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
for (VariableElement var : fields) {
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
String val = var.toString();
enums.add(val);
}
}
}
// is it a definition/factory-bean type then its a oneOf
TreeSet oneOfTypes = new TreeSet<String>();
if (fieldTypeName.endsWith("Definition") || fieldTypeName.endsWith("FactoryBean")) {
TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
if (definitionClass != null) {
XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
if (rootElement != null) {
String childName = rootElement.name();
if (childName != null) {
oneOfTypes.add(childName);
}
}
}
} else if (fieldTypeName.endsWith("Definition>") || fieldTypeName.endsWith("FactoryBean>")) {
// its a list so we need to load the generic type
String typeName = Strings.between(fieldTypeName, "<", ">");
TypeElement definitionClass = findTypeElement(processingEnv, roundEnv, typeName);
if (definitionClass != null) {
XmlRootElement rootElement = definitionClass.getAnnotation(XmlRootElement.class);
if (rootElement != null) {
String childName = rootElement.name();
if (childName != null) {
oneOfTypes.add(childName);
}
}
}
}
boolean oneOf = !oneOfTypes.isEmpty();
boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
boolean asPredicate = false;
String displayName = null;
Metadata metadata = fieldElement.getAnnotation(Metadata.class);
if (metadata != null) {
displayName = metadata.displayName();
}
EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, oneOf, oneOfTypes, asPredicate);
eipOptions.add(ep);
}
}
use of javax.lang.model.element.VariableElement in project camel by apache.
the class CoreEipAnnotationProcessor method findClassProperties.
protected void findClassProperties(ProcessingEnvironment processingEnv, PrintWriter writer, RoundEnvironment roundEnv, Set<EipOption> eipOptions, TypeElement originalClassType, TypeElement classElement, String prefix, String modelName) {
while (true) {
List<VariableElement> fieldElements = ElementFilter.fieldsIn(classElement.getEnclosedElements());
for (VariableElement fieldElement : fieldElements) {
String fieldName = fieldElement.getSimpleName().toString();
XmlAttribute attribute = fieldElement.getAnnotation(XmlAttribute.class);
if (attribute != null) {
boolean skip = processAttribute(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, attribute, eipOptions, prefix, modelName);
if (skip) {
continue;
}
}
XmlValue value = fieldElement.getAnnotation(XmlValue.class);
if (value != null) {
processValue(processingEnv, roundEnv, originalClassType, classElement, fieldElement, fieldName, value, eipOptions, prefix, modelName);
}
XmlElements elements = fieldElement.getAnnotation(XmlElements.class);
if (elements != null) {
processElements(processingEnv, roundEnv, classElement, elements, fieldElement, eipOptions, prefix);
}
XmlElement element = fieldElement.getAnnotation(XmlElement.class);
if (element != null) {
processElement(processingEnv, roundEnv, classElement, element, fieldElement, eipOptions, prefix);
}
// special for eips which has outputs or requires an expressions
XmlElementRef elementRef = fieldElement.getAnnotation(XmlElementRef.class);
if (elementRef != null) {
// special for routes
processRoutes(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
// special for outputs
processOutputs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
// special for when clauses (choice eip)
processRefWhenClauses(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
// special for rests (rest-dsl)
processRests(roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
// special for verbs (rest-dsl)
processVerbs(processingEnv, roundEnv, originalClassType, elementRef, fieldElement, fieldName, eipOptions, prefix);
// special for expression
processRefExpression(processingEnv, roundEnv, originalClassType, classElement, elementRef, fieldElement, fieldName, eipOptions, prefix);
}
}
// special when we process these nodes as they do not use JAXB annotations on fields, but on methods
if ("OptionalIdentifiedDefinition".equals(classElement.getSimpleName().toString())) {
processIdentified(processingEnv, roundEnv, originalClassType, classElement, eipOptions, prefix);
} else if ("RouteDefinition".equals(classElement.getSimpleName().toString())) {
processRoute(processingEnv, roundEnv, originalClassType, classElement, eipOptions, prefix);
}
// check super classes which may also have fields
TypeElement baseTypeElement = null;
TypeMirror superclass = classElement.getSuperclass();
if (superclass != null) {
String superClassName = canonicalClassName(superclass.toString());
baseTypeElement = findTypeElement(processingEnv, roundEnv, superClassName);
}
if (baseTypeElement != null) {
classElement = baseTypeElement;
} else {
break;
}
}
}
use of javax.lang.model.element.VariableElement in project camel by apache.
the class CoreEipAnnotationProcessor method processAttribute.
private boolean processAttribute(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, TypeElement classElement, VariableElement fieldElement, String fieldName, XmlAttribute attribute, Set<EipOption> eipOptions, String prefix, String modelName) {
Elements elementUtils = processingEnv.getElementUtils();
String name = attribute.name();
if (isNullOrEmpty(name) || "##default".equals(name)) {
name = fieldName;
}
// lets skip some unwanted attributes
if (skipUnwanted) {
// we want to skip inheritErrorHandler which is only applicable for the load-balancer
boolean loadBalancer = "LoadBalanceDefinition".equals(originalClassType.getSimpleName().toString());
if (!loadBalancer && "inheritErrorHandler".equals(name)) {
return true;
}
}
name = prefix + name;
TypeMirror fieldType = fieldElement.asType();
String fieldTypeName = fieldType.toString();
TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
boolean required = attribute.required();
// metadata may overrule element required
required = findRequired(fieldElement, required);
// gather enums
Set<String> enums = new TreeSet<String>();
boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
if (isEnum) {
TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
if (enumClass != null) {
// find all the enum constants which has the possible enum value that can be used
List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
for (VariableElement var : fields) {
if (var.getKind() == ElementKind.ENUM_CONSTANT) {
String val = var.toString();
enums.add(val);
}
}
}
}
boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
String displayName = null;
Metadata metadata = fieldElement.getAnnotation(Metadata.class);
if (metadata != null) {
displayName = metadata.displayName();
}
EipOption ep = new EipOption(name, displayName, "attribute", fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, false, null, false);
eipOptions.add(ep);
return false;
}
use of javax.lang.model.element.VariableElement in project dagger by square.
the class InjectAdapterProcessor method getMethod.
private MethodSpec getMethod(ExecutableElement constructor, boolean disambiguateFields, boolean injectMembers, ClassName injectedClassName) {
MethodSpec.Builder result = MethodSpec.methodBuilder("get").addJavadoc(AdapterJavadocs.GET_METHOD, injectedClassName).addAnnotation(Override.class).returns(injectedClassName).addModifiers(PUBLIC);
result.addCode("$T result = new $T(", injectedClassName, injectedClassName);
boolean first = true;
for (VariableElement parameter : constructor.getParameters()) {
if (!first)
result.addCode(", ");
else
first = false;
result.addCode("$N.get()", parameterName(disambiguateFields, parameter));
}
result.addCode(");\n");
if (injectMembers) {
result.addStatement("injectMembers(result)");
}
result.addStatement("return result");
return result.build();
}
use of javax.lang.model.element.VariableElement in project dagger by square.
the class InjectAdapterProcessor method generateInjectAdapter.
/**
* Write a companion class for {@code type} that extends {@link Binding}.
*
* @param constructor the injectable constructor, or null if this binding
* supports members injection only.
*/
private void generateInjectAdapter(TypeElement type, ExecutableElement constructor, List<Element> fields) throws IOException {
String packageName = getPackage(type).getQualifiedName().toString();
TypeMirror supertype = getApplicationSupertype(type);
if (supertype != null) {
supertype = processingEnv.getTypeUtils().erasure(supertype);
}
ClassName injectedClassName = ClassName.get(type);
ClassName adapterClassName = adapterName(injectedClassName, INJECT_ADAPTER_SUFFIX);
boolean isAbstract = type.getModifiers().contains(ABSTRACT);
boolean injectMembers = !fields.isEmpty() || supertype != null;
boolean disambiguateFields = !fields.isEmpty() && (constructor != null) && !constructor.getParameters().isEmpty();
boolean dependent = injectMembers || ((constructor != null) && !constructor.getParameters().isEmpty());
TypeSpec.Builder result = TypeSpec.classBuilder(adapterClassName.simpleName()).addOriginatingElement(type).addModifiers(PUBLIC, FINAL).superclass(ParameterizedTypeName.get(ClassName.get(Binding.class), injectedClassName)).addJavadoc("$L", bindingTypeDocs(injectableType(type.asType()), isAbstract, injectMembers, dependent).toString());
for (Element field : fields) {
result.addField(memberBindingField(disambiguateFields, field));
}
if (constructor != null) {
for (VariableElement parameter : constructor.getParameters()) {
result.addField(parameterBindingField(disambiguateFields, parameter));
}
}
if (supertype != null) {
result.addField(supertypeBindingField(supertype));
}
result.addMethod(writeInjectAdapterConstructor(constructor, type, injectedClassName));
if (dependent) {
result.addMethod(attachMethod(constructor, fields, disambiguateFields, injectedClassName, supertype, true));
result.addMethod(getDependenciesMethod(constructor, fields, disambiguateFields, supertype, true));
}
if (constructor != null) {
result.addMethod(getMethod(constructor, disambiguateFields, injectMembers, injectedClassName));
}
if (injectMembers) {
result.addMethod(membersInjectMethod(fields, disambiguateFields, injectedClassName, supertype));
}
JavaFile javaFile = JavaFile.builder(packageName, result.build()).addFileComment(AdapterJavadocs.GENERATED_BY_DAGGER).build();
javaFile.writeTo(processingEnv.getFiler());
}
Aggregations