public static Visibility of(Element element) { if (element.getModifiers().contains(Modifier.PUBLIC)) { return PUBLIC; } if (element.getModifiers().contains(Modifier.PRIVATE)) { return PRIVATE; } return PACKAGE; }
/** * Returns true if and only if a component can instantiate new instances (typically of a module) * rather than requiring that they be passed. */ static boolean componentCanMakeNewInstances(TypeElement typeElement) { switch (typeElement.getKind()) { case CLASS: break; case ENUM: case ANNOTATION_TYPE: case INTERFACE: return false; default: throw new AssertionError("TypeElement cannot have kind: " + typeElement.getKind()); } if (typeElement.getModifiers().contains(ABSTRACT)) { return false; } if (requiresEnclosingInstance(typeElement)) { return false; } for (Element enclosed : typeElement.getEnclosedElements()) { if (enclosed.getKind().equals(CONSTRUCTOR) && ((ExecutableElement) enclosed).getParameters().isEmpty() && !enclosed.getModifiers().contains(PRIVATE)) { return true; } } // TODO(gak): still need checks for visibility return false; }
static boolean isStatic(Element element) { for (Modifier modifier : element.getModifiers()) { if (modifier.equals(Modifier.STATIC)) { return true; } } return false; }
private void createBeanInfo(TypeElement classElement) throws Exception { if (verbose) { processingEnv .getMessager() .printMessage( Diagnostic.Kind.NOTE, "Generating BeanInfo for " + classElement.getSimpleName()); } String className = classElement.getSimpleName().toString(); String qualifiedName = classElement.getQualifiedName().toString(); PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); String packageName = packageElement.getQualifiedName().toString(); Map<String, VariableElement> fields = new HashMap<String, VariableElement>(); Writer writer = null; try { // Build Bean Info TypeElement curClassElement = classElement; do { for (Element e : curClassElement.getEnclosedElements()) { if (e.getKind().equals(ElementKind.FIELD) && !e.getModifiers().contains(Modifier.TRANSIENT) && !e.getModifiers().contains(Modifier.STATIC)) { fields.put(e.getSimpleName().toString(), (VariableElement) e); } } TypeMirror t = curClassElement.getSuperclass(); if (t instanceof DeclaredType) { curClassElement = (TypeElement) ((DeclaredType) t).asElement(); } else { curClassElement = null; } } while (curClassElement != null); VelocityContext vc = new VelocityContext(); vc.put("className", className); vc.put("packageName", packageName); vc.put("fields", fields); // Create source file JavaFileObject jfo = processingEnv.getFiler().createSourceFile(qualifiedName + "BeanInfo"); if (verbose) { processingEnv .getMessager() .printMessage(Diagnostic.Kind.NOTE, "creating source file: " + jfo.toUri()); } writer = jfo.openWriter(); template.merge(vc, writer); } finally { closeQuietly(writer); } }
private boolean doesClassContainNoArgsConstructor(Element el) { for (Element subelement : el.getEnclosedElements()) { if (subelement.getKind() == ElementKind.CONSTRUCTOR && subelement.getModifiers().contains(Modifier.PUBLIC)) { TypeMirror mirror = subelement.asType(); if (mirror.accept(noArgsVisitor, null)) return true; } } return false; }
static boolean checkAbstractValueType(Element element, Collection<String> violations) { boolean ofSupportedKind = false || element.getKind() == ElementKind.INTERFACE || element.getKind() == ElementKind.ANNOTATION_TYPE || element.getKind() == ElementKind.CLASS; boolean staticOrTopLevel = false || element.getEnclosingElement().getKind() == ElementKind.PACKAGE || element.getModifiers().contains(Modifier.STATIC); boolean nonFinal = !element.getModifiers().contains(Modifier.FINAL); boolean hasNoTypeParameters = ((TypeElement) element).getTypeParameters().isEmpty(); boolean publicOrPackageVisible = !element.getModifiers().contains(Modifier.PRIVATE) && !element.getModifiers().contains(Modifier.PROTECTED); if (!ofSupportedKind) { violations.add("must be class or interface or annotation type"); } if (!nonFinal) { violations.add("must be non-final"); } if (!hasNoTypeParameters) { violations.add("should have no type parameters"); } if (!publicOrPackageVisible) { violations.add("should be public or package-visible"); } if (!staticOrTopLevel) { violations.add("should be top-level or static inner class"); } return violations.isEmpty(); }
private boolean isInaccessibleViaGeneratedCode( Class<? extends Annotation> annotationClass, String targetThing, Element element) { boolean hasError = false; TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); // Verify 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; }
private void addPersistentMembers( List<? extends Element> membersOfClass, AccessType membersKind) { for (Element memberOfClass : membersOfClass) { AccessType forcedAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(memberOfClass); if (entityAccessTypeInfo.getAccessType() != membersKind && forcedAccessType == null) { continue; } if (TypeUtils.containsAnnotation(memberOfClass, Constants.TRANSIENT) || memberOfClass.getModifiers().contains(Modifier.TRANSIENT) || memberOfClass.getModifiers().contains(Modifier.STATIC)) { continue; } MetaAttributeGenerationVisitor visitor = new MetaAttributeGenerationVisitor(this, context); AnnotationMetaAttribute result = memberOfClass.asType().accept(visitor, memberOfClass); if (result != null) { members.put(result.getPropertyName(), result); } } }
/** @throws IOException */ public List<Api> parse(TypeElement typeElement) throws IOException { RequestMapping requestMapping = typeElement.getAnnotation(RequestMapping.class); if (requestMapping == null) { generator.log("Controller missing @RequestMapping, " + typeElement); return null; } ApiDoc apiDoc = typeElement.getAnnotation(ApiDoc.class); if (apiDoc == null) { generator.log("Controller missing @ApiDoc, " + typeElement); return null; } // printElement(typeElement); List<Api> apiList = Lists.newArrayList(); String controllerName = typeElement.getQualifiedName().toString(); for (Element ele : typeElement.getEnclosedElements()) { if (ele.getKind().equals(ElementKind.METHOD) && ele.getModifiers().contains(Modifier.PUBLIC)) { Api api = new Api(); api.apiDoc = apiDoc; api.packageMapping = requestMapping; api.controllerName = controllerName; ExecutableElement method = (ExecutableElement) ele; this.parseMethod(api, method); if (api.apiMethodDoc != null) { apiList.add(api); } } } return apiList; }
private boolean isValidInnerBuilder(Element t) { ElementKind kind = t.getKind(); if (kind != ElementKind.CLASS && kind != ElementKind.INTERFACE) { protoclass() .report() .withElement(t) .warning( "Inner type %s is %s - not supported as Builder extend/super type", t.getSimpleName(), kind.name().toLowerCase()); return false; } Set<Modifier> modifiers = t.getModifiers(); if (!modifiers.contains(Modifier.STATIC) || modifiers.contains(Modifier.PRIVATE)) { protoclass() .report() .withElement(t) .warning( "Inner type %s should be static non-private to be supported as Builder extend/super type", t.getSimpleName()); return false; } if (kind == ElementKind.CLASS && !hasAccessibleConstructor(t)) { protoclass() .report() .withElement(t) .warning( "%s should have non-private no-argument constructor to be supported as Builder extend/super type", t.getSimpleName()); return false; } return true; }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { if (!roundEnv.processingOver()) { // System.out.println("Processing Annotations: " + annotations); // System.out.println(); for (TypeElement typeElement : annotations) { // System.out.println("Processing: " + typeElement); Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(typeElement); // System.out.println("Processing Elements: " + elements); // System.out.println(); for (Element element : elements) { if (element.getKind() == ElementKind.CLASS && !element.getSimpleName().toString().contains("$") && !element.getModifiers().contains(Modifier.STATIC) && !element.getModifiers().contains(Modifier.PRIVATE)) { processClass(element); } } } } return true; }
// TODO(dpb,gak): Make sure that all cases are covered here. E.g., what if element is public but // enclosed in a package-private element? private static boolean visibleToMembersInjector( MembersInjectionBinding binding, Element element) { return getPackage(element).equals(getPackage(binding.bindingElement())) || element.getModifiers().contains(PUBLIC); }
private boolean isPublicField(Element element) { return element.getModifiers().contains(Modifier.PUBLIC); }
@Override public boolean apply(Element input) { return input.getModifiers().contains(Modifier.PUBLIC); }
/** Returns a map containing all {@code @Provides} methods, indexed by class. */ private Map<String, List<ExecutableElement>> providerMethodsByClass(RoundEnvironment env) { Map<String, List<ExecutableElement>> result = new HashMap<String, List<ExecutableElement>>(); for (Element providerMethod : providesMethods(env)) { TypeElement type = (TypeElement) providerMethod.getEnclosingElement(); Set<Modifier> typeModifiers = type.getModifiers(); if (type.getKind() != ElementKind.CLASS) { // TODO(tbroyer): pass annotation information error("Unexpected @Provides on " + providerMethod, providerMethod); continue; } if (typeModifiers.contains(Modifier.PRIVATE) || typeModifiers.contains(Modifier.ABSTRACT)) { error( "Classes declaring @Provides methods must not be private or abstract: " + type.getQualifiedName(), type); continue; } Set<Modifier> methodModifiers = providerMethod.getModifiers(); if (methodModifiers.contains(Modifier.PRIVATE) || methodModifiers.contains(Modifier.ABSTRACT) || methodModifiers.contains(Modifier.STATIC)) { error( "@Provides methods must not be private, abstract or static: " + type.getQualifiedName() + "." + providerMethod, providerMethod); continue; } ExecutableElement providerMethodAsExecutable = (ExecutableElement) providerMethod; if (!providerMethodAsExecutable.getThrownTypes().isEmpty()) { error( "@Provides methods must not have a throws clause: " + type.getQualifiedName() + "." + providerMethod, providerMethod); continue; } List<ExecutableElement> methods = result.get(type.getQualifiedName().toString()); if (methods == null) { methods = new ArrayList<ExecutableElement>(); result.put(type.getQualifiedName().toString(), methods); } methods.add(providerMethodAsExecutable); } Elements elementUtils = processingEnv.getElementUtils(); TypeMirror objectType = elementUtils.getTypeElement("java.lang.Object").asType(); // Catch any stray modules without @Provides since their entry points // should still be registered and a ModuleAdapter should still be written. for (Element module : env.getElementsAnnotatedWith(Module.class)) { if (!module.getKind().equals(ElementKind.CLASS)) { error("Modules must be classes: " + module, module); continue; } TypeElement moduleType = (TypeElement) module; // Verify that all modules do not extend from non-Object types. if (!moduleType.getSuperclass().equals(objectType)) { error("Modules must not extend from other classes: " + module, module); } String moduleName = moduleType.getQualifiedName().toString(); if (result.containsKey(moduleName)) continue; result.put(moduleName, new ArrayList<ExecutableElement>()); } return result; }
public static boolean hasAnyModifier(final Element element, Modifier... modifiers) { final Set<Modifier> elementModifiers = element.getModifiers(); return hasAnyModifier(elementModifiers, modifiers); }
/** * Returns an integer which results in the appropriate modifier based on the element. * * @param element the element to check the modifiers on * @return an integer representing the modifiers */ public static int of(final Element element) { int result = 0; for (Modifier modifier : element.getModifiers()) { switch (modifier.name()) { case "ABSTRACT": { result = result | ABSTRACT; break; } case "FINAL": { result = result | FINAL; break; } case "NATIVE": { result = result | NATIVE; break; } case "PRIVATE": { result = result | PRIVATE; break; } case "PROTECTED": { result = result | PROTECTED; break; } case "PUBLIC": { result = result | PUBLIC; break; } case "STATIC": { result = result | STATIC; break; } case "STRICTFP": { result = result | STRICTFP; break; } case "SYNCHRONIZED": { result = result | SYNCHRONIZED; break; } case "TRANSIENT": { result = result | TRANSIENT; break; } case "VOLATILE": { result = result | VOLATILE; break; } case "DEFAULT": { result = result | DEFAULT; break; } } } if (element instanceof ExecutableElement && ((ExecutableElement) element).isVarArgs()) { result = result | VARARGS; } return result; }
private boolean isStaticElement(Element element) { return element.getModifiers().contains(Modifier.STATIC); }