Java Code Examples for javax.lang.model.element.ElementKind.CLASS

The following are Jave code examples for showing how to use CLASS of the javax.lang.model.element.ElementKind 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: DUnit   File: DUnitProcessor.java   View Source Code Vote up 6 votes
/**
 * 处理每一个TypeElement
 *
 * @param unitModels      保存DUnitModel
 * @param unitGroupModels 保存DUnitGroupModel
 * @param type            TypeElement
 */
private void processType(ArrayList<DUnitModel> unitModels, ArrayList<DUnitGroupModel> unitGroupModels, TypeElement type) {
	if (type.getKind() != ElementKind.CLASS) {
		mErrorReporter.abortWithError("@" + DUnit.class.getName() + " only applies to classes", type);
	}

	DUnit dUnit = type.getAnnotation(DUnit.class);
	DUnitGroup dUnitGroup = type.getAnnotation(DUnitGroup.class);

	if (dUnit != null) {
		DUnitModel unitModel = mUnitModelUtil.createUnitModel(type, dUnit);
		unitModels.add(unitModel);
	}

	if (dUnitGroup != null) {
		DUnitGroupModel unitGroupModel = mUnitModelUtil.createUnitGroupModel(type, dUnitGroup);
		unitGroupModels.add(unitGroupModel);
	}


}
 
Example 2
Project: incubator-netbeans   File: MavenWhiteListQueryImpl.java   View Source Code Vote up 6 votes
@Override
public WhiteListQuery.Result check(ElementHandle<?> element, WhiteListQuery.Operation operation) {
    if (!operation.equals(WhiteListQuery.Operation.USAGE)) {
        return OK;
    }
    List<WhiteListQuery.RuleDescription> rds = new ArrayList<WhiteListQuery.RuleDescription>();
    if (element.getKind() == ElementKind.CLASS || element.getKind() == ElementKind.INTERFACE) {
        String qn = element.getQualifiedName();
        if (qn.lastIndexOf('.') > 0) {
            String pack = qn.substring(0, qn.lastIndexOf("."));
            synchronized (IMPL_LOCK) {
                if (privatePackages.contains(pack)) {
                    rds.add(PRIVATE_RD);
                }
                if (transitivePackages.contains(pack)) {
                    rds.add(TRANSITIVE_RD);
                }
            }
            if (!rds.isEmpty()) {
                return new WhiteListQuery.Result(rds);
            }
        }
    }
    return OK;
}
 
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: Type.java   View Source Code Vote up 6 votes
private void collectConstructors(Collection<IConstructor> constructors, Element element){
    if(element == null || element.getKind()!=ElementKind.CLASS) {
        return;
    }
    TypeElement el = (TypeElement) element;
    for(Element sub: el.getEnclosedElements()){
        if(sub.getKind() == ElementKind.CONSTRUCTOR){
            constructors.add(new Constructor(this, (ExecutableElement)sub));
        } else if ((sub.getKind() == ElementKind.CLASS) && (((TypeElement) sub).getSuperclass() != null)){
            TypeMirror supMirror = ((TypeElement) sub).getSuperclass();
            if (supMirror.getKind() == TypeKind.DECLARED) {
                DeclaredType superclassDeclaredType = (DeclaredType)supMirror;
                Element superclassElement = superclassDeclaredType.asElement();  
                collectConstructors(constructors, superclassElement);
            }
        }
    }
}
 
Example 5
Project: incubator-netbeans   File: DocumentUtil.java   View Source Code Vote up 6 votes
@NonNull
static ElementKind decodeKind (char kind) {
    switch (kind) {
        case EK_CLASS:
        case EK_LOCAL_CLASS:
            return ElementKind.CLASS;
        case EK_INTERFACE:
        case EK_LOCAL_INTERFACE:
            return ElementKind.INTERFACE;
        case EK_ENUM:
        case EK_LOCAL_ENUM:
            return ElementKind.ENUM;
        case EK_ANNOTATION:
        case EK_LOCAL_ANNOTATION:
            return ElementKind.ANNOTATION_TYPE;
        case EK_MODULE:
            return ElementKind.MODULE;
        default:
            throw new IllegalArgumentException ();
    }
}
 
Example 6
Project: easybundler   File: BundlerClassProcessor.java   View Source Code Vote up 5 votes
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
    TypeElement annotationElement = processingEnvironment.getElementUtils()
            .getTypeElement(ANNOTATION_CLASS);

    for (Element e : roundEnvironment.getElementsAnnotatedWith(annotationElement)) {
        if (e.getKind() == ElementKind.CLASS) {
            TypeElement te = (TypeElement) e;
            processClass(te);
        }
    }

    return true;
}
 
Example 7
Project: GitHub   File: ValueTypeComposer.java   View Source Code Vote up 5 votes
static boolean checkAbstractValueType(Element element, Collection<String> violations) {
  boolean ofSupportedKind = false
      || element.getKind() == ElementKind.INTERFACE
      || element.getKind() == ElementKind.ANNOTATION_TYPE
      || element.getKind() == ElementKind.CLASS;

  boolean staticOrTopLevel = false
      || element.getEnclosingElement().getKind() == ElementKind.PACKAGE
      || element.getModifiers().contains(Modifier.STATIC);

  boolean nonFinal = !element.getModifiers().contains(Modifier.FINAL);

  boolean publicOrPackageVisible =
      !element.getModifiers().contains(Modifier.PRIVATE)
          && !element.getModifiers().contains(Modifier.PROTECTED);

  if (!ofSupportedKind) {
    violations.add("must be class or interface or annotation type");
  }

  if (!nonFinal) {
    violations.add("must be non-final");
  }

  if (!publicOrPackageVisible) {
    violations.add("should be public or package-visible");
  }

  if (!staticOrTopLevel) {
    violations.add("should be top-level or static inner class");
  }

  return violations.isEmpty();
}
 
Example 8
Project: GitHub   File: AttributeBuilderReflection.java   View Source Code Vote up 5 votes
/**
 * Return true if the possibleBuilderMethod matches the
 * Style#attributeBuilder() and returns a class.
 *
 * TODO: may need to make this return true if the return type is an interface too...
 *
 * @param possibleBuilderMethod executableElement
 */
private static boolean isPossibleBuilderMethod(Element possibleBuilderMethod, boolean onValueType, ValueAttribute valueAttribute) {
  if (possibleBuilderMethod.getKind() == ElementKind.METHOD) {
    String detectedAttributeBuilder = valueAttribute.containingType.names()
        .rawFromAttributeBuilder(possibleBuilderMethod.getSimpleName().toString());

    if (detectedAttributeBuilder.isEmpty()) {
      return false;
    }
    ExecutableElement candidateMethod = (ExecutableElement) possibleBuilderMethod;

    return possibleBuilderMethod.getModifiers().containsAll(
        Arrays.asList(Modifier.STATIC, Modifier.PUBLIC))
        && candidateMethod.getParameters().isEmpty()
        && candidateMethod
        .getReturnType().getKind()
        == TypeKind.DECLARED
        && ((DeclaredType) candidateMethod.getReturnType())
        .asElement().getKind() == ElementKind.CLASS;

  } else if (!onValueType && possibleBuilderMethod.getKind() == ElementKind.CONSTRUCTOR) {
    if (!valueAttribute.containingType.names().newTokenInAttributeBuilder()) {
      return false;
    }

    ExecutableElement candidateConstructor = (ExecutableElement) possibleBuilderMethod;

    return candidateConstructor.getModifiers().contains(Modifier.PUBLIC)
        && candidateConstructor.getTypeParameters().isEmpty();
  }
  return false;
}
 
Example 9
Project: GitHub   File: AttributeBuilderReflection.java   View Source Code Vote up 5 votes
/**
 * Determine if inner class could be a builder.
 *
 * @param possibleBuilderClass nested value element that could be builder class.
 * @return true if it's a static inner class.
 */
private static boolean isPossibleBuilderClass(Element possibleBuilderClass, ValueAttribute valueAttribute) {
  if (possibleBuilderClass.getKind() == ElementKind.CLASS) {

    if (valueAttribute.containingType.names().newTokenInAttributeBuilder()) {
      return possibleBuilderClass.getModifiers().contains(Modifier.STATIC)
          && possibleBuilderClass.getKind() == ElementKind.CLASS;
    }
  }

  return false;
}
 
Example 10
Project: incubator-netbeans   File: Flow.java   View Source Code Vote up 5 votes
/**
 * Determines if the Element should be processed as an undefined variable. Currently the implementation
 * only returns true if an undefinedSymbolScope is set, AND the undefined symbol might belong to that scope.
 * If the undefined symbol is qualified (by this. or class.this. or whatever else manner), the method will
 * only return true if the qualifier corresponds to the undefinedSymbolScope.
 * 
 */
private boolean shouldProcessUndefined(Element e) {
    if (e == null || undefinedSymbolScope == null ||
        e.asType().getKind() != TypeKind.ERROR) {
        return false;
    }
    if (e.getKind() == ElementKind.CLASS) {
        return referenceTarget == null || 
            referenceTarget == undefinedSymbolScope;
    }
    return false;
}
 
Example 11
Project: incubator-netbeans   File: InnerToOuterRefactoringUI.java   View Source Code Vote up 5 votes
/** Creates a new instance of InnerToOuterRefactoringUI
 * @param selectedElements Elements the refactoring action was invoked on.
 */
private InnerToOuterRefactoringUI(TreePathHandle sourceType, CompilationInfo info) {
    refactoring = new InnerToOuterRefactoring(sourceType);
    refactoring.setReferenceName("outer"); //NOI18N
    Element temp = sourceType.resolveElement(info);
    className = temp.getSimpleName().toString();
    disableDeclareFields = temp.getModifiers().contains(Modifier.STATIC) || temp.getKind() !=ElementKind.CLASS;
}
 
Example 12
Project: XModulable   File: XModuleProcessor.java   View Source Code Vote up 5 votes
/**
 * 校验{@code moduleElement}的合法性
 *
 * @param moduleElement 携带@XModule的Element
 * @throws ProcessException
 */
private void verify(Element moduleElement) throws ProcessException {
    XModule XModule = moduleElement.getAnnotation(XModule.class);
    // 检验XModule注解
    if (XModule == null) {
        throw new ProcessException(
                String.format("当前的element(%s)未携带%s注解",
                        moduleElement.toString(),
                        XModule.class.getSimpleName()));
    } else if (XModule.name() == null || XModule.name().isEmpty()) {
        throw new ProcessException(String.format("%s的组件名不能为空", moduleElement.toString()));
    }

    // 检验被XModule注解的是否为class
    if (moduleElement.getKind() != ElementKind.CLASS) {
        throw new ProcessException(
                String.format("%s不是类,只有类才能使用%s",
                        moduleElement.toString(),
                        XModule.class.getSimpleName()));
    }

    TypeElement classElement = (TypeElement) moduleElement;

    // 检验类修饰符
    Set<Modifier> modifiers = classElement.getModifiers();
    if (!modifiers.contains(Modifier.PUBLIC)) {
        throw new ProcessException(
                String.format("被%s注解的%s的权限修饰符必须为public",
                        XModule.class.getSimpleName(),
                        classElement.getQualifiedName().toString()));
    }
    if (modifiers.contains(Modifier.ABSTRACT)) {
        throw new ProcessException(
                String.format("%s是抽象类,不能被%s注解",
                        classElement.getQualifiedName().toString(),
                        XModule.class.getSimpleName()));
    }
}
 
Example 13
Project: incubator-netbeans   File: Utilities.java   View Source Code Vote up 5 votes
public static boolean isPrimitiveWrapperType(TypeMirror tm) {
    if (tm == null || tm.getKind() != TypeKind.DECLARED) { 
        return false;
    }
    Element el = ((DeclaredType)tm).asElement();
    if (el == null || el.getKind() != ElementKind.CLASS) {
        return false;
    }
    String s = ((TypeElement)el).getQualifiedName().toString();
    return PRIMITIVE_NAMES.contains(s); // NOI18N
}
 
Example 14
Project: incubator-netbeans   File: ObjectProviders.java   View Source Code Vote up 5 votes
@Override
public List<T> createObjects(TypeElement type) {
    final List<T> result = new ArrayList<T>();
    if (type.getKind() == ElementKind.CLASS || type.getKind() == ElementKind.INTERFACE) {
        if (helper.hasAnyAnnotation(type.getAnnotationMirrors(), getAnnotationTypeNames())) {
            result.add(createObject(helper, type));
        }
    }
    return result;
}
 
Example 15
Project: incubator-netbeans   File: AbstractTestGenerator.java   View Source Code Vote up 5 votes
/**
 * Checks whether the given type object represents type
 * {@code java.lang.Object}.
 * 
 * @param  type  type to be checked
 * @return  {@code true} if the passed type object represents type
 *          {@code java.lang.Object}, {@code false} otherwise
 */
private static boolean isRootObjectType(DeclaredType type) {
    if (type.getKind() != TypeKind.DECLARED) {
        return false;
    }

    TypeElement elem = (TypeElement) type.asElement();
    return (elem.getKind() == ElementKind.CLASS)
           && (elem.getSuperclass().getKind() == TypeKind.NONE);
}
 
Example 16
Project: incubator-netbeans   File: JavacClassInfo.java   View Source Code Vote up 5 votes
private void addAnonymousInnerClasses(final CompilationController cc, final Set<SourceClassInfo> innerClasses)
        throws IOException {
    final int parentClassNameLength = getQualifiedName().length();

    cc.toPhase(JavaSource.Phase.RESOLVED);

    ErrorAwareTreePathScanner<Void, Void> scanner = new ErrorAwareTreePathScanner<Void, Void>() {

        @Override
        public Void visitClass(ClassTree node, Void v) {
            Element classElement = cc.getTrees().getElement(getCurrentPath());

            if ((classElement != null) && (classElement.getKind() == ElementKind.CLASS)) {
                TypeElement innerClassElement = (TypeElement) classElement;
                String className = ElementUtilities.getBinaryName(innerClassElement);

                if (className.length() <= parentClassNameLength) {
                    className = "";
                } else {
                    className = className.substring(parentClassNameLength);
                }

                if (isAnonymous(className)) {
                    innerClasses.add(new JavacClassInfo(ElementHandle.create(innerClassElement), cc));
                }
            }

            super.visitClass(node, v);

            return null;
        }  
    };

    scanner.scan(cc.getCompilationUnit(), null);
}
 
Example 17
Project: incubator-netbeans   File: WorkingCopy.java   View Source Code Vote up 5 votes
FileObject doCreateFromTemplate(CompilationUnitTree cut) throws IOException {
    ElementKind kind;
    if ("package-info.java".equals(cut.getSourceFile().getName())) {
        kind = ElementKind.PACKAGE;
    } else if (cut.getTypeDecls().isEmpty()) {
        kind = null;
    } else {
        switch (cut.getTypeDecls().get(0).getKind()) {
            case CLASS:
                kind = ElementKind.CLASS;
                break;
            case INTERFACE:
                kind = ElementKind.INTERFACE;
                break;
            case ANNOTATION_TYPE:
                kind = ElementKind.ANNOTATION_TYPE;
                break;
            case ENUM:
                kind = ElementKind.ENUM;
                break;
            default:
                Logger.getLogger(WorkingCopy.class.getName()).log(Level.SEVERE, "Cannot resolve template for {0}", cut.getTypeDecls().get(0).getKind());
                kind = null;
        }
    }
    FileObject template = FileUtil.getConfigFile(template(kind));
    return doCreateFromTemplate(template, cut.getSourceFile());
}
 
Example 18
Project: incubator-netbeans   File: InstantRenamePerformer.java   View Source Code Vote up 4 votes
private static boolean allowInstantRename(CompilationInfo info, Element e, ElementUtilities eu) {
    if(e.getKind() == ElementKind.FIELD) {
        VariableElement variableElement = (VariableElement) e;
        TypeElement typeElement = eu.enclosingTypeElement(e);
        
        boolean isProperty = false;
        try {
            CodeStyle codeStyle = CodeStyle.getDefault(info.getDocument());
            isProperty = eu.hasGetter(typeElement, variableElement, codeStyle);
            isProperty = isProperty || (!variableElement.getModifiers().contains(Modifier.FINAL) &&
                                eu.hasSetter(typeElement, variableElement, codeStyle));
        } catch (IOException ex) {
        }
        if(isProperty) {
            return false;
        }
    }
    if (org.netbeans.modules.java.editor.base.semantic.Utilities.isPrivateElement(e)) {
        return true;
    }
    
    if (isInaccessibleOutsideOuterClass(e, eu)) {
        return true;
    }
    
    //#92160: check for local classes:
    if (e.getKind() == ElementKind.CLASS) {//only classes can be local
        Element enclosing = e.getEnclosingElement();
        final ElementKind enclosingKind = enclosing.getKind();

        //#150352: parent is annonymous class
        if (enclosingKind == ElementKind.CLASS) {
            final Set<ElementKind> fm = EnumSet.of(ElementKind.METHOD, ElementKind.FIELD);
            if (enclosing.getSimpleName().length() == 0 || fm.contains(enclosing.getEnclosingElement().getKind())) {
                return true;
            }
        }


        return LOCAL_CLASS_PARENTS.contains(enclosingKind);
    }

    if (e.getKind() == ElementKind.TYPE_PARAMETER) {
        return true;
    }
    
    return false;
}
 
Example 19
Project: incubator-netbeans   File: ElementOverlay.java   View Source Code Vote up 4 votes
@Override
public ElementKind getKind() {
    return ElementKind.CLASS;
}
 
Example 20
Project: incubator-netbeans   File: StaticImport.java   View Source Code Vote up 2 votes
/**
 * @param info
 * @param t1
 * @param t3
 * @return true iff the first type (or its containing class in the case of inner classes)
 * is a subtype of the second.
 * @see Types#isSubtype(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror)
 */
private static boolean isSubTypeOrInnerOfSubType(CompilationInfo info, Element t1, Element t2) {
    boolean isSubtype = info.getTypes().isSubtype(t1.asType(), t2.asType());
    boolean isInnerClass = t1.getEnclosingElement().getKind() == ElementKind.CLASS;
    return isSubtype || (isInnerClass && info.getTypes().isSubtype(t1.getEnclosingElement().asType(), t2.asType()));
}