Java Code Examples for javax.lang.model.element.ElementKind#isInterface()

The following examples show how to use javax.lang.model.element.ElementKind#isInterface() . These examples are extracted from open source projects. 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 Project: jdk8u60   File: JavacTrees.java    License: GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 2
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 3
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 4
Source Project: auto   File: AutoValueOrOneOfProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Checks that, if the given {@code @AutoValue} or {@code @AutoOneOf} class is nested, it is
 * static and not private. This check is not necessary for correctness, since the generated code
 * would not compile if the check fails, but it produces better error messages for the user.
 */
final void checkModifiersIfNested(TypeElement type) {
  ElementKind enclosingKind = type.getEnclosingElement().getKind();
  if (enclosingKind.isClass() || enclosingKind.isInterface()) {
    if (type.getModifiers().contains(Modifier.PRIVATE)) {
      errorReporter.abortWithError(
          type, "@%s class must not be private", simpleAnnotationName);
    } else if (Visibility.effectiveVisibilityOfElement(type).equals(Visibility.PRIVATE)) {
      // The previous case, where the class itself is private, is much commoner so it deserves
      // its own error message, even though it would be caught by the test here too.
      errorReporter.abortWithError(
          type, "@%s class must not be nested in a private class", simpleAnnotationName);
    }
    if (!type.getModifiers().contains(Modifier.STATIC)) {
      errorReporter.abortWithError(
          type, "Nested @%s class must be static", simpleAnnotationName);
    }
  }
  // In principle type.getEnclosingElement() could be an ExecutableElement (for a class
  // declared inside a method), but since RoundEnvironment.getElementsAnnotatedWith doesn't
  // return such classes we won't see them here.
}
 
Example 5
Source Project: hottub   File: JavacTrees.java    License: GNU General Public License v2.0 6 votes vote down vote up
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 6
Source Project: netbeans   File: InstantRenamePerformer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * computes accessibility of members of nested classes
 * @param e member
 * @return {@code true} if the member cannot be accessed outside the outer class
 * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=169377">169377</a>
 */
private static boolean isInaccessibleOutsideOuterClass(Element e, ElementUtilities eu) {
    Element enclosing = e.getEnclosingElement();
    boolean isStatic = e.getModifiers().contains(Modifier.STATIC);
    ElementKind kind = e.getKind();
    if (isStatic || kind.isClass() || kind.isInterface() || kind.isField()) {
        // static declaration of nested class, interface, enum, ann type, method, field
        // or inner class
        return isAnyEncloserPrivate(e);
    } else if (enclosing != null && kind == ElementKind.METHOD) {
        // final is enum, ann type and some classes
        ElementKind enclosingKind = enclosing.getKind();
        boolean isEnclosingFinal = enclosing.getModifiers().contains(Modifier.FINAL)
                // ann type is not final even if it cannot be subclassed
                || enclosingKind == ElementKind.ANNOTATION_TYPE;
        return isAnyEncloserPrivate(e) && !eu.overridesMethod((ExecutableElement) e) && !eu.implementsMethod((ExecutableElement)e) &&
                (isEnclosingFinal || !isOverriddenInsideOutermostEnclosingClass((ExecutableElement)e, eu));
    }
    return false;
}
 
Example 7
private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
    Symbol javadocSymbol = getElement(path);
    if (javadocSymbol == null)
        return null;
    ElementKind kind = javadocSymbol.getKind();
    List<? extends Symbol> params = List.nil();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        MethodSymbol ee = (MethodSymbol) javadocSymbol;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        ClassSymbol te = (ClassSymbol) javadocSymbol;
        params = te.getTypeParameters();
    }

    for (Symbol param : params) {
        if (param.getSimpleName() == ptag.getName().getName()) {
            return param;
        }
    }
    return null;
}
 
Example 8
Source Project: netbeans   File: ElementHandle.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isSameKind (ElementKind k1, ElementKind k2) {
    if ((k1 == k2) ||
       (k1 == ElementKind.OTHER && (k2.isClass() || k2.isInterface())) ||     
       (k2 == ElementKind.OTHER && (k1.isClass() || k1.isInterface()))) {
        return true;
    }
    return false;
}
 
Example 9
Source Project: netbeans   File: ElementHandle.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link ElementHandle} representing a {@link TypeElement}.
 * @param kind the {@link ElementKind} of the {@link TypeElement},
 * allowed values are {@link ElementKind#CLASS}, {@link ElementKind#INTERFACE},
 * {@link ElementKind#ENUM} and {@link ElementKind#ANNOTATION_TYPE}.
 * @param binaryName the class binary name as specified by JLS ยง13.1
 * @return the created {@link ElementHandle}
 * @throws IllegalArgumentException if kind is neither class nor interface
 * @since 0.98
 */
@NonNull
public static ElementHandle<TypeElement> createTypeElementHandle(
    @NonNull final ElementKind kind,
    @NonNull final String binaryName) throws IllegalArgumentException {
    Parameters.notNull("kind", kind);   //NOI18N
    Parameters.notNull("binaryName", binaryName);   //NOI18N
    if (!kind.isClass() && !kind.isInterface()) {
        throw new IllegalArgumentException(kind.toString());
    }
    return new ElementHandle<TypeElement>(kind, binaryName);
}
 
Example 10
Source Project: buck   File: DescriptorFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public String getDescriptor(Element element) {
  ElementKind kind = element.getKind();
  if (kind.isClass() || kind.isInterface()) {
    return null;
  }
  return getType(element).getDescriptor();
}
 
Example 11
Source Project: netbeans   File: RenameRefactoringUI.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CustomRefactoringPanel getPanel(ChangeListener parent) {
    if (panel == null) {
        String suffix = "";
        if(handle != null && handle.getKind() == Tree.Kind.LABELED_STATEMENT) {
            suffix = getString("LBL_Label");
        } else if (handle != null && handle.getElementHandle() !=null) {
            ElementKind kind = handle.getElementHandle().getKind();
            if (kind!=null && (kind.isClass() || kind.isInterface())) {
                suffix  = kind.isInterface() ? getString("LBL_Interface") : getString("LBL_Class");
            } else if (kind == ElementKind.METHOD) {
                suffix = getString("LBL_Method");
            } else if (kind == ElementKind.FIELD) {
                suffix = getString("LBL_Field");
            } else if (kind == ElementKind.LOCAL_VARIABLE) {
                suffix = getString("LBL_LocalVar");
            } else if (kind == ElementKind.PACKAGE || (handle == null && fromListener)) {
                suffix = pkgRename ? getString("LBL_Package") : getString("LBL_Folder");
            } else if (kind == ElementKind.PARAMETER) {
                suffix = getString("LBL_Parameter");
            }
        }
        suffix = suffix + " " + this.oldName; // NOI18N
        panel = new RenamePanel(handle, newName, parent, NbBundle.getMessage(RenamePanel.class, "LBL_Rename") + " " + suffix, !fromListener, fromListener && !byPassPakageRename);
    }
    return panel;
}
 
Example 12
Source Project: auto-parcel   File: AutoParcelProcessor.java    License: Apache License 2.0 5 votes vote down vote up
private void checkModifiersIfNested(TypeElement type) {
    ElementKind enclosingKind = type.getEnclosingElement().getKind();
    if (enclosingKind.isClass() || enclosingKind.isInterface()) {
        if (type.getModifiers().contains(PRIVATE)) {
            mErrorReporter.abortWithError("@AutoParcel class must not be private", type);
        }
        if (!type.getModifiers().contains(STATIC)) {
            mErrorReporter.abortWithError("Nested @AutoParcel class must be static", type);
        }
    }
    // In principle type.getEnclosingElement() could be an ExecutableElement (for a class
    // declared inside a method), but since RoundEnvironment.getElementsAnnotatedWith doesn't
    // return such classes we won't see them here.
}
 
Example 13
Source Project: openjdk-jdk9   File: Utils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public TypeElement getEnclosingTypeElement(Element e) {
    if (e.getKind() == ElementKind.PACKAGE)
        return null;
    Element encl = e.getEnclosingElement();
    ElementKind kind = encl.getKind();
    if (kind == ElementKind.PACKAGE)
        return null;
    while (!(kind.isClass() || kind.isInterface())) {
        encl = encl.getEnclosingElement();
    }
    return (TypeElement)encl;
}
 
Example 14
Source Project: paperparcel   File: AdapterValidator.java    License: Apache License 2.0 4 votes vote down vote up
ValidationReport<TypeElement> validate(TypeElement element) {
  ValidationReport.Builder<TypeElement> builder = ValidationReport.about(element);
  if (element.getKind() != ElementKind.CLASS) {
    builder.addError(ErrorMessages.ADAPTER_MUST_BE_CLASS);
  }
  if (element.getModifiers().contains(Modifier.ABSTRACT)) {
    builder.addError(ErrorMessages.ADAPTER_IS_ABSTRACT);
  }
  if (Visibility.ofElement(element) != Visibility.PUBLIC) {
    builder.addError(ErrorMessages.ADAPTER_MUST_BE_PUBLIC);
  } else if (Visibility.effectiveVisibilityOfElement(element) != Visibility.PUBLIC) {
    builder.addError(ErrorMessages.ADAPTER_VISIBILITY_RESTRICTED);
  }
  ElementKind enclosingKind = element.getEnclosingElement().getKind();
  if (enclosingKind.isClass() || enclosingKind.isInterface()) {
    if (!element.getModifiers().contains(Modifier.STATIC)) {
      builder.addError(ErrorMessages.NESTED_ADAPTER_MUST_BE_STATIC);
    }
  }
  TypeMirror adaptedType = Utils.getAdaptedType(elements, types, asDeclared(element.asType()));
  if (Utils.isRawType(adaptedType)) {
    builder.addError(ErrorMessages.ADAPTER_TYPE_ARGUMENT_HAS_RAW_TYPE);
  } else if (Utils.containsWildcards(adaptedType)) {
    builder.addError(ErrorMessages.ADAPTER_TYPE_ARGUMENT_HAS_WILDCARDS);
  } else {
    List<TypeParameterElement> missingParameters = findMissingParameters(element, adaptedType);
    for (TypeParameterElement missingParameter : missingParameters) {
      builder.addError(
          String.format(
              ErrorMessages.ADAPTER_TYPE_ARGUMENT_MISSING_PARAMETER,
              missingParameter.getSimpleName().toString()),
          missingParameter);
    }
  }

  ExecutableElement mainConstructor = Utils.findLargestPublicConstructor(element);
  if (mainConstructor != null) {
    builder.addSubreport(validateConstructor(mainConstructor));
  } else if (adaptedType != null) {
    if (!Utils.isSingletonAdapter(elements, types, element, adaptedType)) {
      builder.addError(ErrorMessages.ADAPTER_MUST_HAVE_PUBLIC_CONSTRUCTOR);
    }
  }
  return builder.build();
}
 
Example 15
Source Project: netbeans   File: TopClassFinder.java    License: Apache License 2.0 4 votes vote down vote up
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 16
Source Project: netbeans   File: TopClassFinder.java    License: Apache License 2.0 4 votes vote down vote up
static boolean isTestable(TypeElement typeDeclElement) {
    ElementKind elemKind = typeDeclElement.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 17
Source Project: netbeans   File: PersistentClassIndex.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <T> void search (
        @NonNull final ElementHandle<?> element,
        @NonNull final Set<? extends UsageType> usageType,
        @NonNull final Set<? extends ClassIndex.SearchScopeType> scope,
        @NonNull final Convertor<? super Document, T> convertor,
        @NonNull final Set<? super T> result) throws InterruptedException, IOException {
    Parameters.notNull("element", element); //NOI18N
    Parameters.notNull("usageType", usageType); //NOI18N
    Parameters.notNull("scope", scope); //NOI18N
    Parameters.notNull("convertor", convertor); //NOI18N
    Parameters.notNull("result", result);   //NOI18N
    final Pair<Convertor<? super Document, T>,Index> ctu = indexPath.getPatch(convertor);
    try {
        final String binaryName = SourceUtils.getJVMSignature(element)[0];
        final ElementKind kind = element.getKind();
        if (kind == ElementKind.PACKAGE) {
            IndexManager.priorityAccess(() -> {
                final Query q = QueryUtil.scopeFilter(
                        QueryUtil.createPackageUsagesQuery(binaryName,usageType,Occur.SHOULD),
                        scope);
                if (q != null) {
                    index.query(result, ctu.first(), DocumentUtil.declaredTypesFieldSelector(false, false), cancel.get(), q);
                    if (ctu.second() != null) {
                        ctu.second().query(result, convertor, DocumentUtil.declaredTypesFieldSelector(false, false), cancel.get(), q);
                    }
                }
                return null;
            });
        } else if (kind.isClass() ||
                   kind.isInterface() ||
                   kind == ElementKind.OTHER) {
            if (BinaryAnalyser.OBJECT.equals(binaryName)) {
                getDeclaredElements(
                    "", //NOI18N
                    ClassIndex.NameKind.PREFIX,
                    scope,
                    DocumentUtil.declaredTypesFieldSelector(false, false),
                    convertor,
                    result);
            } else {
                IndexManager.priorityAccess(() -> {
                    final Query usagesQuery = QueryUtil.scopeFilter(
                            QueryUtil.createUsagesQuery(binaryName, usageType, Occur.SHOULD),
                            scope);
                    if (usagesQuery != null) {
                        index.query(result, ctu.first(), DocumentUtil.declaredTypesFieldSelector(false, false), cancel.get(), usagesQuery);
                        if (ctu.second() != null) {
                            ctu.second().query(result, convertor, DocumentUtil.declaredTypesFieldSelector(false, false), cancel.get(), usagesQuery);
                        }
                    }
                    return null;
                });
            }
        } else {
            throw new IllegalArgumentException(element.toString());
        }
    } catch (IOException ioe) {
        this.<Void,IOException>handleException(null, ioe, root);
    }
}
 
Example 18
Source Project: netbeans   File: TopClassFinder.java    License: Apache License 2.0 4 votes vote down vote up
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 19
Source Project: j2objc   File: ElementUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isTypeElement(Element e) {
  ElementKind kind = e.getKind();
  return kind.isClass() || kind.isInterface();
}
 
Example 20
Source Project: j2objc   File: TypeUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isInterface(TypeMirror t) {
  ElementKind kind = getDeclaredTypeKind(t);
  return kind != null ? kind.isInterface() : false;
}