Java Code Examples for javax.lang.model.element.TypeElement#getEnclosingElement()
The following examples show how to use
javax.lang.model.element.TypeElement#getEnclosingElement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TurbineElementTest.java From turbine with Apache License 2.0 | 6 votes |
@Test public void typeElement() { TypeElement e = factory.typeElement(new ClassSymbol("java/util/Map$Entry")); TypeElement m = (TypeElement) e.getEnclosingElement(); TypeMirror t = e.asType(); assertThat(e.getSimpleName().toString()).isEqualTo("Entry"); assertThat(e.getQualifiedName().toString()).isEqualTo("java.util.Map.Entry"); assertThat(e.toString()).isEqualTo("java.util.Map.Entry"); assertThat(e.asType().toString()).isEqualTo("java.util.Map.Entry<K,V>"); assertThat(e.getKind()).isEqualTo(ElementKind.INTERFACE); assertThat(e.getNestingKind()).isEqualTo(NestingKind.MEMBER); assertThat(e.getModifiers()) .containsExactly(Modifier.PUBLIC, Modifier.ABSTRACT, Modifier.STATIC); assertThat(m.getSimpleName().toString()).isEqualTo("Map"); assertThat(m.getSuperclass().getKind()).isEqualTo(TypeKind.NONE); assertThat(m.getQualifiedName().toString()).isEqualTo("java.util.Map"); assertThat(m.toString()).isEqualTo("java.util.Map"); assertThat(m.asType().toString()).isEqualTo("java.util.Map<K,V>"); assertThat(m.getNestingKind()).isEqualTo(NestingKind.TOP_LEVEL); assertThat(m.getSuperclass().getKind()).isEqualTo(TypeKind.NONE); assertThat(m.getEnclosingElement().getKind()).isEqualTo(ElementKind.PACKAGE); assertThat(t.getKind()).isEqualTo(TypeKind.DECLARED); }
Example 2
Source File: QualifiedName.java From FreeBuilder with Apache License 2.0 | 6 votes |
/** * Returns a {@link QualifiedName} for {@code type}. */ public static QualifiedName of(TypeElement type) { switch (type.getNestingKind()) { case TOP_LEVEL: PackageElement pkg = (PackageElement) type.getEnclosingElement(); return QualifiedName.of(pkg.getQualifiedName().toString(), type.getSimpleName().toString()); case MEMBER: List<String> reversedNames = new ArrayList<String>(); reversedNames.add(type.getSimpleName().toString()); Element parent = type.getEnclosingElement(); while (parent.getKind() != ElementKind.PACKAGE) { reversedNames.add(parent.getSimpleName().toString()); parent = parent.getEnclosingElement(); } return new QualifiedName( ((PackageElement) parent).getQualifiedName().toString(), ImmutableList.copyOf(Lists.reverse(reversedNames))); default: throw new IllegalArgumentException("Cannot determine qualified name of " + type); } }
Example 3
Source File: ProcessorImpl.java From takari-lifecycle with Eclipse Public License 1.0 | 6 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element element : roundEnv.getElementsAnnotatedWith(Annotation.class)) { try { TypeElement cls = (TypeElement) element; PackageElement pkg = (PackageElement) cls.getEnclosingElement(); String clsSimpleName = getGeneratedClassName(cls, prefix); String pkgName = pkg.getQualifiedName().toString(); FileObject sourceFile = createFile(pkgName, clsSimpleName, element); BufferedWriter w = new BufferedWriter(sourceFile.openWriter()); try { w.append("package ").append(pkgName).append(";"); w.newLine(); appendClassAnnotations(w); w.append("public class ").append(clsSimpleName); appendBody(pkgName, clsSimpleName, w); } finally { w.close(); } } catch (IOException e) { e.printStackTrace(); processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), element); } } return false; // not "claimed" so multiple processors can be tested }
Example 4
Source File: MyAnnotationProcessor.java From takari-lifecycle with Eclipse Public License 1.0 | 5 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { try { TypeElement cls = (TypeElement) element; PackageElement pkg = (PackageElement) cls.getEnclosingElement(); String clsSimpleName = "My" + cls.getSimpleName(); String clsQualifiedName = pkg.getQualifiedName() + "." + clsSimpleName; JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(clsQualifiedName, element); OutputStream ios = sourceFile.openOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(ios, "UTF-8"); BufferedWriter w = new BufferedWriter(writer); try { w.append("package ").append(pkg.getQualifiedName()).append(";"); w.newLine(); w.append("public class ").append(clsSimpleName).append(" { }"); } finally { w.close(); writer.close(); ios.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; }
Example 5
Source File: ELHyperlinkProvider.java From netbeans with Apache License 2.0 | 5 votes |
Void printType(TypeElement e, DeclaredType dt, Boolean highlightName) { modifier(e.getModifiers()); switch (e.getKind()) { case CLASS: result.append("class "); break; case INTERFACE: result.append("interface "); break; case ENUM: result.append("enum "); break; case ANNOTATION_TYPE: result.append("@interface "); break; default: break; } Element enclosing = e.getEnclosingElement(); if (enclosing == info.getElementUtilities().enclosingTypeElement(e)) { result.append(((TypeElement) enclosing).getQualifiedName()); result.append('.'); boldStartCheck(highlightName); result.append(e.getSimpleName()); boldStopCheck(highlightName); } else { result.append(e.getQualifiedName()); } if (dt != null) dumpRealTypeArguments(dt.getTypeArguments()); return null; }
Example 6
Source File: ExtensionAnnotationProcessor.java From quarkus with Apache License 2.0 | 5 votes |
private StringBuilder getRelativeBinaryName(TypeElement te, StringBuilder b) { final Element enclosing = te.getEnclosingElement(); if (enclosing instanceof TypeElement) { getRelativeBinaryName((TypeElement) enclosing, b); b.append('$'); } b.append(te.getSimpleName()); return b; }
Example 7
Source File: InferredPackageElementTest.java From buck with Apache License 2.0 | 5 votes |
@Test public void testToStringSimpleNamePackage() throws IOException { compile(Joiner.on('\n').join("public class Foo extends pkg.Bar.Baz {", "}")); DeclaredType superclass = (DeclaredType) elements.getTypeElement("Foo").getSuperclass(); TypeElement superclassElement = (TypeElement) superclass.asElement(); TypeElement superclassEnclosingTypeElement = (TypeElement) superclassElement.getEnclosingElement(); PackageElement element = (InferredPackageElement) superclassEnclosingTypeElement.getEnclosingElement(); assertEquals("pkg", element.toString()); }
Example 8
Source File: StoreInitProcessor.java From core with GNU Lesser General Public License v2.1 | 5 votes |
@Override protected boolean onProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element e : roundEnv.getElementsAnnotatedWith(Store.class)) { TypeElement storeElement = (TypeElement) e; PackageElement packageElement = (PackageElement) storeElement.getEnclosingElement(); String packageName = packageElement.getQualifiedName().toString(); String storeDelegate = storeElement.getSimpleName().toString(); String storeAdapter = storeDelegate + ADAPTER_SUFFIX; StoreInfo storeInfo = new StoreInfo(packageName, storeDelegate, storeAdapter); storeInfos.add(storeInfo); debug("Discovered store / store adapter [%s]", storeInfo); } // Don't generate files in onLastRound, since the generated GIN module // needs to be picked up by the GinProcessor! if (!storeInfos.isEmpty()) { debug("Generating code for store ginjector"); code(STORE_GINJECTOR_TEMPLATE, STORE_GINJECTOR_PACKAGE, STORE_GINJECTOR_CLASS, context(STORE_GINJECTOR_PACKAGE, STORE_GINJECTOR_CLASS)); debug("Generating code for store module"); code(STORE_MODULE_TEMPLATE, STORE_MODULE_PACKAGE, STORE_MODULE_CLASS, context(STORE_MODULE_PACKAGE, STORE_MODULE_CLASS)); info("Successfully generated store initialization classes [%s] and [%s].", STORE_GINJECTOR_CLASS, STORE_MODULE_CLASS); storeInfos.clear(); } return false; }
Example 9
Source File: GeneratedTypeElement.java From j2objc with Apache License 2.0 | 5 votes |
public static GeneratedTypeElement mutableCopy(TypeElement element) { GeneratedTypeElement generatedTypeElement = new GeneratedTypeElement( element.getSimpleName().toString(), element.getKind(), element.getEnclosingElement(), element.getSuperclass(), element.getNestingKind(), ElementUtil.getHeader(element), ElementUtil.isIosType(element), ElementUtil.isSynthetic(element)); generatedTypeElement.addModifiers(element.getModifiers()); generatedTypeElement.addAnnotationMirrors(element.getAnnotationMirrors()); return generatedTypeElement; }
Example 10
Source File: ApNavigator.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }
Example 11
Source File: ApNavigator.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }
Example 12
Source File: SystemConfigFactoryGenerator.java From qpid-broker-j with Apache License 2.0 | 4 votes |
private String generateObjectFactory(final Filer filer, final ExecutableElement constructorElement) { TypeElement classElement = (TypeElement) constructorElement.getEnclosingElement(); String factoryName = classElement.getQualifiedName().toString() + "Factory"; String factorySimpleName = classElement.getSimpleName().toString() + "Factory"; String objectSimpleName = classElement.getSimpleName().toString(); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generating factory file for " + classElement.getQualifiedName().toString()); PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); try { JavaFileObject factoryFile = filer.createSourceFile(factoryName); PrintWriter pw = new PrintWriter(new OutputStreamWriter(factoryFile.openOutputStream(), "UTF-8")); pw.println("/*"); for(String headerLine : License.LICENSE) { pw.println(" *" + headerLine); } pw.println(" */"); pw.println(); pw.print("package "); pw.print(packageElement.getQualifiedName()); pw.println(";"); pw.println(); pw.println("import java.security.Principal;"); pw.println("import java.util.Map;"); pw.println(); pw.println("import org.apache.qpid.server.configuration.updater.TaskExecutor;"); pw.println("import org.apache.qpid.server.logging.EventLogger;"); pw.println("import org.apache.qpid.server.model.ConfiguredObjectTypeRegistry;"); pw.println("import org.apache.qpid.server.model.SystemConfig;"); pw.println("import org.apache.qpid.server.plugin.PluggableService;"); pw.println("import org.apache.qpid.server.plugin.SystemConfigFactory;"); pw.println(); pw.println("@PluggableService"); pw.println("public final class " + factorySimpleName + " implements SystemConfigFactory<"+ objectSimpleName +">"); pw.println("{"); pw.println(" public " + factorySimpleName + "()"); pw.println(" {"); pw.println(" }"); pw.println(); pw.println(" @Override"); pw.println(" public final String getType()"); pw.println(" {"); pw.println(" return ConfiguredObjectTypeRegistry.getType(" + objectSimpleName + ".class);"); pw.println(" }"); pw.println(); pw.println(" @Override"); pw.println(" public "+objectSimpleName+" newInstance(final TaskExecutor taskExecutor,"); pw.println(" final EventLogger eventLogger,"); pw.println(" final Principal systemPrincipal,"); pw.println(" final Map<String,Object> attributes)"); pw.println(" {"); pw.println(" return new "+objectSimpleName+"(taskExecutor, eventLogger, systemPrincipal, attributes);"); pw.println(" }"); pw.println("}"); pw.close(); } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write factory file: " + factoryName + " - " + e.getLocalizedMessage()); } return factoryName; }
Example 13
Source File: ApNavigator.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }
Example 14
Source File: CompositeTypeConstructorGenerator.java From qpid-broker-j with Apache License 2.0 | 4 votes |
private void generateCompositeTypeConstructor(final Filer filer, final TypeElement typeElement) { String objectQualifiedClassName = typeElement.getQualifiedName().toString(); String objectSimpleName = typeElement.getSimpleName().toString(); String compositeTypeConstructorNameSimpleName = objectSimpleName + "Constructor"; PackageElement packageElement = (PackageElement) typeElement.getEnclosingElement(); final String compositeTypeConstructorPackage = packageElement.getQualifiedName() + ".codec"; String compositeTypeConstructorName = compositeTypeConstructorPackage + "." + compositeTypeConstructorNameSimpleName; final CompositeType annotation = typeElement.getAnnotation(CompositeType.class); processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generating composite constructor file for " + objectQualifiedClassName); try { JavaFileObject factoryFile = filer.createSourceFile(compositeTypeConstructorName); PrintWriter pw = new PrintWriter(new OutputStreamWriter(factoryFile.openOutputStream(), "UTF-8")); pw.println("/*"); for(String headerLine : License.LICENSE) { pw.println(" *" + headerLine); } pw.println(" */"); pw.println(); pw.print("package "); pw.print(compositeTypeConstructorPackage); pw.println(";"); pw.println(); pw.println("import java.util.List;"); pw.println(); pw.println("import org.apache.qpid.server.protocol.v1_0.codec.AbstractCompositeTypeConstructor;"); pw.println("import org.apache.qpid.server.protocol.v1_0.codec.DescribedTypeConstructorRegistry;"); pw.println("import org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException;"); pw.println("import org.apache.qpid.server.protocol.v1_0.type.Symbol;"); pw.println("import org.apache.qpid.server.protocol.v1_0.type.UnsignedLong;"); pw.println("import org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError;"); pw.println("import org.apache.qpid.server.protocol.v1_0.type.transport.Error;"); pw.println("import " + objectQualifiedClassName + ";"); pw.println(); pw.println("public final class " + compositeTypeConstructorNameSimpleName + " extends AbstractCompositeTypeConstructor<"+ objectSimpleName +">"); pw.println("{"); pw.println(" private static final " + compositeTypeConstructorNameSimpleName + " INSTANCE = new " + compositeTypeConstructorNameSimpleName + "();"); pw.println(); pw.println(" public static void register(DescribedTypeConstructorRegistry registry)"); pw.println(" {"); pw.println(" registry.register(Symbol.valueOf(\"" + annotation.symbolicDescriptor() + "\"), INSTANCE);"); pw.println(String.format(" registry.register(UnsignedLong.valueOf(%#016x), INSTANCE);", annotation.numericDescriptor())); pw.println(" }"); pw.println(); pw.println(" @Override"); pw.println(" protected String getTypeName()"); pw.println(" {"); pw.println(" return " + objectSimpleName + ".class.getSimpleName();"); pw.println(" }"); pw.println(); pw.println(" @Override"); pw.println(" protected " + objectSimpleName + " construct(final FieldValueReader fieldValueReader) throws AmqpErrorException"); pw.println(" {"); pw.println(" " + objectSimpleName + " obj = new " + objectSimpleName + "();"); pw.println(); generateAssigners(pw, typeElement); pw.println(" return obj;"); pw.println(" }"); pw.println("}"); pw.close(); } catch (IOException e) { processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write composite constructor file: " + compositeTypeConstructorName + " - " + e.getLocalizedMessage()); } }
Example 15
Source File: WebServiceVisitor.java From hottub with GNU General Public License v2.0 | 4 votes |
protected boolean isLegalImplementation(WebService webService, TypeElement classElement) { boolean isStateful = isStateful(classElement); Collection<Modifier> modifiers = classElement.getModifiers(); if (!modifiers.contains(Modifier.PUBLIC)) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_NOT_PUBLIC(classElement.getQualifiedName()), classElement); return false; } if (modifiers.contains(Modifier.FINAL) && !isStateful) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_FINAL(classElement.getQualifiedName()), classElement); return false; } if (modifiers.contains(Modifier.ABSTRACT) && !isStateful) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_ABSTRACT(classElement.getQualifiedName()), classElement); return false; } boolean hasDefaultConstructor = false; for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) { if (constructor.getModifiers().contains(Modifier.PUBLIC) && constructor.getParameters().isEmpty()) { hasDefaultConstructor = true; break; } } if (!hasDefaultConstructor && !isStateful) { if (classElement.getEnclosingElement() != null && !modifiers.contains(Modifier.STATIC)) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_INNERCLASS_NOT_STATIC( classElement.getQualifiedName()), classElement); return false; } builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_NO_DEFAULT_CONSTRUCTOR( classElement.getQualifiedName()), classElement); return false; } if (webService.endpointInterface().isEmpty()) { if (!methodsAreLegal(classElement)) return false; } else { TypeElement interfaceElement = getEndpointInterfaceElement(webService.endpointInterface(), classElement); if (!classImplementsSei(classElement, interfaceElement)) return false; } return true; }
Example 16
Source File: ApNavigator.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }
Example 17
Source File: ApNavigator.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }
Example 18
Source File: WebServiceVisitor.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
protected boolean isLegalImplementation(WebService webService, TypeElement classElement) { boolean isStateful = isStateful(classElement); Collection<Modifier> modifiers = classElement.getModifiers(); if (!modifiers.contains(Modifier.PUBLIC)) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_NOT_PUBLIC(classElement.getQualifiedName()), classElement); return false; } if (modifiers.contains(Modifier.FINAL) && !isStateful) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_FINAL(classElement.getQualifiedName()), classElement); return false; } if (modifiers.contains(Modifier.ABSTRACT) && !isStateful) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_ABSTRACT(classElement.getQualifiedName()), classElement); return false; } boolean hasDefaultConstructor = false; for (ExecutableElement constructor : ElementFilter.constructorsIn(classElement.getEnclosedElements())) { if (constructor.getModifiers().contains(Modifier.PUBLIC) && constructor.getParameters().isEmpty()) { hasDefaultConstructor = true; break; } } if (!hasDefaultConstructor && !isStateful) { if (classElement.getEnclosingElement() != null && !modifiers.contains(Modifier.STATIC)) { builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_CLASS_IS_INNERCLASS_NOT_STATIC( classElement.getQualifiedName()), classElement); return false; } builder.processError(WebserviceapMessages.WEBSERVICEAP_WEBSERVICE_NO_DEFAULT_CONSTRUCTOR( classElement.getQualifiedName()), classElement); return false; } if (webService.endpointInterface().isEmpty()) { if (!methodsAreLegal(classElement)) return false; } else { TypeElement interfaceElement = getEndpointInterfaceElement(webService.endpointInterface(), classElement); if (!classImplementsSei(classElement, interfaceElement)) return false; } return true; }
Example 19
Source File: ElementCheck.java From stitch with Apache License 2.0 | 4 votes |
static void checkTopLevel(TypeElement e) { Element element = e.getEnclosingElement(); if (element.getKind() != ElementKind.PACKAGE) { throw new IllegalStateException("@Component error: " + e.getQualifiedName() + " can not be inner class!"); } }
Example 20
Source File: ApNavigator.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
public boolean isInnerClass(TypeElement clazz) { return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC); }