private boolean isInnerClass(Element element) { TypeElement typeElement = (TypeElement) element; return typeElement.getNestingKind().isNested(); } }
if (enclosingElement.getNestingKind().isNested()) { if (!enclosingElement.getModifiers().contains(STATIC)) { errorLogger.logError(
/** * finds the symbol for the top-level class containing the given symbol * * @param symbol the given symbol * @return symbol for the non-nested enclosing class */ public static Symbol.ClassSymbol getOutermostClassSymbol(Symbol symbol) { // get the symbol for the outermost enclosing class. this handles // the case of anonymous classes Symbol.ClassSymbol outermostClassSymbol = ASTHelpers.enclosingClass(symbol); while (outermostClassSymbol.getNestingKind().isNested()) { Symbol.ClassSymbol enclosingSymbol = ASTHelpers.enclosingClass(outermostClassSymbol.owner); if (enclosingSymbol != null) { outermostClassSymbol = enclosingSymbol; } else { // enclosingSymbol can be null in weird cases like for array methods break; } } return outermostClassSymbol; }
throw new LayerGenerationException(clazz + " is not public", originatingElement, processingEnv, annotation, annotationMethod); if (((TypeElement) originatingElement).getNestingKind().isNested() && !originatingElement.getModifiers().contains(Modifier.STATIC)) { throw new LayerGenerationException(clazz + " is nested but not static", originatingElement, processingEnv, annotation, annotationMethod);
protected void setGeneratedClass() throws Exception { String annotatedComponentQualifiedName = annotatedElement.getQualifiedName().toString(); annotatedClass = getCodeModel().directClass(annotatedElement.asType().toString()); if (annotatedElement.getNestingKind().isNested()) { Element enclosingElement = annotatedElement.getEnclosingElement(); GeneratedClassHolder enclosingHolder = environment.getGeneratedClassHolder(enclosingElement); String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + classSuffix(); int modifier = PUBLIC | STATIC; if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) { modifier |= FINAL; } generatedClass = enclosingHolder.getGeneratedClass()._class(modifier, generatedBeanSimpleName, EClassType.CLASS); } else { String generatedClassQualifiedName = annotatedComponentQualifiedName + classSuffix(); int modifier = PUBLIC; if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) { modifier |= FINAL; } generatedClass = getCodeModel()._class(modifier, generatedClassQualifiedName, EClassType.CLASS); } codeModelHelper.generify(generatedClass, annotatedElement); setExtends(); codeModelHelper.copyNonAAAnnotations(generatedClass, annotatedElement.getAnnotationMirrors()); }
@Nullable private ClassTree findEnclosingLocalOrAnonymousClass(ClassTree classTree) { Symbol.ClassSymbol symbol = ASTHelpers.getSymbol(classTree); // we need this while loop since we can have a NestingKind.NESTED class (i.e., a nested // class declared at the top-level within its enclosing class) nested (possibly deeply) // within a NestingKind.ANONYMOUS or NestingKind.LOCAL class while (symbol.getNestingKind().isNested()) { if (symbol.getNestingKind().equals(NestingKind.ANONYMOUS) || symbol.getNestingKind().equals(NestingKind.LOCAL)) { return Trees.instance(JavacProcessingEnvironment.instance(context)).getTree(symbol); } else { // symbol.owner is the enclosing element, which could be a class or a method. // if it's a class, the enclClass() method will (surprisingly) return the class itself, // so this works symbol = symbol.owner.enclClass(); } } return null; }
if (!nestingKind.isNested()) { matchWithinClass = !isExcludedClass(classSymbol, state);
if (enclosingClass.getNestingKind().isNested()) { if (!enclosingClass.getModifiers().contains(STATIC)) { logError(
/** Returns whether or not the implementation is nested within another class. */ boolean isNested() { return nestingKind.isNested(); }
private boolean isInnerClass(Element element) { TypeElement typeElement = (TypeElement) element; return typeElement.getNestingKind().isNested(); } }
public Map<String, Element> processConverters(Set<? extends Element> converters) { Map<String, Element> configurations = new LinkedHashMap<String, Element>(); for (Element el : converters) { findConverters(el); if (el instanceof TypeElement) { TypeElement te = (TypeElement) el; if (!el.getModifiers().contains(Modifier.ABSTRACT)) { for (TypeElement it : getTypeHierarchy((TypeElement) el)) { if (Configuration.class.getName().equals(it.toString())) { if (te.getNestingKind().isNested()) { configurations.put(te.getEnclosingElement().asType().toString() + "$" + te.getSimpleName().toString(), te); } else { configurations.put(te.asType().toString(), te); } break; } } } } } return configurations; }
@Override public StringTuple visitType(TypeElement e, Boolean aBoolean) { String name = e.getQualifiedName().toString(); if (!e.getModifiers().contains(Modifier.ABSTRACT) && name.startsWith("io.github.nucleuspowered.nucleus.modules")) { if (aBoolean) { // Only care about the package here. return new StringTuple(name.replaceAll("\\.[^.]+?$", "\\."), name); } else if (e.getNestingKind().isNested()) { return new StringTuple(null, name.replaceAll("\\.([^.]+?)$", "\\$$1")); } return new StringTuple(null, name); } return null; }
/** * The module constructor being proxied. A proxy is generated if it is not publicly accessible and * has no arguments. If an implicit reference to the enclosing class exists, or the module is * abstract, no proxy method can be generated. */ // TODO(ronshapiro): make this an @Injectable class that injects DaggerElements static Optional<ExecutableElement> nonPublicNullaryConstructor( TypeElement moduleElement, DaggerElements elements) { ModuleKind.checkIsModule(moduleElement); if (moduleElement.getModifiers().contains(ABSTRACT) || (moduleElement.getNestingKind().isNested() && !moduleElement.getModifiers().contains(STATIC))) { return Optional.empty(); } return constructorsIn(elements.getAllMembers(moduleElement)).stream() .filter(constructor -> !Accessibility.isElementPubliclyAccessible(constructor)) .filter(constructor -> !constructor.getModifiers().contains(PRIVATE)) .filter(constructor -> constructor.getParameters().isEmpty()) .findAny(); }
@Nullable private String validateDeserializeAs(TypeElement source, TypeElement target) { if (target.getModifiers().contains(Modifier.PRIVATE)) { return "can't be private"; } else if (requiresPublic(target) && !target.getModifiers().contains(Modifier.PUBLIC)) { return "must be public"; } else if (target.getNestingKind().isNested() && !target.getModifiers().contains(Modifier.STATIC)) { return "can't be a nested member. Only public static nested classes are supported"; } else if (target.getNestingKind().isNested() && !target.getModifiers().contains(Modifier.PUBLIC)) { return "must be public when nested in another class"; } else if (onlyBasicFeatures && (target.getQualifiedName().contentEquals(target.getSimpleName()) || target.getNestingKind().isNested() && target.getModifiers().contains(Modifier.STATIC) && target.getEnclosingElement() instanceof TypeElement && ((TypeElement) target.getEnclosingElement()).getQualifiedName().contentEquals(target.getEnclosingElement().getSimpleName()))) { return "is defined without a package name and cannot be accessed"; } else if (target.getKind() == ElementKind.INTERFACE || target.getModifiers().contains(Modifier.ABSTRACT)) { return "must be a concrete type"; } else if (!source.asType().toString().equals(target.asType().toString()) && source.getKind() != ElementKind.INTERFACE && !source.getModifiers().contains(Modifier.ABSTRACT)) { return "can only be specified for interfaces and abstract classes. '" + source + "' is neither interface nor abstract class"; } else if (!types.isAssignable(target.asType(), source.asType())) { return "is not assignable to '" + source.getQualifiedName() + "'"; } else { return null; } }
boolean valid(final TypeElement spec, final Element loc) { { final Set<Modifier> modifiers = spec.getModifiers(); if (!modifiers.contains(PUBLIC) || modifiers.contains(FINAL)) return error("Not a public and non-final class or interface.", loc); if (spec.getNestingKind().isNested()) { if (!modifiers.contains(STATIC)) return error("Impossible to implement outside of the lexical scope of the enclosing class.", loc); warning("Bad practice: Not a top-level class or interface.", loc); } } final Collection<ExecutableElement> ctors = new LinkedList<ExecutableElement>(); for (final Element elem : spec.getEnclosedElements()) if (elem.getKind() == CONSTRUCTOR) ctors.add((ExecutableElement) elem); return ctors.isEmpty() || valid(ctors) || error("No public or protected constructor with zero parameters available.", loc); }
@Override public String getPackage(TypeElement element) { while ( element.getNestingKind().isNested() ) { Element enclosingElement = element.getEnclosingElement(); if ( enclosingElement instanceof TypeElement ) { element = (TypeElement)enclosingElement; } else { break; } } return element.getEnclosingElement().toString(); }
private void validateClass(final TypeElement clazz) { final Set<Modifier> modifiers = clazz.getModifiers(); if (modifiers.contains(FINAL)) { error("A neuron class must not be final.", clazz); } if (clazz.getNestingKind().isNested()) { if (!modifiers.contains(STATIC)) { error("A neuron class must be static.", clazz); } } if (!hasNonPrivateConstructorWithoutParameters(clazz)) { error("A neuron class must have a non-private constructor without parameters.", clazz); } }
/** * Returns the top-level type element enclosing {@code element}. */ @Requires({ "element != null", "element.getKind() != ElementKind.PACKAGE", "element.getKind() != ElementKind.OTHER" }) @Ensures("element != null") protected static TypeElement getRootElement(Element element) { if (element.getKind().isClass() || element.getKind().isInterface()) { TypeElement type = (TypeElement) element; if (!type.getNestingKind().isNested()) { return type; } } return getRootElement(element.getEnclosingElement()); } }
protected void validateConverter(TypeElement convElement) { if (!ctx.getTypes().isAssignable(convElement.asType(), DomainConverter.class)) { throw new AptException(Message.DOMA4191, convElement, new Object[] {}); } if (convElement.getNestingKind().isNested()) { throw new AptException(Message.DOMA4198, convElement, new Object[] {}); } if (convElement.getModifiers().contains(Modifier.ABSTRACT)) { throw new AptException(Message.DOMA4192, convElement, new Object[] {}); } ExecutableElement constructor = ctx.getElements().getNoArgConstructor(convElement); if (constructor == null || !constructor.getModifiers().contains(Modifier.PUBLIC)) { throw new AptException(Message.DOMA4193, convElement, new Object[] {}); } }
protected void validateInterface(TypeElement interfaceElement, DaoMeta daoMeta) { if (!interfaceElement.getKind().isInterface()) { DaoAnnot daoAnnot = daoMeta.getDaoAnnot(); throw new AptException( Message.DOMA4014, interfaceElement, daoAnnot.getAnnotationMirror(), new Object[] {}); } if (interfaceElement.getNestingKind().isNested()) { throw new AptException(Message.DOMA4017, interfaceElement, new Object[] {}); } if (!interfaceElement.getTypeParameters().isEmpty()) { throw new AptException(Message.DOMA4059, interfaceElement, new Object[] {}); } }