private boolean hasEnumContainedElementType() { return containedTypeElement != null && containedTypeElement.getKind() == ElementKind.ENUM; }
private boolean isAbstractClass(Element annotatedElement) { if (annotatedElement instanceof TypeElement) { TypeElement typeElement = (TypeElement) annotatedElement; return typeElement.getKind() == ElementKind.CLASS && typeElement.getModifiers().contains(Modifier.ABSTRACT); } else { return false; } }
public boolean isInterface(TypeElement element) { return element.getKind().isInterface(); }
@Override public Type visitDeclared(DeclaredType declaredType, Boolean p) { if (declaredType.asElement() instanceof TypeElement) { TypeElement typeElement = (TypeElement) declaredType.asElement(); switch (typeElement.getKind()) { case ENUM: return createEnumType(declaredType, typeElement, p); case ANNOTATION_TYPE: case CLASS: return createClassType(declaredType, typeElement, p); case INTERFACE: return createInterfaceType(declaredType, typeElement, p); default: throw new IllegalArgumentException("Illegal type " + typeElement); } } else { throw new IllegalArgumentException("Unsupported element type " + declaredType.asElement()); } }
static boolean isKotlinSingleton(TypeElement element) { return element.getKind() == ElementKind.CLASS && element .getEnclosedElements() .stream() .anyMatch( e -> { final CharSequence instanceFieldName = "INSTANCE"; return e.getSimpleName().contentEquals(instanceFieldName) && e.asType().toString().equals(((Symbol.ClassSymbol) element).className()) && e.getModifiers() .containsAll( ImmutableList.of(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)); }); }
private boolean isInaccessibleViaGeneratedCode(Class<? extends Annotation> annotationClass, String targetThing, Element element) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify field or method modifiers. Set<Modifier> modifiers = element.getModifiers(); if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) { error(element, "@%s %s must not be private or static. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify containing type. if (enclosingElement.getKind() != CLASS) { error(enclosingElement, "@%s %s may only be contained in classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } // Verify containing class visibility is not private. if (enclosingElement.getModifiers().contains(PRIVATE)) { error(enclosingElement, "@%s %s may not be contained in private classes. (%s.%s)", annotationClass.getSimpleName(), targetThing, enclosingElement.getQualifiedName(), element.getSimpleName()); hasError = true; } return hasError; }
@Override public TypeElement visitDeclared(DeclaredType t, Void p) { if (t.asElement() instanceof TypeElement) { TypeElement typeElement = (TypeElement) t.asElement(); switch (typeElement.getKind()) { case ENUM: return skipEnum ? null : typeElement; case CLASS: return typeElement; case INTERFACE: return visitInterface(t); default: throw new IllegalArgumentException("Illegal type: " + typeElement); } } else { return null; } }
@Override public Binding<?> getAtInjectBinding( String key, String className, ClassLoader classLoader, boolean mustHaveInjections) { TypeElement type = resolveType(processingEnv.getElementUtils(), className); if (type == null) { // We've encountered a type that the compiler can't introspect. If this // causes problems in practice (due to incremental compiles, etc.) we // should return a new unresolved binding and warn about the possibility // of runtime failures. return null; } if (type.getKind() == ElementKind.INTERFACE) { return null; } return GraphAnalysisInjectBinding.create(type, mustHaveInjections); }
public static boolean isValidClass(Messager messager, TypeElement element) { if (element.getKind() != ElementKind.CLASS) { return false; } if (!isPublic(element)) { String message = String.format("Classes annotated with %s must be public.", ANNOTATION); messager.printMessage(Diagnostic.Kind.ERROR, message, element); return false; } if (isAbstract(element)) { String message = String.format("Classes annotated with %s must not be abstract.", ANNOTATION); messager.printMessage(Diagnostic.Kind.ERROR, message, element); return false; } return true; }
@Override protected boolean hasPropertyGeneratorForField(VariableElement field, DeclaredTypeName fieldType) { if (TypeConstants.isConstant(field)) { // Looks like a constant, ignore return false; } TypeElement typeElement = (TypeElement) utils.getTypes().asElement(field.asType()); return typeElement != null && typeElement.getKind() == ElementKind.ENUM; }
public static boolean isValidClass(Messager messager, TypeElement element) { if (element.getKind() != ElementKind.CLASS) { return false; } if (!isPublic(element)) { String message = String.format("Classes annotated with %s must be public.", ANNOTATION); messager.printMessage(Diagnostic.Kind.ERROR, message, element); return false; } if (isAbstract(element)) { String message = String.format("Classes annotated with %s must not be abstract.", ANNOTATION); messager.printMessage(Diagnostic.Kind.ERROR, message, element); return false; } return true; }
public boolean hasEnumFirstTypeParameter() { return typeKind().isEnumKeyed() && containedTypeElement.getKind() == ElementKind.ENUM; }
public Collection<TypeElement> getEnumElements() { if (isEnumType()) { return Collections.singletonList(containedTypeElement); } if (!isContainerType()) { List<TypeElement> elements = Lists.newArrayListWithCapacity(2); if (hasEnumContainedElementType()) { elements.add(containedTypeElement); } if (isMapType() && containedSecondaryTypeElement.getKind() == ElementKind.ENUM) { elements.add(containedSecondaryTypeElement); } return elements; } return Collections.emptyList(); }
@Override public void processTypeElement(final TypeElement annotation, final TypeElement element, final MessageInterface messageInterface) { if (generatedFilesPath != null) { if (element.getKind().isInterface()) { String packageName = processingEnv.getElementUtils().getPackageOf(element).getQualifiedName().toString(); String relativePath = packageName.replace('.', File.separatorChar); String fileName = getPrimaryClassNamePrefix(element) + GENERATED_FILE_EXTENSION; this.generateSkeletalTranslationFile(relativePath, fileName, messageInterface); } } // Always generate an Interface.i18n.properties file. generateDefaultTranslationFile(messageInterface); }
@Override public String tableName() { return annotationOf(Table.class).map(Table::name) .orElse( annotationOf(javax.persistence.Table.class) .map(javax.persistence.Table::name) .orElse( annotationOf(View.class).map(View::name) .orElse( element().getKind().isInterface() || isImmutable() ? element().getSimpleName().toString() : Names.removeClassPrefixes(element().getSimpleName())))); }
@Override public boolean isUnimplementable() { boolean extendable = annotationOf(Entity.class).map(Entity::extendable).orElse(true); return !extendable || (element().getKind().isClass() && element().getModifiers().contains(Modifier.FINAL)); }
private boolean isExtending(TypeElement element) { if (element.getKind() == ElementKind.CLASS) { String superclassString = SourceExtraction.getSuperclassString(element); String rawSuperclass = SourceTypes.extract(superclassString).getKey(); // If we are extending yet to be generated builder, we detect it by having the same name // as relative name of builder type return rawSuperclass.endsWith(typeImplementationBuilder().relativeRaw()); } return false; }
private void collectInterfacesMirrors(TypeMirror typeMirror, TypevarContext context) { if (typeMirror.getKind() != TypeKind.DECLARED) { return; } DeclaredType declaredType = toDeclaredType(typeMirror); TypeElement e = toTypeElement(declaredType); if (e.getKind().isInterface()) { implementedInterfaces.add(e); String stringified = stringify(declaredType, context); TypevarContext nestedContext = new TypevarContext(e, stringified); implementedInterfaceNames.add(stringified); for (TypeMirror m : e.getInterfaces()) { collectInterfacesMirrors(m, nestedContext); } } }
@Override public void generate(EntityDescriptor entity, TypeSpec.Builder builder) { if (isObservable() && entity.element().getKind().isInterface()) { builder.superclass(ClassName.get(BINDING_PACKAGE, "BaseObservable")); } }