Java 类javax.lang.model.element.NestingKind 实例源码
项目:incubator-netbeans
文件:CtSymArchiveTest.java
public void testIssue247469() throws IOException {
final JavaPlatform jp = JavaPlatformManager.getDefault().getDefaultPlatform();
assertNotNull(jp);
final ClasspathInfo cpInfo = ClasspathInfo.create(jp.getBootstrapLibraries(), ClassPath.EMPTY, ClassPath.EMPTY);
assertNotNull(cpInfo);
final JavaSource js = JavaSource.create(cpInfo);
js.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(final CompilationController cc) throws Exception {
final PackageElement packageElement = cc.getElements().getPackageElement("java.lang"); // NOI18N
for (Element elem : packageElement.getEnclosedElements()) {
if ("ProcessBuilder$1".equals(elem.getSimpleName().toString())) { // NOI18N
TypeElement te = (TypeElement) elem;
assertEquals(NestingKind.ANONYMOUS, te.getNestingKind());
break;
}
}
}
}, true);
}
项目:incubator-netbeans
文件:BeanModelBuilder.java
private void findBuilder() {
if (classElement.getNestingKind() != NestingKind.TOP_LEVEL || builder) {
return;
}
Collection<? extends BuilderResolver> resolvers = MimeLookup.getLookup(JavaFXEditorUtils.FXML_MIME_TYPE).lookupAll(BuilderResolver.class);
for (BuilderResolver r : resolvers) {
String builderName = r.findBuilderClass(compilationInfo, null, className);
if (builderName != null) {
FxBean builderBean = provider.getBeanInfo(builderName);
if (builderBean != null) {
resultInfo.setBuilder(builderBean);
builderBean.makeBuilder(resultInfo);
return;
}
}
}
}
项目:incubator-netbeans
文件:SerialVersionUID.java
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
if (treePath == null || !TreeUtilities.CLASS_TREE_KINDS.contains(treePath.getLeaf().getKind())) {
return null;
}
if (treePath.getLeaf().getKind() == Tree.Kind.INTERFACE) {
return null;
}
TypeElement type = (TypeElement) info.getTrees().getElement(treePath);
if (type == null) {
return null;
}
List<Fix> fixes = new ArrayList<Fix>();
fixes.add(new FixImpl(TreePathHandle.create(treePath, info), false).toEditorFix());
// fixes.add(new FixImpl(TreePathHandle.create(treePath, info), true));
if (!type.getNestingKind().equals(NestingKind.ANONYMOUS)) {
// add SuppressWarning only to non-anonymous class
fixes.addAll(FixFactory.createSuppressWarnings(info, treePath, SERIAL));
}
return fixes;
}
项目:incubator-netbeans
文件:TargetDescription.java
public static TargetDescription create(CompilationInfo info, TypeElement type, TreePath path, boolean allowForDuplicates, boolean iface) {
boolean canStatic = true;
if (iface) {
// interface cannot have static methods
canStatic = false;
} else {
if (type.getNestingKind() == NestingKind.ANONYMOUS ||
type.getNestingKind() == NestingKind.LOCAL ||
(type.getNestingKind() != NestingKind.TOP_LEVEL && !type.getModifiers().contains(Modifier.STATIC))) {
canStatic = false;
}
}
return new TargetDescription(Utilities.target2String(type),
ElementHandle.create(type),
TreePathHandle.create(path, info),
allowForDuplicates,
type.getSimpleName().length() == 0, iface, canStatic);
}
项目:incubator-netbeans
文件:JavadocGenerator.java
public String generateComment(TypeElement clazz, CompilationInfo javac) {
StringBuilder builder = new StringBuilder(
// "/**\n" + // NOI18N
"\n" // NOI18N
);
if (clazz.getNestingKind() == NestingKind.TOP_LEVEL) {
builder.append("@author ").append(author).append("\n"); // NOI18N
}
if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0) {
for (TypeParameterElement param : clazz.getTypeParameters()) {
builder.append("@param <").append(param.getSimpleName().toString()).append("> \n"); // NOI18N
}
}
if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 &&
JavadocUtilities.isDeprecated(javac, clazz)) {
builder.append("@deprecated\n"); // NOI18N
}
// builder.append("*/\n"); // NOI18N
return builder.toString();
}
项目:DUnit
文件:DUnitModelUtil.java
/**
* 获取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();
}
}
项目:Spyglass
文件:CompanionNamer.java
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();
}
}
项目:btrace.nb
文件:ErrorScanner.java
@Override
public Void visitClass(ClassTree node, Map<ErrorDescription, Integer> p) {
Element e = ci.getTrees().getElement(getCurrentPath());
if (e != null) {
if (((TypeElement)e).getNestingKind() != NestingKind.TOP_LEVEL) {
addError(node, ERR_ONLY_TOP_LEVEL_CLASS, p);
} else {
btraceClassName = ((TypeElement)e).getQualifiedName().toString();
if (e.getModifiers().contains(Modifier.PRIVATE) ||
e.getModifiers().contains(Modifier.PROTECTED)) {
addError(node, ERR_CLASS_PUBLIC_OR_DEFAULT, p);
} else if (!e.getModifiers().contains(Modifier.PUBLIC)) {
isShortSyntax = true;
}
}
}
return super.visitClass(node, p);
}
项目:btrace.nb
文件:ErrorScanner.java
@Override
public Void visitClass(ClassTree node, Map<ErrorDescription, Integer> p) {
Element e = ci.getTrees().getElement(getCurrentPath());
if (e != null) {
if (((TypeElement)e).getNestingKind() != NestingKind.TOP_LEVEL) {
addError(node, ERR_ONLY_TOP_LEVEL_CLASS, p);
} else {
btraceClassName = ((TypeElement)e).getQualifiedName().toString();
if (e.getModifiers().contains(Modifier.PRIVATE) ||
e.getModifiers().contains(Modifier.PROTECTED)) {
addError(node, ERR_CLASS_PUBLIC_OR_DEFAULT, p);
} else if (!e.getModifiers().contains(Modifier.PUBLIC)) {
isShortSyntax = true;
}
}
}
return super.visitClass(node, p);
}
项目:aidl2
文件:AptHelper.java
public TypeElement lookupStaticClass(DeclaredType type, CharSequence className, TypeMirror classType) {
for (TypeElement clazz : ElementFilter.typesIn(type.asElement().getEnclosedElements())) {
final Collection<? extends Modifier> modifiers = clazz.getModifiers();
if (clazz.getNestingKind() != NestingKind.MEMBER
|| !modifiers.contains(Modifier.STATIC)
|| !modifiers.contains(Modifier.PUBLIC)
|| !clazz.getKind().isClass()) {
continue;
}
if (!clazz.getSimpleName().contentEquals(className)) {
continue;
}
if (!types.isAssignable(clazz.asType(), classType)) {
continue;
}
return clazz;
}
return null;
}
项目:zerobuilder
文件:ProjectionValidatorB.java
private static TypeElement validateBeanType(TypeElement beanType) {
if (!hasParameterlessConstructor(beanType)) {
throw new ValidationException(BEAN_NO_DEFAULT_CONSTRUCTOR, beanType);
}
if (beanType.getModifiers().contains(PRIVATE)) {
throw new ValidationException(NESTING_KIND, beanType);
}
if (beanType.getNestingKind() == NestingKind.MEMBER &&
!beanType.getModifiers().contains(STATIC)) {
throw new ValidationException(NESTING_KIND, beanType);
}
if (beanType.getModifiers().contains(ABSTRACT)) {
throw new ValidationException(BEAN_ABSTRACT_CLASS, beanType);
}
if (!beanType.getTypeParameters().isEmpty()) {
throw new ValidationException(TYPE_PARAMS_BEAN, beanType);
}
return beanType;
}
项目:Akatsuki
文件:SourceTreeModel.java
static boolean enclosingClassValid(ProcessorContext context, TypeElement enclosingClass){
// protected, package-private, and public all allow same package
// access
if (enclosingClass.getModifiers().contains(Modifier.PRIVATE)) {
context.messager().printMessage(Kind.ERROR,
"class cannot be private",
enclosingClass);
return false;
}
if (enclosingClass.getNestingKind() != NestingKind.TOP_LEVEL
&& !enclosingClass.getModifiers().contains(Modifier.STATIC)) {
context.messager().printMessage(Kind.ERROR,
"class is nested but not static", enclosingClass);
return false;
}
return true;
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newElementWithMarker() {
TypeElement type = (TypeElement) model.newElementWithMarker(
"package foo.bar;",
"public class MyType {",
" ---> public class MyInnerType {",
" public void doNothing() { }",
" }",
"}");
assertEquals(ElementKind.CLASS, type.getKind());
assertEquals(NestingKind.MEMBER, type.getNestingKind());
assertEquals("MyInnerType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType.MyInnerType", type.toString());
assertEquals("doNothing",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newElementAnnotatedWith() {
TypeElement type = (TypeElement) model.newElementAnnotatedWith(
Deprecated.class,
"package foo.bar;",
"public class MyType {",
" @Deprecated public class MyInnerType {",
" public void doNothing() { }",
" }",
"}");
assertEquals(ElementKind.CLASS, type.getKind());
assertEquals(NestingKind.MEMBER, type.getNestingKind());
assertEquals("MyInnerType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType.MyInnerType", type.toString());
assertEquals("doNothing",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:buck
文件:AccessFlags.java
/**
* Gets the class access flags (see JVMS8 4.1) for the given type element as they should appear in
* the ClassNode of a class file. Inner-class specific flags are not allowed in that node,
* presumably for compatibility reasons.
*/
public int getAccessFlagsForClassNode(TypeElement e) {
// Static never makes it into the file for classes
int accessFlags = getAccessFlags(e) & ~Opcodes.ACC_STATIC;
if (e.getNestingKind() != NestingKind.TOP_LEVEL) {
if (e.getModifiers().contains(Modifier.PROTECTED)) {
// It looks like inner classes with protected visibility get marked as public, and then
// their InnerClasses attributes override that more specifically
accessFlags = (accessFlags & ~Opcodes.ACC_PROTECTED) | Opcodes.ACC_PUBLIC;
} else if (e.getModifiers().contains(Modifier.PRIVATE)) {
// It looks like inner classes with private visibility get marked as package, and then
// their InnerClasses attributes override that more specifically
accessFlags = (accessFlags & ~Opcodes.ACC_PRIVATE);
}
}
return accessFlags;
}
项目:incubator-netbeans
文件:ReplaceConstructorWithBuilderUI.java
private ReplaceConstructorWithBuilderUI(TreePathHandle constructor, CompilationInfo info) {
this.refactoring = new ReplaceConstructorWithBuilderRefactoring(constructor);
ExecutableElement contructorElement = (ExecutableElement) constructor.resolveElement(info);
this.name = contructorElement.getSimpleName().toString();
MethodTree constTree = (MethodTree) constructor.resolve(info).getLeaf();
paramaterNames = new ArrayList<String>();
parameterTypes = new ArrayList<String>();
parameterTypeVars = new ArrayList<Boolean>();
boolean varargs = contructorElement.isVarArgs();
List<? extends VariableElement> parameterElements = contructorElement.getParameters();
List<? extends VariableTree> parameters = constTree.getParameters();
for (int i = 0; i < parameters.size(); i++) {
VariableTree var = parameters.get(i);
paramaterNames.add(var.getName().toString());
String type = contructorElement.getParameters().get(i).asType().toString();
if(varargs && i+1 == parameters.size()) {
if(var.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
ArrayTypeTree att = (ArrayTypeTree) var.getType();
type = att.getType().toString();
type += "..."; //NOI18N
}
}
parameterTypes.add(type);
parameterTypeVars.add(parameterElements.get(i).asType().getKind() == TypeKind.TYPEVAR);
}
TypeElement typeEl = (TypeElement) contructorElement.getEnclosingElement();
if(typeEl.getNestingKind() != NestingKind.TOP_LEVEL) {
PackageElement packageOf = info.getElements().getPackageOf(typeEl);
builderFQN = packageOf.toString() + "." + typeEl.getSimpleName().toString();
} else {
builderFQN = typeEl.getQualifiedName().toString();
}
buildMethodName = "create" + typeEl.getSimpleName();
}
项目:incubator-netbeans
文件:ClassMetrics.java
@Hint(
displayName = "#DN_ClassTooComplex",
description = "#DESC_ClassTooComplex",
category = "metrics",
options = { Hint.Options.HEAVY, Hint.Options.QUERY },
enabled = false
)
@UseOptions(OPTION_COMPLEXITY_LIMIT)
@TriggerTreeKind(Tree.Kind.CLASS)
public static ErrorDescription tooComplexClass(HintContext ctx) {
ClassTree clazz = (ClassTree)ctx.getPath().getLeaf();
TypeElement e = (TypeElement)ctx.getInfo().getTrees().getElement(ctx.getPath());
if (e.getNestingKind() == NestingKind.ANONYMOUS) {
return null;
}
CyclomaticComplexityVisitor v = new CyclomaticComplexityVisitor();
v.scan(ctx.getPath(), null);
int complexity = v.getComplexity();
int limit = ctx.getPreferences().getInt(OPTION_COMPLEXITY_LIMIT, DEFAULT_COMPLEXITY_LIMIT);
if (complexity > limit) {
return ErrorDescriptionFactory.forName(ctx,
ctx.getPath(),
TEXT_ClassTooComplex(clazz.getSimpleName().toString(), complexity));
} else {
return null;
}
}
项目:incubator-netbeans
文件:CompletionFilter.java
@Override
public boolean isAccessible(Scope scope, TypeElement te) {
if (te == null || scope == null) {
return false;
}
if (te.getQualifiedName().toString().startsWith("REPL.") && te.getNestingKind() == NestingKind.TOP_LEVEL) {
return false;
}
return delegate.isAccessible(scope, te);
}
项目:incubator-netbeans
文件:InitializerCanBeStatic.java
@TriggerTreeKind(Tree.Kind.BLOCK)
public static ErrorDescription run(HintContext ctx) {
TreePath path = ctx.getPath();
if (((BlockTree)path.getLeaf()).isStatic()) {
return null;
}
TreePath parentPath = path.getParentPath();
if (parentPath == null) {
return null;
}
Tree l = parentPath.getLeaf();
if (!(l instanceof ClassTree)) {
return null;
}
Element el = ctx.getInfo().getTrees().getElement(parentPath);
if (el == null || !el.getKind().isClass()) {
return null;
}
TypeElement tel = (TypeElement)el;
// do not suggest for anonymous classes, local classes or members which are not static.
if (tel.getNestingKind() != NestingKind.TOP_LEVEL &&
(tel.getNestingKind() != NestingKind.MEMBER || !tel.getModifiers().contains(Modifier.STATIC))) {
return null;
}
InstanceRefFinder finder = new InstanceRefFinder(ctx.getInfo(), path);
finder.process();
if (finder.containsInstanceReferences() || finder.containsReferencesToSuper()) {
return null;
}
return ErrorDescriptionFactory.forTree(ctx, path, Bundle.TEXT_InitializerCanBeStatic(),
new MakeInitStatic(TreePathHandle.create(path, ctx.getInfo())).toEditorFix());
}
项目:incubator-netbeans
文件:InstanceRefFinder.java
/**
* If the variable is typed as a local class, record the local class; it may need to go along
* with the factored expression, unless the expression only uses some specific interface
* implemented by the local class.
*
* @param el
*/
private void addLocalClassVariable(Element el) {
TypeMirror tm = el.asType();
if (tm.getKind() != TypeKind.DECLARED) {
return;
}
Element e = ((DeclaredType)tm).asElement();
if (!(e instanceof TypeElement)) {
return;
}
TypeElement t = (TypeElement)e;
if (t.getNestingKind() == NestingKind.LOCAL) {
addLocalReference(t);
}
}
项目:incubator-netbeans
文件:JavadocGenerator.java
/**
* computes name of throws clause to work around
* <a href="http://www.netbeans.org/issues/show_bug.cgi?id=160414">issue 160414</a>.
*/
private static String resolveThrowsName(Element el, String fqn, ExpressionTree throwTree) {
boolean nestedClass = ElementKind.CLASS == el.getKind()
&& NestingKind.TOP_LEVEL != ((TypeElement) el).getNestingKind();
String insertName = nestedClass ? fqn : throwTree.toString();
return insertName;
}
项目:incubator-netbeans
文件:TopLevelClass.java
@TriggerPatterns(value = {
@TriggerPattern(value = JPAAnnotations.ENTITY),
@TriggerPattern(value = JPAAnnotations.EMBEDDABLE),
@TriggerPattern(value = JPAAnnotations.MAPPED_SUPERCLASS)})
public static ErrorDescription apply(HintContext hc) {
if (hc.isCanceled() || (hc.getPath().getLeaf().getKind() != Tree.Kind.IDENTIFIER || hc.getPath().getParentPath().getLeaf().getKind() != Tree.Kind.ANNOTATION)) {//NOI18N
return null;//we pass only if it is an annotation
}
final JPAProblemContext ctx = ModelUtils.getOrCreateCachedContext(hc);
if (ctx == null || hc.isCanceled()) {
return null;
}
TypeElement subject = ctx.getJavaClass();
if (subject.getNestingKind() == NestingKind.TOP_LEVEL){
return null;
}
TreePath par = hc.getPath();
while (par != null && par.getParentPath() != null && par.getLeaf().getKind() != Tree.Kind.CLASS) {
par = par.getParentPath();
}
Utilities.TextSpan underlineSpan = Utilities.getUnderlineSpan(
ctx.getCompilationInfo(), par.getLeaf());
return ErrorDescriptionFactory.forSpan(
hc,
underlineSpan.getStartOffset(),
underlineSpan.getEndOffset(),
NbBundle.getMessage(TopLevelClass.class, "MSG_NestedClassAsEntity"));
}
项目:react4j
文件:ComponentDescriptor.java
ComponentDescriptor( @Nonnull final String name,
@Nonnull final PackageElement packageElement,
@Nonnull final TypeElement element )
{
_name = Objects.requireNonNull( name );
_packageElement = Objects.requireNonNull( packageElement );
_element = Objects.requireNonNull( element );
if ( ElementKind.CLASS != element.getKind() )
{
throw new ReactProcessorException( "@ReactComponent target must be a class", element );
}
else if ( element.getModifiers().contains( Modifier.ABSTRACT ) )
{
throw new ReactProcessorException( "@ReactComponent target must not be abstract", element );
}
else if ( element.getModifiers().contains( Modifier.FINAL ) )
{
throw new ReactProcessorException( "@ReactComponent target must not be final", element );
}
else if ( NestingKind.TOP_LEVEL != element.getNestingKind() &&
!element.getModifiers().contains( Modifier.STATIC ) )
{
throw new ReactProcessorException( "@ReactComponent target must not be a non-static nested class", element );
}
final List<ExecutableElement> constructors = element.getEnclosedElements().stream().
filter( m -> m.getKind() == ElementKind.CONSTRUCTOR ).
map( m -> (ExecutableElement) m ).
collect( Collectors.toList() );
if ( !( 1 == constructors.size() &&
constructors.get( 0 ).getParameters().isEmpty() &&
!constructors.get( 0 ).getModifiers().contains( Modifier.PRIVATE ) ) )
{
throw new ReactProcessorException( "@ReactComponent target must have a single non-private, no-argument " +
"constructor or the default constructor", element );
}
}
项目:Spyglass
文件:CompanionGenerator.java
private PackageElement getPackage(final TypeElement type) {
if (type.getNestingKind() == NestingKind.TOP_LEVEL) {
return (PackageElement) type.getEnclosingElement();
} else {
return getPackage((TypeElement) type.getEnclosingElement());
}
}
项目:EgTest
文件:AnnotationCollector.java
private static boolean allowedNesting(NestingKind nk) {
switch (nk) {
case TOP_LEVEL: return true;
case MEMBER: return true;
case LOCAL: return false;
case ANONYMOUS: return false;
default: return false;
}
}
项目:qualifier
文件:StaticQualifierMetamodelProcessor.java
public static String getFlatName(TypeElement classRepresenter) {
if (classRepresenter.getNestingKind() == NestingKind.MEMBER) {
return classRepresenter.getEnclosingElement() + "" + classRepresenter.getSimpleName().toString();
} else {
return classRepresenter.getQualifiedName().toString();
}
}
项目:openjdk-jdk10
文件:Symbol.java
@DefinedBy(Api.LANGUAGE_MODEL)
public NestingKind getNestingKind() {
complete();
if (owner.kind == PCK)
return NestingKind.TOP_LEVEL;
else if (name.isEmpty())
return NestingKind.ANONYMOUS;
else if (owner.kind == MTH)
return NestingKind.LOCAL;
else
return NestingKind.MEMBER;
}
项目:openjdk9
文件:Symbol.java
@DefinedBy(Api.LANGUAGE_MODEL)
public NestingKind getNestingKind() {
complete();
if (owner.kind == PCK)
return NestingKind.TOP_LEVEL;
else if (name.isEmpty())
return NestingKind.ANONYMOUS;
else if (owner.kind == MTH)
return NestingKind.LOCAL;
else
return NestingKind.MEMBER;
}
项目:cloudkeeper
文件:TypeDeclarationImpl.java
@Override
public NestingKind getNestingKind() {
require(State.FINISHED);
boolean isTopLevel = enclosingTypeDeclaration == null;
return isTopLevel
? NestingKind.TOP_LEVEL
: NestingKind.MEMBER;
}
项目:pojo-updater-compiler
文件:UpdaterGenerator.java
/**
* Initialize necessary fields
*
* @param packageElement Package element which will be used while generating java source file
* @param typeElement The definition of a certain class for which we are generating updater
*/
public UpdaterGenerator(PackageElement packageElement, TypeElement typeElement) {
mPackageElement = packageElement;
final ClassName entityClassName = ClassName.get(typeElement);
/**
* Generate parameter definitions of method {@link Updater#update(Object, Object)}
*/
mParamSpecOfOldEntity = ParameterSpec.builder(entityClassName, "old" + typeElement.getSimpleName(), Modifier.FINAL).build();
mParamSpecOfNewEntity = ParameterSpec.builder(entityClassName, "new" + typeElement.getSimpleName(), Modifier.FINAL).build();
/**
* the 'extends' part of generated updater class
*/
mUpdaterNameWithTypeVariable = ParameterizedTypeName.get(ClassName.get(Updater.class), TypeVariableName.get(typeElement.asType()));
/**
* Class name of updater class
*/
mGeneratedClassName = entityClassName.simpleName() + Updater.CLASS_PREFIX;
mIsTopClass = typeElement.getNestingKind() == NestingKind.TOP_LEVEL;
mIsStatic = typeElement.getModifiers().contains(Modifier.STATIC);
mUpdaterAnnotation = AnnotationSpec.builder(UpdaterManager.ManagedUpdater.class)
.addMember("value", "$T.class", typeElement)
.build();
}
项目:zerobuilder
文件:TypeValidator.java
static void validateContextClass(TypeElement type) throws ValidationException {
Set<Modifier> modifiers = type.getModifiers();
NestingKind nestingKind = type.getNestingKind();
if (modifiers.contains(PRIVATE)) {
throw new ValidationException(NESTING_KIND, type);
}
if (!ALLOWED_NESTING_KINDS.contains(nestingKind)
|| nestingKind == MEMBER && !modifiers.contains(STATIC)) {
throw new ValidationException(NESTING_KIND, type);
}
}
项目:Akatsuki
文件:SourceTreeModel.java
private static boolean enclosingClassValid(ProcessorContext context, Element element) {
Element enclosingElement = element.getEnclosingElement();
while (enclosingElement != null) {
// skip until we find a class
if (!enclosingElement.getKind().equals(ElementKind.CLASS))
break;
if (!enclosingElement.getKind().equals(ElementKind.CLASS)) {
context.messager().printMessage(Kind.ERROR,
"enclosing element(" + enclosingElement.toString() + ") is not a class",
element);
return false;
}
TypeElement enclosingClass = (TypeElement) enclosingElement;
// protected, package-private, and public all allow same package
// access
if (enclosingClass.getModifiers().contains(Modifier.PRIVATE)) {
context.messager().printMessage(Kind.ERROR,
"enclosing class (" + enclosingElement.toString() + ") cannot be private",
element);
return false;
}
if (enclosingClass.getNestingKind() != NestingKind.TOP_LEVEL
&& !enclosingClass.getModifiers().contains(Modifier.STATIC)) {
context.messager().printMessage(Kind.ERROR,
"enclosing class is nested but not static", element);
return false;
}
enclosingElement = enclosingClass.getEnclosingElement();
}
return true;
}
项目:Akatsuki
文件:SourceClassModel.java
public ClassInfo asClassInfo() {
String fqpn = fullyQualifiedPackageName();
String className;
if (enclosingClass.getNestingKind() != NestingKind.TOP_LEVEL) {
// in case of the static class, we get all the nested classes and
// replace '.' with '$'
className = CharMatcher.is('.')
.replaceFrom(fullyQualifiedName().replace(fqpn + ".", ""), '$');
} else {
className = simpleName();
}
return new ClassInfo(fqpn, className);
}
项目:FreeBuilder
文件:ModelRuleTest.java
@Test
public void newType() {
TypeElement type = model.newType(
"package foo.bar;",
"public class MyType {",
" public void doNothing() { }",
"}");
assertEquals(ElementKind.CLASS, type.getKind());
assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind());
assertEquals("MyType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType", type.toString());
assertEquals("doNothing",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newType_class() {
TypeElement type = model.newType(
"package foo.bar;",
"public class MyType {",
" public void doNothing() { }",
"}");
assertEquals(ElementKind.CLASS, type.getKind());
assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind());
assertEquals("MyType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType", type.toString());
assertEquals("doNothing",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newType_interface() {
TypeElement type = model.newType(
"package foo.bar;",
"public interface MyType {",
" public void doNothing();",
"}");
assertEquals(ElementKind.INTERFACE, type.getKind());
assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind());
assertEquals("MyType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType", type.toString());
assertEquals("doNothing",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newType_enum() {
TypeElement type = model.newType(
"package foo.bar;",
"public enum MyType { A, B; }");
assertEquals(ElementKind.ENUM, type.getKind());
assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind());
assertEquals("MyType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType", type.toString());
}
项目:FreeBuilder
文件:ModelTest.java
@Test
public void newType_annotation() {
TypeElement type = model.newType(
"package foo.bar;",
"public @interface MyType {",
" String param();",
"}");
assertEquals(ElementKind.ANNOTATION_TYPE, type.getKind());
assertEquals(NestingKind.TOP_LEVEL, type.getNestingKind());
assertEquals("MyType", type.getSimpleName().toString());
assertEquals("foo.bar.MyType", type.toString());
assertEquals("param",
getOnlyElement(methodsIn(type.getEnclosedElements())).getSimpleName().toString());
}
项目:bsoneer
文件:ModelExt.java
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;
}
项目:Databind
文件:EmulImportElement.java
public EmulImportElement(TypeMirror type,
Element owner,
Name simpleName,
Name qualifiedName,
NestingKind nestingKind,
boolean _static,
boolean asterisk) {
super(type, owner, simpleName, qualifiedName, nestingKind);
this._static = _static;
this.asterisk = asterisk;
}