Java Code Examples for javax.lang.model.element.NestingKind#TOP_LEVEL

The following examples show how to use javax.lang.model.element.NestingKind#TOP_LEVEL . 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: SourceTreeModel.java    From Akatsuki with Apache License 2.0 6 votes vote down vote up
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;
	}
 
Example 2
Source File: TargetDescription.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: AccessFlags.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: BeanModelBuilder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
            }
        }
    }
}
 
Example 5
Source File: SourceClassModel.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: SourceTreeModel.java    From Akatsuki with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: ReplaceConstructorWithBuilderUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: CompletionFilter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: Symbol.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: ExternalDomainMetaFactory.java    From doma with Apache License 2.0 5 votes vote down vote up
private void validateEnclosingElement(Element element) {
  TypeElement typeElement = ctx.getMoreElements().toTypeElement(element);
  if (typeElement == null) {
    return;
  }
  String simpleName = typeElement.getSimpleName().toString();
  if (simpleName.contains(Constants.BINARY_NAME_DELIMITER)
      || simpleName.contains(Constants.TYPE_NAME_DELIMITER)) {
    throw new AptException(
        Message.DOMA4280, typeElement, new Object[] {typeElement.getQualifiedName()});
  }
  NestingKind nestingKind = typeElement.getNestingKind();
  if (nestingKind == NestingKind.TOP_LEVEL) {
    return;
  } else if (nestingKind == NestingKind.MEMBER) {
    Set<Modifier> modifiers = typeElement.getModifiers();
    if (modifiers.containsAll(Arrays.asList(Modifier.STATIC, Modifier.PUBLIC))) {
      validateEnclosingElement(typeElement.getEnclosingElement());
    } else {
      throw new AptException(
          Message.DOMA4278, typeElement, new Object[] {typeElement.getQualifiedName()});
    }
  } else {
    throw new AptException(
        Message.DOMA4279, typeElement, new Object[] {typeElement.getQualifiedName()});
  }
}
 
Example 11
Source File: ClassTypeImpl.java    From FreeBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
  return (enclosingElement.getKind() == ElementKind.PACKAGE)
      ? NestingKind.TOP_LEVEL : NestingKind.MEMBER;
}
 
Example 12
Source File: ArchiveProbeObject.java    From revapi with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
    return NestingKind.TOP_LEVEL;
}
 
Example 13
Source File: InMemoryJavaFile.java    From FreeBuilder with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
  return qualifiedName.isTopLevel() ? NestingKind.TOP_LEVEL : NestingKind.MEMBER;
}
 
Example 14
Source File: ELJavaCompletion.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isAccessibleClass(TypeElement te) {
    NestingKind nestingKind = te.getNestingKind();
    return (nestingKind == NestingKind.TOP_LEVEL && te.getModifiers().contains(Modifier.PUBLIC));
}
 
Example 15
Source File: ProcessorContext.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public String nameOf(final TypeElement e) {
	if (e.getNestingKind() == NestingKind.TOP_LEVEL) { return e.getQualifiedName().toString(); }
	return nameOf((TypeElement) e.getEnclosingElement()) + "." + e.getSimpleName().toString();
}
 
Example 16
Source File: TypeSimplifier.java    From auto with Apache License 2.0 4 votes vote down vote up
private static TypeElement topLevelType(TypeElement type) {
  while (type.getNestingKind() != NestingKind.TOP_LEVEL) {
    type = MoreElements.asType(type.getEnclosingElement());
  }
  return type;
}
 
Example 17
Source File: GeneratedTypeElement.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static GeneratedTypeElement newIosType(
    String name, ElementKind kind, TypeElement superclass, String header) {
  return new GeneratedTypeElement(
      name, kind, null, superclass != null ? superclass.asType() : null, NestingKind.TOP_LEVEL,
      header, true, false);
}
 
Example 18
Source File: ElementUtil.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static boolean isTopLevel(TypeElement type) {
  return type.getNestingKind() == NestingKind.TOP_LEVEL;
}
 
Example 19
Source File: VirtualFile.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
	return NestingKind.TOP_LEVEL;
}
 
Example 20
Source File: DefaultSourceLevelQueryImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
    return NestingKind.TOP_LEVEL;
}