use of javax.lang.model.element.Modifier in project DeepLinkDispatch by airbnb.
the class DeepLinkProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Set<Element> customAnnotations = new HashSet<>();
for (Element annotation : annotations) {
if (annotation.getAnnotation(DEEP_LINK_SPEC_CLASS) != null) {
customAnnotations.add(annotation);
}
}
Map<Element, String[]> prefixes = new HashMap<>();
Set<Element> customAnnotatedElements = new HashSet<>();
for (Element customAnnotation : customAnnotations) {
ElementKind kind = customAnnotation.getKind();
if (kind != ElementKind.ANNOTATION_TYPE) {
error(customAnnotation, "Only annotation types can be annotated with @%s", DEEP_LINK_SPEC_CLASS.getSimpleName());
}
String[] prefix = customAnnotation.getAnnotation(DEEP_LINK_SPEC_CLASS).prefix();
if (Utils.hasEmptyOrNullString(prefix)) {
error(customAnnotation, "Prefix property cannot have null or empty strings");
}
if (prefix.length == 0) {
error(customAnnotation, "Prefix property cannot be empty");
}
prefixes.put(customAnnotation, prefix);
for (Element customAnnotatedElement : roundEnv.getElementsAnnotatedWith(MoreElements.asType(customAnnotation))) {
customAnnotatedElements.add(customAnnotatedElement);
}
}
Set<Element> elementsToProcess = new HashSet<>(customAnnotatedElements);
elementsToProcess.addAll(roundEnv.getElementsAnnotatedWith(DEEP_LINK_CLASS));
List<DeepLinkAnnotatedElement> deepLinkElements = new ArrayList<>();
for (Element element : elementsToProcess) {
ElementKind kind = element.getKind();
if (kind != ElementKind.METHOD && kind != ElementKind.CLASS) {
error(element, "Only classes and methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
}
if (kind == ElementKind.METHOD) {
Set<Modifier> methodModifiers = element.getModifiers();
if (!methodModifiers.contains(Modifier.STATIC)) {
error(element, "Only static methods can be annotated with @%s", DEEP_LINK_CLASS.getSimpleName());
}
}
DeepLink deepLinkAnnotation = element.getAnnotation(DEEP_LINK_CLASS);
List<String> deepLinks = new ArrayList<>();
if (deepLinkAnnotation != null) {
deepLinks.addAll(Arrays.asList(deepLinkAnnotation.value()));
}
if (customAnnotatedElements.contains(element)) {
deepLinks.addAll(enumerateCustomDeepLinks(element, prefixes));
}
DeepLinkEntry.Type type = kind == ElementKind.CLASS ? DeepLinkEntry.Type.CLASS : DeepLinkEntry.Type.METHOD;
for (String deepLink : deepLinks) {
try {
deepLinkElements.add(new DeepLinkAnnotatedElement(deepLink, element, type));
} catch (MalformedURLException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Malformed Deep Link URL " + deepLink);
}
}
}
Set<? extends Element> deepLinkHandlerElements = roundEnv.getElementsAnnotatedWith(DeepLinkHandler.class);
for (Element deepLinkHandlerElement : deepLinkHandlerElements) {
Optional<AnnotationMirror> annotationMirror = getAnnotationMirror(deepLinkHandlerElement, DeepLinkHandler.class);
if (annotationMirror.isPresent()) {
Iterable<TypeMirror> klasses = getTypeValue(annotationMirror.get(), "value");
List<TypeElement> typeElements = FluentIterable.from(klasses).transform(new Function<TypeMirror, TypeElement>() {
@Override
public TypeElement apply(TypeMirror klass) {
return MoreTypes.asTypeElement(klass);
}
}).toList();
String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkHandlerElement).getQualifiedName().toString();
try {
generateDeepLinkDelegate(packageName, typeElements);
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
} catch (RuntimeException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
}
}
}
Set<? extends Element> deepLinkModuleElements = roundEnv.getElementsAnnotatedWith(DeepLinkModule.class);
for (Element deepLinkModuleElement : deepLinkModuleElements) {
String packageName = processingEnv.getElementUtils().getPackageOf(deepLinkModuleElement).getQualifiedName().toString();
try {
generateDeepLinkLoader(packageName, deepLinkModuleElement.getSimpleName().toString(), deepLinkElements);
} catch (IOException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Error creating file");
} catch (RuntimeException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Internal error during annotation processing: " + e.getClass().getSimpleName());
}
}
return false;
}
use of javax.lang.model.element.Modifier in project epoxy by airbnb.
the class ClassToGenerateInfo method collectMethodsReturningClassType.
/**
* Get information about methods returning class type of the original class so we can duplicate
* them in the generated class for chaining purposes
*/
private void collectMethodsReturningClassType(TypeElement originalClass) {
TypeElement clazz = originalClass;
while (clazz.getSuperclass().getKind() != TypeKind.NONE) {
for (Element subElement : clazz.getEnclosedElements()) {
Set<Modifier> modifiers = subElement.getModifiers();
if (subElement.getKind() == ElementKind.METHOD && !modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.STATIC)) {
TypeMirror methodReturnType = ((ExecutableType) subElement.asType()).getReturnType();
if (methodReturnType.equals(clazz.asType()) || typeUtils.isSubtype(clazz.asType(), methodReturnType)) {
ExecutableElement castedSubElement = ((ExecutableElement) subElement);
List<? extends VariableElement> params = castedSubElement.getParameters();
String methodName = subElement.getSimpleName().toString();
if (methodName.equals(RESET_METHOD) && params.isEmpty()) {
continue;
}
methodsReturningClassType.add(new MethodInfo(methodName, modifiers, buildParamList(params), castedSubElement.isVarArgs()));
}
}
}
clazz = (TypeElement) typeUtils.asElement(clazz.getSuperclass());
}
}
use of javax.lang.model.element.Modifier in project javapoet by square.
the class CodeWriter method emitModifiers.
/**
* Emits {@code modifiers} in the standard order. Modifiers in {@code implicitModifiers} will not
* be emitted.
*/
public void emitModifiers(Set<Modifier> modifiers, Set<Modifier> implicitModifiers) throws IOException {
if (modifiers.isEmpty())
return;
for (Modifier modifier : EnumSet.copyOf(modifiers)) {
if (implicitModifiers.contains(modifier))
continue;
emitAndIndent(modifier.name().toLowerCase(Locale.US));
emitAndIndent(" ");
}
}
use of javax.lang.model.element.Modifier in project javapoet by square.
the class Util method requireExactlyOneOf.
static void requireExactlyOneOf(Set<Modifier> modifiers, Modifier... mutuallyExclusive) {
int count = 0;
for (Modifier modifier : mutuallyExclusive) {
// Skip 'DEFAULT' if it doesn't exist!
if (modifier == null && Util.DEFAULT == null)
continue;
if (modifiers.contains(modifier))
count++;
}
checkArgument(count == 1, "modifiers %s must contain one of %s", modifiers, Arrays.toString(mutuallyExclusive));
}
use of javax.lang.model.element.Modifier in project lwjgl by LWJGL.
the class FieldsGenerator method validateField.
private static void validateField(VariableElement field) {
// Check if field is "public static final"
Set<Modifier> modifiers = field.getModifiers();
if (modifiers.size() != 3 || !modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC) || !modifiers.contains(Modifier.FINAL)) {
throw new RuntimeException("Field " + field.getSimpleName() + " is not declared public static final");
}
// Check suported types (int, long, float, String)
TypeMirror field_type = field.asType();
if ("java.lang.String".equals(field_type.toString())) {
} else if (field_type instanceof PrimitiveType) {
PrimitiveType field_type_prim = (PrimitiveType) field_type;
TypeKind field_kind = field_type_prim.getKind();
if (field_kind != TypeKind.INT && field_kind != TypeKind.LONG && field_kind != TypeKind.FLOAT && field_kind != TypeKind.BYTE) {
throw new RuntimeException("Field " + field.getSimpleName() + " is not of type 'int', 'long', 'float' or 'byte' " + field_kind.toString());
}
} else {
throw new RuntimeException("Field " + field.getSimpleName() + " is not a primitive type or String");
}
Object field_value = field.getConstantValue();
if (field_value == null) {
throw new RuntimeException("Field " + field.getSimpleName() + " has no initial value");
}
}
Aggregations