Java Code Examples for javax.lang.model.element.TypeElement.getEnclosingElement()

The following are Jave code examples for showing how to use getEnclosingElement() of the javax.lang.model.element.TypeElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: limitjson   File: JSONProcessor.java   View Source Code Vote up 6 votes
private void processLimitJSON(RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(LIMITJSON.class)) {
            LJSONTypeElement ljsonElement = LJSONTypeElement.create(elements, element);
            processorHelper.setLimitJSONTypeElements(ljsonElement);
            processorHelper.i("---------------------" + ljsonElement.toString());
//
            TypeElement typeElement = (TypeElement) element;
            Element a = typeElement.getEnclosingElement();
            processorHelper.i("LJSON ++" + a + " " + element.getKind().name());
            List<? extends Element> param = typeElement.getEnclosedElements();
            for (Element p : param) {
                if (p instanceof VariableElement) {
                    processorHelper.i("LJSON ........" + p.getSimpleName() + " " + p.asType().toString());
                }
            }

        }
    }
 
Example 2
Project: DUnit   File: DUnitModelUtil.java   View Source Code Vote up 6 votes
/**
 * 获取TypeElement真实的类名
 */
public String getRealClassName(TypeElement typeElement) {
	NestingKind nestingKind = typeElement.getNestingKind();
	if (nestingKind.isNested()) {
		Element enclosingElement = typeElement.getEnclosingElement();
		if (enclosingElement.getKind() == ElementKind.CLASS || enclosingElement.getKind() == ElementKind.INTERFACE){
			String enclosingClassName = getRealClassName((TypeElement) enclosingElement);
			return enclosingClassName + "$" + typeElement.getSimpleName();
		}else {
			mErrorReporter.reportError("the type(" + enclosingElement.getKind()+ ") of enclosing element is not CLASS or INTERFACE.",typeElement);
			return null;
		}
	}else {
		return typeElement.getQualifiedName().toString();
	}
}
 
Example 3
Project: Spyglass   File: CompanionNamer.java   View Source Code Vote up 6 votes
private static String getParentChain(final TypeElement targetClass) {
	// if input is top level class return it
	// otherwise return the parent chain plus it

	if (targetClass.getNestingKind() == NestingKind.TOP_LEVEL) {
		return targetClass.getSimpleName().toString();
	} else {
		final Element parent = targetClass.getEnclosingElement();

		if (parent.getKind() != ElementKind.CLASS) {
			throw new RuntimeException("Cannot create parent chain. Non-class parent found.");
		}

		return (getParentChain((TypeElement) parent)) + "_" + targetClass.getSimpleName().toString();
	}
}
 
Example 4
Project: incubator-netbeans   File: ElementUtilities.java   View Source Code Vote up 5 votes
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
    List<? extends TypeParameterElement> tpes = e.getTypeParameters();
    TypeMirror[] targs = new TypeMirror[tpes.size()];
    int i = 0;
    for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
        TypeParameterElement tpe = it.next();
        TypeMirror t = map.get(tpe);
        targs[i++] = t != null ? t : tpe.asType();
    }
    Element encl = e.getEnclosingElement();
    if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty())
            return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs);
    return types.getDeclaredType(e, targs);
}
 
Example 5
Project: incubator-netbeans   File: SpringXMLConfigCompletionItem.java   View Source Code Vote up 5 votes
private String getRelativeName(TypeElement elem) {
    StringBuilder sb = new StringBuilder();
    sb.append(elem.getSimpleName().toString());
    Element parent = elem.getEnclosingElement();
    while(parent.getKind() != ElementKind.PACKAGE) {
        sb.insert(0, parent.getSimpleName().toString() + "$"); // NOI18N
        parent = parent.getEnclosingElement();
    }

    return sb.toString();
}
 
Example 6
Project: incubator-netbeans   File: InstanceRefFinder.java   View Source Code Vote up 5 votes
private void addLocalReference(TypeElement el) {
    if (this.enclosingElement != el.getEnclosingElement()) {
        return;
    }
    if (localReferences.isEmpty()) {
        localReferences = new HashSet<TypeElement>();
    }
    localReferences.add(el);
}
 
Example 7
Project: incubator-netbeans   File: Utilities.java   View Source Code Vote up 5 votes
public static Visibility getAccessModifiers(@NonNull CompilationInfo info, @NullAllowed TypeElement source, @NonNull TypeElement target) {
    if (target.getKind().isInterface()) {
        return Visibility.PUBLIC;
    }

    TypeElement outterMostSource = source != null ? info.getElementUtilities().outermostTypeElement(source) : null;
    TypeElement outterMostTarget = info.getElementUtilities().outermostTypeElement(target);

    if (outterMostTarget.equals(outterMostSource)) {
        return Visibility.PRIVATE;
    }

    Element sourcePackage;

    if (outterMostSource != null) {
        sourcePackage = outterMostSource.getEnclosingElement();
    } else if (info.getCompilationUnit().getPackageName() != null) {
        sourcePackage = info.getTrees().getElement(new TreePath(new TreePath(info.getCompilationUnit()), info.getCompilationUnit().getPackageName()));
    } else {
        sourcePackage = info.getElements().getPackageElement("");
    }

    Element targetPackage = outterMostTarget.getEnclosingElement();

    if (sourcePackage != null && sourcePackage.equals(targetPackage)) {
        return Visibility.PACKAGE_PRIVATE;
    }

    //TODO: protected?
    return Visibility.PUBLIC;
}
 
Example 8
Project: incubator-netbeans   File: JavadocCompletionQuery.java   View Source Code Vote up 5 votes
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
    List<? extends TypeParameterElement> tpes = e.getTypeParameters();
    TypeMirror[] targs = new TypeMirror[tpes.size()];
    int i = 0;
    for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
        TypeParameterElement tpe = it.next();
        TypeMirror t = map.get(tpe);
        targs[i++] = t != null ? t : tpe.asType();
    }
    Element encl = e.getEnclosingElement();
    if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty())
            return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs);
    return types.getDeclaredType(e, targs);
}
 
Example 9
Project: incubator-netbeans   File: GoToSupport.java   View Source Code Vote up 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;
    }
    Element enclosing = e.getEnclosingElement();
    
    if (enclosing == SourceUtils.getEnclosingTypeElement(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 10
Project: incubator-netbeans   File: HibernateCompletionItem.java   View Source Code Vote up 5 votes
private String getRelativeName(TypeElement elem) {
    StringBuilder sb = new StringBuilder();
    sb.append(elem.getSimpleName().toString());
    Element parent = elem.getEnclosingElement();
    while (parent.getKind() != ElementKind.PACKAGE) {
        sb.insert(0, parent.getSimpleName().toString() + "$"); // NOI18N
        parent = parent.getEnclosingElement();
    }

    return sb.toString();
}
 
Example 11
Project: auto-value-json   File: AutoValueJsonExtension.java   View Source Code Vote up 5 votes
private static ClassName getFinalClassName(Context context) {
  TypeElement autoValueClass = context.autoValueClass();
  String name = autoValueClass.getSimpleName().toString();

  Element enclosingElement = autoValueClass.getEnclosingElement();
  while (enclosingElement instanceof TypeElement) {
    name = enclosingElement.getSimpleName().toString() + "_" + name;
    enclosingElement = enclosingElement.getEnclosingElement();
  }

  return ClassName.get(context.packageName(), "AutoValue_" + name);
}
 
Example 12
Project: Spyglass   File: CompanionGenerator.java   View Source Code Vote up 5 votes
private PackageElement getPackage(final TypeElement type) {
	if (type.getNestingKind() == NestingKind.TOP_LEVEL) {
		return (PackageElement) type.getEnclosingElement();
	} else {
		return getPackage((TypeElement) type.getEnclosingElement());
	}
}
 
Example 13
Project: EgTest   File: CodeInjector.java   View Source Code Vote up 5 votes
public CodeInjector(TypeElement classElement, Map<String,EgLanguage> languageForClassName, EgLanguage defaultLanguage) {
    this.classElement = classElement;
    this.languageForClassName = languageForClassName;
    this.defaultLanguage = languageForClassName.getOrDefault(AnnotationCollector.className(classElement), defaultLanguage);

    PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();
    this.packageName = ""+packageElement.getQualifiedName();
    if (EgLanguage.INHERIT.equals(this.defaultLanguage)) {
        throw new IllegalArgumentException("Cannot use INHERIT as a default EgTest language");
    }
}
 
Example 14
Project: easy-plugins   File: Util.java   View Source Code Vote up 5 votes
private static void checkClassVisibility(Elements eles, TypeElement target, String fromPkg) {
    Set<Modifier> targetMods = target.getModifiers();
    if (!isNestedClass(target)) {
        simpleVisibility(target.getModifiers(), eles.getPackageOf(target), fromPkg, target);
    } else {
        Element surround = target.getEnclosingElement();

        if (surround.getKind() != CLASS
                && surround.getKind() != INTERFACE
                // Not sure if that is allowed, but we include it anyways.
                && surround.getKind() != ENUM) {
            throw new IllegalStateException(
                    target.toString() + " is not nested? " + surround.toString());
        }

        TypeElement enclosingType = (TypeElement) surround;

        // The surrounding class must be visible for us to see the nested class
        if (!isElementVisibleFrom(eles, enclosingType, fromPkg)) {
            throw new EasyPluginException(enclosingType.toString()
                    + " is not visible from output package; need for access to " + target.toString());
        }
        // Nested class must be static since we don't have an enclosing instance
        if (!targetMods.contains(Modifier.STATIC)) {
            throw new EasyPluginException(target.toString() + " must be static.");
        }

        simpleVisibility(target.getModifiers(), eles.getPackageOf(target), fromPkg, target);
    }
}
 
Example 15
Project: openjdk-jdk10   File: ElementsTable.java   View Source Code Vote up 5 votes
@Override
public Boolean visitType(TypeElement e, Void p) {
    if (includedTypeElements.contains(e)) {
        return true;
    }
    if (isTypeElementSelected(e)) {
        // Class is nameable from top-level and
        // the class and all enclosing classes
        // pass the modifier filter.
        PackageElement pkg = toolEnv.elements.getPackageOf(e);
        if (specifiedPackageElements.contains(pkg)) {
            return true;
        }
        Element enclosing = e.getEnclosingElement();
        if (enclosing != null) {
            switch(enclosing.getKind()) {
                case PACKAGE:
                    return specifiedPackageElements.contains((PackageElement)enclosing);
                case CLASS: case INTERFACE: case ENUM: case ANNOTATION_TYPE:
                    return visit((TypeElement) enclosing);
                default:
                    throw new AssertionError("unknown element: " + enclosing);
            }
        }
    }
    return false;
}
 
Example 16
Project: openjdk-jdk10   File: WebServiceVisitor.java   View Source Code Vote up 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 17
Project: incubator-netbeans   File: IllegalInstanceOf.java   View Source Code Vote up 4 votes
public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {
    if (treePath.getLeaf().getKind() != Kind.INSTANCE_OF) {
        return null;
    }
    
    InstanceOfTree iot = (InstanceOfTree) treePath.getLeaf();
    TypeMirror     leftTypeMirror = info.getTrees().getTypeMirror(new TreePath(treePath, iot.getExpression()));
    Element        rightType = info.getTrees().getElement(new TreePath(treePath, iot.getType()));
    
    if (leftTypeMirror == null || leftTypeMirror.getKind() != TypeKind.DECLARED) {
        return null;
    }
    
    Element leftType = ((DeclaredType) leftTypeMirror).asElement();
    
    if (leftType == null || rightType == null || !leftType.getKind().isInterface() || !rightType.getKind().isInterface()) {
        //no problem:
        return null;
    }
    
    TypeElement left = (TypeElement) leftType;
    TypeElement right = (TypeElement) rightType;
    
    if (   left.getEnclosingElement().getKind() != ElementKind.PACKAGE
        || right.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
        return null;
    }
    
    PackageElement leftPackage = (PackageElement) left.getEnclosingElement();
    PackageElement rightPackage = (PackageElement) right.getEnclosingElement();
    
    String leftPackageFQN = leftPackage.getQualifiedName().toString();
    String rightPackageFQN = rightPackage.getQualifiedName().toString();
    
    if (packagesToCheck.containsKey(leftPackageFQN) && leftPackageFQN.equals(rightPackageFQN)) {
        String verifyClass = packagesToCheck.get(leftPackageFQN);
        TypeElement loadedVerify = info.getElements().getTypeElement(verifyClass);
        
        if (loadedVerify == null || !info.getTypes().isSubtype(left.asType(), loadedVerify.asType()))
            return null;
        
        if (!info.getTypes().isSubtype(right.asType(), loadedVerify.asType()))
            return null;
        
        int start = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), iot);
        int end   = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), iot);
        return Collections.<ErrorDescription>singletonList(
                   ErrorDescriptionFactory.createErrorDescription(getSeverity().toEditorSeverity(),
                                                                  NbBundle.getMessage(IllegalInstanceOf.class, "MSG_IllegalInstanceOf"),
                                                                  info.getFileObject(),
                                                                  start,
                                                                  end
                                                                 )
               );
    }
    
    return null;
}
 
Example 18
Project: openjdk-jdk10   File: ApNavigator.java   View Source Code Vote up 4 votes
public boolean isInnerClass(TypeElement clazz) {
    return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC);
}
 
Example 19
Project: OpenJSharp   File: WebServiceVisitor.java   View Source Code Vote up 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 20
Project: OpenJSharp   File: ApNavigator.java   View Source Code Vote up 4 votes
public boolean isInnerClass(TypeElement clazz) {
    return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC);
}