private boolean isInnerClass(Element element) { TypeElement typeElement = (TypeElement) element; return typeElement.getNestingKind().isNested(); } }
@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; }
/** Is t an anonymous inner class or a lambda? */ private boolean isValidTreeType(Tree t) { if (t instanceof LambdaExpressionTree) { return true; } if (t instanceof ClassTree) { NestingKind nestingKind = ASTHelpers.getSymbol((ClassTree) t).getNestingKind(); return nestingKind.equals(NestingKind.ANONYMOUS) || nestingKind.equals(NestingKind.LOCAL); } return false; } }
@Override public String getNesting() { return type.getNestingKind().toString(); }
@Pure @Override public @Nonnull Contract generateContract(@Nonnull Element element, @Nonnull AnnotationMirror annotationMirror, @NonCaptured @Modified @Nonnull TypeImporter typeImporter) { return Contract.with("# == null || " + getCondition(element, getKind(), typeImporter), "The # has to be null or have the nesting kind '" + getKind().name().toLowerCase().replace("_", "-") + "' but was $.", element); }
public static boolean isTopLevelType(Element e) { if (!ElementKind.CLASS.equals(e.getKind())) { return false; } TypeElement te = (TypeElement)e; if (!NestingKind.TOP_LEVEL.equals(te.getNestingKind())) { return false; } return true; } }
public StringBuilder visitType(TypeElement t, Void _) { ClassTree ct = trees.getTree(t); if(ct ==null) return new StringBuilder(); // not a part of compiled source files. return a bogus value switch(t.getNestingKind()) { case ANONYMOUS: String binaryName = elements.getBinaryName(t).toString(); int idx = binaryName.lastIndexOf('$'); String name = "~"+binaryName.substring(idx); // #$1 is ambiguous between field and anonyous type, so use '~' as the prefix for type return combine(getEnclosingTypeOrPackage(t).accept(this,null)).append(name); case TOP_LEVEL: // check if this class is the 'primary type' of the compilation unit CompilationUnitTree owner = pss.getCompilationUnitOf(ct); String primaryTypeName = TreeUtil.getPrimaryTypeName(owner); String simpleName = ct.getSimpleName().toString(); StringBuilder buf = new StringBuilder(); if(!primaryTypeName.equals(simpleName)) { buf.append("~").append(simpleName); } return buf; case MEMBER: case LOCAL: return recurse(t).append('~').append(t.getSimpleName()); default: throw new IllegalStateException(t.getNestingKind().toString()); } }
/** * Returns the condition for the given element depending on the type of the element. */ @Pure public static @Nonnull String getCondition(@Nonnull Element element, @Nonnull NestingKind kind, @NonCaptured @Modified @Nonnull TypeImporter typeImporter) { if (ProcessingUtility.isRawSubtype(element, Class.class)) { switch (kind) { case ANONYMOUS: return "#.isAnonymousClass()"; case LOCAL: return "#.isLocalClass()"; case MEMBER: return "#.isMemberClass()"; case TOP_LEVEL: return "!#.isAnonymousClass() && !#.isLocalClass() && !#.isMemberClass()"; default: return "false"; } } else { return "#.getNestingKind() == " + typeImporter.importIfPossible(NestingKind.class) + "." + kind.name(); } }
if (enclosingElement.getNestingKind().isNested()) { if (!enclosingElement.getModifiers().contains(STATIC)) { errorLogger.logError(
"A pljava function must not have a non-public " + "enclosing class"); if ( ((TypeElement)ee).getNestingKind().equals( NestingKind.TOP_LEVEL) ) break;
/** * 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()); }
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; }