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

The following are Jave code examples for showing how to use isClass() 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: incubator-netbeans   File: JavadocImports.java   View Source Code Vote up 6 votes
private static Element paramElementFor(Element methodOrClass, ParamTree ptag) {
    ElementKind kind = methodOrClass.getKind();
    List<? extends Element> params = Collections.emptyList();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        ExecutableElement ee = (ExecutableElement) methodOrClass;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        TypeElement te = (TypeElement) methodOrClass;
        params = te.getTypeParameters();
    }

    for (Element param : params) {
        if (param.getSimpleName().contentEquals(ptag.getName().getName())) {
            return param;
        }
    }
    return null;
}
 
Example 2
Project: openjdk-jdk10   File: JavacTrees.java   View Source Code Vote up 6 votes
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
Project: incubator-netbeans   File: InstantRenamePerformer.java   View Source Code Vote up 6 votes
/**
 * 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 4
Project: GitHub   File: Round.java   View Source Code Vote up 5 votes
private TypeElement enclosingTypeOf(Element element) {
  for (Element e = element; e != null;) {
    ElementKind kind = e.getKind();
    if (kind.isClass() || kind.isInterface()) {
      return (TypeElement) e;
    }
    e = e.getEnclosingElement();
  }
  throw new NoSuchElementException();
}
 
Example 5
Project: incubator-netbeans   File: ElementHandle.java   View Source Code Vote up 5 votes
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 6
Project: incubator-netbeans   File: ElementHandle.java   View Source Code Vote up 5 votes
/**
 * Tests if the handle has this same signature as the parameter.
 * The handles has the same signatures if it is resolved into the same
 * element in the same {@link javax.tools.JavaCompiler} task, but may be resolved into
 * the different {@link Element} in the different {@link javax.tools.JavaCompiler} task.
 * @param element to be checked
 * @return true if this handle resolves into the same {@link Element}
 * in the same {@link javax.tools.JavaCompiler} task.
 */
public boolean signatureEquals (@NonNull final T element) {
    final ElementKind ek = element.getKind();
    final ElementKind thisKind = getKind();
    if ((ek != thisKind) && !(thisKind == ElementKind.OTHER && (ek.isClass() || ek.isInterface()))) {
        return false;
    }
    final ElementHandle<T> handle = create (element);
    return signatureEquals (handle);
}
 
Example 7
Project: incubator-netbeans   File: ElementHandle.java   View Source Code Vote up 5 votes
/**
 * 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 8
Project: incubator-netbeans   File: RenameRefactoringUI.java   View Source Code Vote up 5 votes
@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 9
Project: android-auto-mapper   File: AutoMappperProcessor.java   View Source Code Vote up 5 votes
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 10
Project: incubator-netbeans   File: InstanceRefFinder.java   View Source Code Vote up 5 votes
protected TypeElement findOwnerType(Element el) {
    Element t;
    if (el instanceof TypeElement) {
        t = ((TypeElement)el);
    } else {
        t = ci.getElementUtilities().enclosingTypeElement(el);
        if (t == null) {
            return null;
        }
    }
    ElementKind k = t.getKind();
    TypeMirror declType = ci.getTypes().erasure(t.asType());
    
    for (TypeElement enclType = enclosingType; enclType != null; enclType = ci.getElementUtilities().enclosingTypeElement(enclType)) {
        if (ci.getTypes().isSubtype(ci.getTypes().erasure(enclType.asType()), declType)) {
            if (k.isClass()) {
                return enclType;
            } else if (k == ElementKind.INTERFACE) {
                if (t.getModifiers().contains(Modifier.DEFAULT)) {
                    return enclType;
                }
            }
            break;
        }
    }
    // PENDING - this is strange, report an error ??
    return null;
}
 
Example 11
Project: incubator-netbeans   File: InstanceRefFinder.java   View Source Code Vote up 5 votes
private TypeElement findType(Tree selector) {
    TypeMirror tm = ci.getTrees().getTypeMirror(new TreePath(getCurrentPath(), selector));
    if (tm != null && tm.getKind() == TypeKind.DECLARED) {
        TypeElement t = (TypeElement)ci.getTypes().asElement(tm);
        ElementKind ek = t.getKind();
        if (!(ek.isClass() || ek.isInterface())) {
            // PENDING: an error, log
            return null;
        }
        // the referenced type must be in the same CU, cannot be a superclass.
        return t;
    }
    return null;
}
 
Example 12
Project: openjdk-jdk10   File: Utils.java   View Source Code Vote up 5 votes
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 13
Project: incubator-netbeans   File: WhiteListSupport.java   View Source Code Vote up 5 votes
private void handleNode(
        final Tree node,
        final Map<Tree,WhiteListQuery.Result> p) {
    final Element e = trees.getElement(getCurrentPath());
    if (e == null) {
        return;
    }
    final ElementKind k = e.getKind();
    ElementHandle<?> eh = null;
    Tree toReport =  null;
    if (k.isClass() || k.isInterface()) {
        TypeMirror type = e.asType();
        if (type != null) {
            type = findComponentType(type);
            if (type.getKind() == TypeKind.DECLARED) {
                eh = ElementHandle.create(((DeclaredType)type).asElement());
                toReport=node;
            }
        }
    } else if ((k == ElementKind.METHOD || k == ElementKind.CONSTRUCTOR) &&
            !methodInvocation.isEmpty()) {
        toReport=methodInvocation.peekFirst();
        eh = ElementHandle.create(e);
    }
    final WhiteListQuery.Result res;
    if (toReport != null &&
        !(res=whiteList.check(eh,WhiteListQuery.Operation.USAGE)).isAllowed()) {
            p.put(toReport,res);
    }
}
 
Example 14
Project: incubator-netbeans   File: JavaI18nSupport.java   View Source Code Vote up 5 votes
/**
 * Finds a main top-level class or a nested class element
 * for {@code sourceDataObject} which should be initialized.
 */
private TypeElement getClass(WorkingCopy workingCopy)
                                                    throws IOException {
    workingCopy.toPhase(Phase.ELEMENTS_RESOLVED);

    final String preferredName = sourceDataObject.getName();
    TypeElement firstPublicNestedClass = null;
    
    List<? extends TypeElement> topClasses = workingCopy.getTopLevelElements();
    for (TypeElement topElement : topClasses) {
        ElementKind elementKind = topElement.getKind();
        if (!elementKind.isClass()) {
            continue;
        }

        if (topElement.getSimpleName().contentEquals(preferredName)) {
            return topElement;
        }

        if ((firstPublicNestedClass == null)
                && topElement.getModifiers().contains(Modifier.PUBLIC)) {
            firstPublicNestedClass = topElement;
        }
    }

    return firstPublicNestedClass;
}
 
Example 15
Project: GitHub   File: SourceNames.java   View Source Code Vote up 4 votes
private static boolean isTypeElement(Element element) {
  ElementKind kind = element.getKind();
  return kind.isClass()
      || kind.isInterface();
}
 
Example 16
Project: incubator-netbeans   File: TopClassFinder.java   View Source Code Vote up 4 votes
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 17
Project: incubator-netbeans   File: TopClassFinder.java   View Source Code Vote up 4 votes
static boolean isTestable(TypeElement typeDeclElement) {
    ElementKind elemKind = typeDeclElement.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 18
Project: incubator-netbeans   File: PersistentClassIndex.java   View Source Code Vote up 4 votes
@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 19
Project: incubator-netbeans   File: TopClassFinder.java   View Source Code Vote up 4 votes
public boolean passes(TypeElement topClass,
                      CompilationInfo compInfo) {
    ElementKind elemKind = topClass.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}
 
Example 20
Project: incubator-netbeans   File: TopClassFinder.java   View Source Code Vote up 4 votes
static boolean isTestable(TypeElement typeDeclElement) {
    ElementKind elemKind = typeDeclElement.getKind();
    return (elemKind != ElementKind.ANNOTATION_TYPE)
           && (elemKind.isClass()|| elemKind.isInterface());
}