javax.lang.model.element.NestingKind Java Examples

The following examples show how to use javax.lang.model.element.NestingKind. 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: 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 #2
Source File: SerialVersionUID.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: LambdaTypeElement.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public LambdaTypeElement(
    String name, Element enclosingElement, TypeMirror superclass, boolean isWeakOuter) {
  super(name, ElementKind.CLASS, enclosingElement, superclass, NestingKind.ANONYMOUS, null,
        false, false);
  this.isWeakOuter = isWeakOuter;
  addModifiers(Modifier.PRIVATE);
}
 
Example #4
Source File: ModelTest.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #5
Source File: Symbol.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 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 #6
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 #7
Source File: InferredTypeElement.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public NestingKind getNestingKind() {
  // We'll never need to infer local or anonymous classes, so there are only two options left
  // and we can tell the difference:
  if (getEnclosingElement() instanceof InferredTypeElement) {
    return NestingKind.MEMBER;
  }

  return NestingKind.TOP_LEVEL;
}
 
Example #8
Source File: EnclosingEnvironmentNullness.java    From NullAway with MIT License 5 votes vote down vote up
/** Is t an anonymous inner class or a lambda? */
private boolean isValidTreeType(Tree t) {
  if (t instanceof LambdaExpressionTree) {
    return true;
  }
  if (t instanceof ClassTree) {
    NestingKind nestingKind = ASTHelpers.getSymbol((ClassTree) t).getNestingKind();
    return nestingKind.equals(NestingKind.ANONYMOUS) || nestingKind.equals(NestingKind.LOCAL);
  }
  return false;
}
 
Example #9
Source File: InternalDomainMetaFactory.java    From doma with Apache License 2.0 5 votes vote down vote up
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.DOMA4277, 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.DOMA4275, typeElement, new Object[] {typeElement.getQualifiedName()});
    }
  } else {
    throw new AptException(
        Message.DOMA4276, typeElement, new Object[] {typeElement.getQualifiedName()});
  }
}
 
Example #10
Source File: ModelRuleTest.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #11
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 #12
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
  // check if the class is excluded according to the filter
  // if so, set the flag to match within the class to false
  // NOTE: for this mechanism to work, we rely on the enclosing ClassTree
  // always being visited before code within that class.  We also
  // assume that a single checker object is not being
  // used from multiple threads
  Symbol.ClassSymbol classSymbol = ASTHelpers.getSymbol(tree);
  // we don't want to update the flag for nested classes.
  // ideally we would keep a stack of flags to handle nested types,
  // but this is not easy within the Error Prone APIs
  NestingKind nestingKind = classSymbol.getNestingKind();
  if (!nestingKind.isNested()) {
    matchWithinClass = !isExcludedClass(classSymbol);
    // since we are processing a new top-level class, invalidate any cached
    // results for previous classes
    handler.onMatchTopLevelClass(this, tree, state, classSymbol);
    getNullnessAnalysis(state).invalidateCaches();
    initTree2PrevFieldInit.clear();
    class2Entities.clear();
    class2ConstructorUninit.clear();
    computedNullnessMap.clear();
    EnclosingEnvironmentNullness.instance(state.context).clear();
  }
  if (matchWithinClass) {
    // we need to update the environment before checking field initialization, as the latter
    // may run dataflow analysis
    if (nestingKind.equals(NestingKind.LOCAL) || nestingKind.equals(NestingKind.ANONYMOUS)) {
      updateEnvironmentMapping(tree, state);
    }
    checkFieldInitialization(tree, state);
  }
  return Description.NO_MATCH;
}
 
Example #13
Source File: GeneratedTypeElement.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static GeneratedTypeElement newEmulatedType(
    String qualifiedName, ElementKind kind, TypeMirror superclass) {
  int idx = qualifiedName.lastIndexOf('.');
  String packageName = idx < 0 ? "" : qualifiedName.substring(0, idx);
  PackageElement packageElement = new GeneratedPackageElement(packageName);
  return new GeneratedTypeElement(
      qualifiedName.substring(idx + 1), kind, packageElement, superclass, NestingKind.TOP_LEVEL,
      null, false, false);
}
 
Example #14
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 #15
Source File: TypeElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public NestingKind getNestingKind() {
	ReferenceBinding refBinding = (ReferenceBinding)_binding;
	if (refBinding.isAnonymousType()) {
		return NestingKind.ANONYMOUS;
	} else if (refBinding.isLocalType()) {
		return NestingKind.LOCAL;
	} else if (refBinding.isMemberType()) {
		return NestingKind.MEMBER;
	}
	return NestingKind.TOP_LEVEL;
}
 
Example #16
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 #17
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isAnonymousType(TypeMirror type) {
    if (type.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) type;
        TypeElement typeElem = (TypeElement) dt.asElement();
        if (typeElem.getNestingKind() == NestingKind.ANONYMOUS) {
            return true;
        }
    }
    return false;
}
 
Example #18
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override @DefinedBy(Api.COMPILER)
public NestingKind getNestingKind() {
    return null;
}
 
Example #19
Source File: JavacFiler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public NestingKind getNestingKind() {
    return javaFileObject.getNestingKind();
}
 
Example #20
Source File: ModuleNamesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
    return null;
}
 
Example #21
Source File: NestedZipEntryJavaFileObject.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
	return null; // nesting level not known
}
 
Example #22
Source File: LazyTypeCompletionItem.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) || (nestingKind == NestingKind.MEMBER && te.getModifiers().contains(Modifier.STATIC));
}
 
Example #23
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 #24
Source File: InMemoryJavaFileObject.java    From spring-init with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
	return null;
}
 
Example #25
Source File: JavacFiler.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public NestingKind getNestingKind() {
    return javaFileObject.getNestingKind();
}
 
Example #26
Source File: FileManager.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public NestingKind getNestingKind() {
    return delegate.getNestingKind();
}
 
Example #27
Source File: JavacFiler.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@DefinedBy(Api.COMPILER)
public NestingKind getNestingKind() {
    return javaFileObject.getNestingKind();
}
 
Example #28
Source File: ElementStructureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
    return null;
}
 
Example #29
Source File: TreeBackedTypeElement.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public NestingKind getNestingKind() {
  return underlyingElement.getNestingKind();
}
 
Example #30
Source File: SmartFileObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public NestingKind getNestingKind() {
    return file.getNestingKind();
}