use of javax.lang.model.element.ElementKind in project vertx-docgen by vert-x3.
the class Helper method resolveLink.
public Element resolveLink(String signature) {
Matcher signatureMatcher = P.matcher(signature);
if (signatureMatcher.find()) {
String memberName = signatureMatcher.group(1);
String typeName = signature.substring(0, signatureMatcher.start());
TypeElement typeElt = elementUtils.getTypeElement(typeName);
if (typeElt == null) {
return null;
}
Predicate<? super Element> memberMatcher;
if (signatureMatcher.group(2) != null) {
String t = signatureMatcher.group(2).trim();
Predicate<ExecutableElement> parametersMatcher;
if (t.length() == 0) {
parametersMatcher = exeElt -> exeElt.getParameters().isEmpty();
} else {
parametersMatcher = parametersMatcher(t.split("\\s*,\\s*"));
}
memberMatcher = elt -> matchesConstructor(elt, memberName, parametersMatcher) || matchesMethod(elt, memberName, parametersMatcher);
} else {
memberMatcher = elt -> matchesConstructor(elt, memberName, exeElt -> true) || matchesMethod(elt, memberName, exeElt -> true) || matchesFieldOrEnumConstant(elt, memberName);
}
// The order of kinds is important
for (ElementKind kind : Arrays.asList(ElementKind.FIELD, ElementKind.ENUM_CONSTANT, ElementKind.CONSTRUCTOR, ElementKind.METHOD)) {
for (Element memberElt : elementUtils.getAllMembers(typeElt)) {
if (memberElt.getKind() == kind && memberMatcher.test(memberElt)) {
return memberElt;
}
}
}
return null;
} else {
Element elt = elementUtils.getTypeElement(signature);
if (elt == null) {
elt = elementUtils.getPackageElement(signature);
}
return elt;
}
}
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);
}
}
}
}
}
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;
}
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) {
utils = new Utils(processingEnv, 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) != 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;
}
use of javax.lang.model.element.ElementKind in project tiger by google.
the class Tiger2InjectorGenerator method generateInjectorBuilder.
/**
* Subcomponents has 1 ctor parameter, which is the parent (sub)component. Components has 0 ctor
* parameter and 0 or more dependecies.
*/
protected void generateInjectorBuilder(@Nullable TypeElement expliciteBuilder) {
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, component: " + eitherComponent + " explicit: " + (expliciteBuilder != null));
Preconditions.checkArgument(utils.isEitherComponent(eitherComponent), "Expect (sub)component, but found: " + eitherComponent);
boolean explicit = expliciteBuilder != null;
boolean isSubcomponent = utils.isSubcomponent(eitherComponent);
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 1");
String packageString = utils.getPackageString(eitherComponent);
String generatedComponentSimpleName = utils.getComponentImplementationSimpleNameFromInterface(eitherComponent);
ClassName componentClassName = ClassName.get(packageString, generatedComponentSimpleName);
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 2");
// Generate class header.
String builderName = explicit ? expliciteBuilder.getSimpleName().toString() : "Builder";
Builder builderBuilder = TypeSpec.classBuilder(builderName).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 3");
if (explicit) {
ElementKind kind = expliciteBuilder.getKind();
ClassName superName = ClassName.get(eitherComponent).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, "generateInjectorBuilder, pos: 4");
// ctor for subcomponent.
if (isSubcomponent) {
TypeName parentComponentTypeName = TypeName.get(parentEitherComponent.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 && parentEitherComponent != null) {
dependencyClassName = (ClassName) ClassName.get(parentEitherComponent.asType());
String methodName = explicit ? utils.getBuilderSetterName(types, elements, expliciteBuilder, parentEitherComponent) : null;
if (methodName != null || !explicit) {
utils.addSetMethod(types, elements, componentClassName, builderBuilder, dependencyClassName, methodName, builderName);
}
}
}
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 5");
/**
* Set deps methods.
*/
for (TypeElement m : componentDependencies) {
String methodName = explicit ? Preconditions.checkNotNull(utils.getBuilderSetterName(types, elements, expliciteBuilder, m)) : null;
logger.l(Kind.NOTE, "componentDependency: %s", m);
utils.addSetMethod(types, elements, componentClassName, builderBuilder, ClassName.get(m.asType()), methodName, builderName);
}
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 6");
/**
* Set @BindsInstance methods.
*/
/**
* TODO: refactor this. see {@link #collectComponentToBindsInstanceMap()}
*/
// 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 + ".generateInjectorBuilder instanceDeps" + bindsInstances);
for (BindingKey key : bindsInstances) {
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 + " generateInjectorBuilder @BindsInstance " + key);
utils.addSetMethod(types, elements, builderParentClassName, builderBuilder, key.getTypeName(), methodName, builderName);
}
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, pos: 7");
/**
* Set module methods.
*/
for (TypeElement m : nonNullaryCtorModules) {
String methodName = explicit ? Preconditions.checkNotNull(utils.getBuilderSetterName(types, elements, expliciteBuilder, m)) : null;
logger.l(Kind.NOTE, "nonNullaryCtorModules: %s", m);
utils.addSetMethod(types, elements, componentClassName, builderBuilder, 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(");
for (BindingKey key : utils.sortBindingKeys(getAllCtorParameters())) {
returnCodeBuilder.append(Utils.getSourceCodeNameHandlingBox(key, dependencies)).append(", ");
}
if (returnCodeBuilder.charAt(returnCodeBuilder.length() - 1) != '(') {
returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
}
returnCodeBuilder.append(");");
messager.printMessage(Kind.NOTE, "generateInjectorBuilder, return string: " + returnCodeBuilder.toString());
buildMethodBuilder.addCode(returnCodeBuilder.toString(), componentClassName);
builderBuilder.addMethod(buildMethodBuilder.build());
injectorBuilder.addType(builderBuilder.build());
}
Aggregations