javax.lang.model.element.Element#getEnclosingElement ( )源码实例Demo

下面列出了javax.lang.model.element.Element#getEnclosingElement ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: alchemy   文件: RelationRule.java
@Override
public void process(Element element) throws Exception {
    final Element enclosingElement = element.getEnclosingElement();
    if (enclosingElement.getAnnotation(Entry.class) == null) {
        throw new ElementException("Class containing @Relation must be annotated with @Entry", enclosingElement);
    }
    element.asType().accept(new SimpleTypeVisitor7<Void, Void>() {
        @Override
        public Void visitDeclared(DeclaredType t, Void unused) {
            final List<? extends TypeMirror> args = t.getTypeArguments();
            if (args.isEmpty()) {
                processOneToOne(element, t.asElement());
            } else {
                processOneToMany(element, (TypeElement) t.asElement(), args.get(0));
            }
            return super.visitDeclared(t, unused);
        }
    }, null);
}
 
源代码2 项目: netbeans   文件: LoggerNotStaticFinal.java
@TriggerPatterns({
    @TriggerPattern(value="$mods$ java.util.logging.Logger $LOG;"), //NOI18N
    @TriggerPattern(value="$mods$ java.util.logging.Logger $LOG = $init;") //NOI18N
})
public static ErrorDescription checkLoggerDeclaration(HintContext ctx) {
    Element e = ctx.getInfo().getTrees().getElement(ctx.getPath());
    if (e != null && e.getEnclosingElement().getKind() == ElementKind.CLASS &&
        (!e.getModifiers().contains(Modifier.STATIC) || !e.getModifiers().contains(Modifier.FINAL)) &&
        ctx.getInfo().getElementUtilities().outermostTypeElement(e) == e.getEnclosingElement()
    ) {
        return ErrorDescriptionFactory.forName(
                ctx,
                ctx.getPath(),
                NbBundle.getMessage(LoggerNotStaticFinal.class, "MSG_LoggerNotStaticFinal_checkLoggerDeclaration", e), //NOI18N
                new LoggerNotStaticFinalFix(NbBundle.getMessage(LoggerNotStaticFinal.class, "MSG_LoggerNotStaticFinal_checkLoggerDeclaration_fix", e), TreePathHandle.create(e, ctx.getInfo())).toEditorFix() //NOI18N
        );
    } else {
        return null;
    }
}
 
源代码3 项目: quarkus   文件: ExtensionAnnotationProcessor.java
private TypeElement getClassOf(Element e) {
    Element t = e;
    while (!(t instanceof TypeElement)) {
        if (t == null) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Element " + e + " has no enclosing class");
            return null;
        }
        t = t.getEnclosingElement();
    }
    return (TypeElement) t;
}
 
源代码4 项目: Kratos   文件: KratosProcessor.java
private void parseBindText(Element element, Map<TypeElement, BindingClass> targetClassMap,
                           Set<String> erasedTargetNames) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

    TypeMirror elementType = element.asType();
    if (elementType.getKind() == TypeKind.TYPEVAR) {
        TypeVariable typeVariable = (TypeVariable) elementType;
        elementType = typeVariable.getUpperBound();
    }
    // Assemble information on the field.
    int[] ids = element.getAnnotation(BindText.class).value();
    BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
    for (int id : ids) {
        if (bindingClass != null) {
            KBindings bindings = bindingClass.getKBindings(String.valueOf(id));
            if (bindings != null) {
                Iterator<FieldViewBinding> iterator = bindings.getFieldBindings().iterator();
                if (iterator.hasNext()) {
                    FieldViewBinding existingBinding = iterator.next();
                    error(element, "Attempt to use @%s for an already bound ID %s on '%s'. (%s.%s)",
                            BindText.class.getSimpleName(), id, existingBinding.getName(),
                            enclosingElement.getQualifiedName(), element.getSimpleName());
                    return;
                }
            }
        } else {
            bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
        }
        String name = element.getSimpleName().toString();
        TypeName type = TypeName.get(elementType);
        boolean required = isRequiredBinding(element);

        FieldViewBinding binding = new FieldViewBinding(name, type, required);
        bindingClass.addField(String.valueOf(id), binding);
    }

    // Add the type-erased version to the valid binding targets set.
    erasedTargetNames.add(enclosingElement.toString());
}
 
源代码5 项目: jdk8u60   文件: Checker.java
private Element getEnclosingPackageOrClass(Element e) {
    while (e != null) {
        switch (e.getKind()) {
            case CLASS:
            case ENUM:
            case INTERFACE:
            case PACKAGE:
                return e;
            default:
                e = e.getEnclosingElement();
        }
    }
    return e;
}
 
源代码6 项目: butterknife   文件: ButterKnifeProcessor.java
private void parseResourceInt(Element element,
    Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
  boolean hasError = false;
  TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

  // Verify that the target type is int.
  if (element.asType().getKind() != TypeKind.INT) {
    error(element, "@%s field type must be 'int'. (%s.%s)", BindInt.class.getSimpleName(),
        enclosingElement.getQualifiedName(), element.getSimpleName());
    hasError = true;
  }

  // Verify common generated code restrictions.
  hasError |= isInaccessibleViaGeneratedCode(BindInt.class, "fields", element);
  hasError |= isBindingInWrongPackage(BindInt.class, element);

  if (hasError) {
    return;
  }

  // Assemble information on the field.
  String name = element.getSimpleName().toString();
  int id = element.getAnnotation(BindInt.class).value();
  Id resourceId = elementToId(element, BindInt.class, id);
  BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
  builder.addResource(
      new FieldResourceBinding(resourceId, name, FieldResourceBinding.Type.INT));

  erasedTargetNames.add(enclosingElement);
}
 
源代码7 项目: picocli   文件: AnnotatedCommandSourceGenerator.java
private static String extractPackageName(TypeElement typeElement) {
    Element enclosing = typeElement.getEnclosingElement();
    while (enclosing != null) {
        if (enclosing instanceof PackageElement) {
            PackageElement pkg = (PackageElement) enclosing;
            if (pkg.isUnnamed()) {
                return "";
            }
            String fqcn = pkg.getQualifiedName().toString();
            return fqcn;
        }
        enclosing = enclosing.getEnclosingElement();
    }
    return "";
}
 
源代码8 项目: openjdk-8-source   文件: NoPrivateTypesExported.java
private TypeElement outermostTypeElement(Element el) {
    while (el.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
        el = el.getEnclosingElement();
    }

    return (TypeElement) el;
}
 
源代码9 项目: openjdk-jdk9   文件: LocalInAnonymous.java
@Override
public Void visitMethod(MethodTree node, Void p) {
    Element prevOwner = currentOwner;
    try {
        Element currentElement = trees.getElement(getCurrentPath());
        if (currentElement.getEnclosingElement() != currentOwner) {
            throw new AssertionError("Unexpected owner!");
        }
        currentOwner = currentElement;
        return super.visitMethod(node, p);
    } finally {
        currentOwner = prevOwner;
    }
}
 
源代码10 项目: qpid-broker-j   文件: AttributeFieldValidation.java
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
{
    Elements elementUtils = processingEnv.getElementUtils();
    Types typeUtils = processingEnv.getTypeUtils();

    TypeElement annotationElement = elementUtils.getTypeElement(MANAGED_ATTRIBUTE_FIELD_CLASS_NAME);
    for (Element e : roundEnv.getElementsAnnotatedWith(annotationElement))
    {
        for(AnnotationMirror am : e.getAnnotationMirrors())
        {
            if(typeUtils.isSameType(am.getAnnotationType(), annotationElement.asType()))
            {
                for(Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : am.getElementValues().entrySet())
                {
                    String elementName = entry.getKey().getSimpleName().toString();
                    if(elementName.equals("beforeSet") || elementName.equals("afterSet"))
                    {
                        String methodName = entry.getValue().getValue().toString();
                        if(!"".equals(methodName))
                        {
                            TypeElement parent = (TypeElement) e.getEnclosingElement();
                            if(!containsMethod(parent, methodName))
                            {
                                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
                                                                         "Could not find method '"
                                                                         + methodName
                                                                         + "' which is defined as the "
                                                                         + elementName
                                                                         + " action", e);

                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
源代码11 项目: gama   文件: ProcessorContext.java
private String getRootClassOf(final Element e) {
	final ElementKind kind = e.getKind();
	final Element enclosing = e.getEnclosingElement();
	final ElementKind enclosingKind = enclosing.getKind();
	if ((kind == ElementKind.CLASS || kind == ElementKind.INTERFACE)
			&& !(enclosingKind == ElementKind.CLASS || enclosingKind == ElementKind.INTERFACE)) {
		return e.toString();
	}
	return getRootClassOf(enclosing);
}
 
源代码12 项目: ViewFinder   文件: ViewFinderProcesser.java
private AnnotatedClass getAnnotatedClass(Element element) {
    TypeElement classElement = (TypeElement) element.getEnclosingElement();
    String fullClassName = classElement.getQualifiedName().toString();
    AnnotatedClass annotatedClass = mAnnotatedClassMap.get(fullClassName);
    if (annotatedClass == null) {
        annotatedClass = new AnnotatedClass(classElement, mElementUtils);
        mAnnotatedClassMap.put(fullClassName, annotatedClass);
    }
    return annotatedClass;
}
 
源代码13 项目: openjdk-jdk9   文件: Utils.java
public TypeElement getEnclosingTypeElement(Element e) {
    if (e.getKind() == ElementKind.PACKAGE)
        return null;
    Element encl = e.getEnclosingElement();
    ElementKind kind = encl.getKind();
    if (kind == ElementKind.PACKAGE)
        return null;
    while (!(kind.isClass() || kind.isInterface())) {
        encl = encl.getEnclosingElement();
    }
    return (TypeElement)encl;
}
 
源代码14 项目: openjdk-jdk8u-backup   文件: Main.java
public static PackageElement getPackage(TypeElement type) {
    Element owner = type;
    while (owner.getKind() != ElementKind.PACKAGE)
        owner = owner.getEnclosingElement();
    return (PackageElement)owner;
}
 
源代码15 项目: javaide   文件: PrintingProcessor.java
private void printModifiers(Element e) {
    ElementKind kind = e.getKind();
    if (kind == PARAMETER) {
        printAnnotationsInline(e);
    } else {
        printAnnotations(e);
        indent();
    }

    if (kind == ENUM_CONSTANT)
        return;

    Set<Modifier> modifiers = new LinkedHashSet<Modifier>();
    modifiers.addAll(e.getModifiers());

    switch (kind) {
    case ANNOTATION_TYPE:
    case INTERFACE:
        modifiers.remove(Modifier.ABSTRACT);
        break;

    case ENUM:
        modifiers.remove(Modifier.FINAL);
        modifiers.remove(Modifier.ABSTRACT);
        break;

    case METHOD:
    case FIELD:
        Element enclosingElement = e.getEnclosingElement();
        if (enclosingElement != null &&
            enclosingElement.getKind().isInterface()) {
            modifiers.remove(Modifier.PUBLIC);
            modifiers.remove(Modifier.ABSTRACT); // only for methods
            modifiers.remove(Modifier.STATIC);   // only for fields
            modifiers.remove(Modifier.FINAL);    // only for fields
        }
        break;

    }

    for(Modifier m: modifiers) {
        writer.print(m.toString() + " ");
    }
}
 
源代码16 项目: openjdk-8-source   文件: Main.java
public static PackageElement getPackage(TypeElement type) {
    Element owner = type;
    while (owner.getKind() != ElementKind.PACKAGE)
        owner = owner.getEnclosingElement();
    return (PackageElement)owner;
}
 
源代码17 项目: dagger2-sample   文件: DependencyRequest.java
static DeclaredType getEnclosingType(Element element) {
  while (!MoreElements.isType(element)) {
    element = element.getEnclosingElement();
  }
  return MoreTypes.asDeclared(element.asType());
}
 
源代码18 项目: Witch-Android   文件: FileUtils.java
private static String getElementPackage(Element element) {
    while (element.getKind() != PACKAGE) {
        element = element.getEnclosingElement();
    }
    return ((PackageElement) element).getQualifiedName().toString();
}
 
@TriggerTreeKind(Tree.Kind.METHOD_INVOCATION)
public static ErrorDescription hint(HintContext ctx) {
    MethodInvocationTree mit = (MethodInvocationTree) ctx.getPath().getLeaf();
    CompilationInfo info = ctx.getInfo();
    TreePath enclosingMethod = Utilities.findOwningExecutable(ctx, ctx.getPath(), false);

    if (enclosingMethod == null) {
        return null;
    }

    Element enclosingMethodElement = ctx.getInfo().getTrees().getElement(enclosingMethod);
    
    if (enclosingMethodElement == null || enclosingMethodElement.getKind() != ElementKind.CONSTRUCTOR) {
        return null;
    }

    Element methodInvocationElement = info.getTrees().getElement(new TreePath(ctx.getPath(), mit.getMethodSelect()));
    if (methodInvocationElement == null || methodInvocationElement.getKind() != ElementKind.METHOD) {
        return null;
    }
    Element classElement = methodInvocationElement.getEnclosingElement();
    if (classElement == null || classElement.getKind() != ElementKind.CLASS) {
        return null;
    }
    Element classEl = enclosingMethodElement.getEnclosingElement();
    if (classEl == null || classEl.getKind() != ElementKind.CLASS) {
        return null;
    }
    boolean sameClass = classElement.equals(enclosingMethodElement.getEnclosingElement());
    if (!info.getTypes().isSubtype(classEl.asType(), classElement.asType())) {
        return null;
    }
    // classEl exts classElemenet - either classElement == classEl, or classElement cannot be final anyway
    if (classEl.getModifiers().contains(Modifier.FINAL)) {
        return null;
    }

    Set<Modifier> modifiers = methodInvocationElement.getModifiers();
    if (modifiers.contains(Modifier.PRIVATE) || 
        modifiers.contains(Modifier.FINAL) || 
        modifiers.contains(Modifier.STATIC)) {
        return null;
    }

    if (!invocationOnThis(mit)) {
        return null;
    }

    TreePath methodDeclaration = ctx.getInfo().getTrees().getPath(methodInvocationElement);

    if (methodDeclaration == null || ctx.getInfo().getTreeUtilities().isSynthetic(methodDeclaration)) return null;

    return ErrorDescriptionFactory.forName(ctx, mit,
            NbBundle.getMessage(
                OverridableMethodCallInConstructor.class,
                "MSG_org.netbeans.modules.java.hints.OverridableMethodCallInConstructor"),
                sameClass ? computeFixes((MethodTree) methodDeclaration.getLeaf(),
                    classElement, ctx) : null);
}
 
源代码20 项目: netbeans   文件: FieldValidator.java
@Override
public void run(CompilationController parameter) throws Exception {
    parameter.toPhase(JavaSource.Phase.RESOLVED);
    this.cinfo = parameter;
    if (targetHandle == null || srcHandle == null) {
        return;
    }
    TreePath srcPath = srcHandle.resolve(cinfo);
    if (srcPath == null) {
        return;
    }
    TreePath targetPath = targetHandle.resolve(cinfo);
    if (targetPath == null) {
        return;
    }
    initialScope = cinfo.getTrees().getScope(srcPath);
    Scope targetScope = cinfo.getTrees().getScope(targetPath);
    Map<? extends Element, Scope> visibleVariables = 
            cinfo.getElementUtilities().findElementsAndOrigins(initialScope, this);
    lastResult = null;
    Element target = cinfo.getTrees().getElement(targetPath);
    for (Element e : visibleVariables.keySet()) {
        switch (e.getKind()) {
            case FIELD: case ENUM_CONSTANT: case PARAMETER:
                Scope def = visibleVariables.get(e);
                if (def != targetScope) {
                    for (Scope s = def; s.getEnclosingClass() != null; s = s.getEnclosingScope()) {
                        if (s == target) {
                            lastResult = new MemberSearchResult(ElementHandle.create(e));
                            return;
                        }
                    }
                }
                TypeElement owner = def.getEnclosingClass();
                if (owner == null) {
                    // static import
                    lastResult = new MemberSearchResult(ElementHandle.create(e),
                        ElementHandle.create((TypeElement)e.getEnclosingElement()));
                } else if (owner == e.getEnclosingElement()) {
                    if (owner == target) {
                        lastResult = new MemberSearchResult(ElementHandle.create(e));
                        return;
                    } else {
                        lastResult = new MemberSearchResult(ElementHandle.create(e),
                            ElementHandle.create(owner));
                    }
                } else {
                    // special case - hiding superclass field
                    lastResult = new MemberSearchResult(ElementHandle.create(e),
                        ElementHandle.create(owner), null);
                }
                break;
            case EXCEPTION_PARAMETER:
            case LOCAL_VARIABLE: 
            case RESOURCE_VARIABLE: {
                TreePath locPath = findLocalPathWorkaround(cinfo, e, srcPath);
                if (locPath == null) {
                    lastResult = new MemberSearchResult(e.getKind());
                } else {
                    lastResult = new MemberSearchResult(TreePathHandle.create(locPath, cinfo), e.getKind());
                }
            }
                return;
            default:
                // another namespace
                return;
        }
    }
}