Java Code Examples for javax.lang.model.element.ElementKind#OTHER

The following examples show how to use javax.lang.model.element.ElementKind#OTHER . 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: ReferencesCount.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an estimate of a number of classes on given source path (source root) which are
 * using given type.
 * @param type the type type to find the usage frequency for.
 * @return number of classes using the type.
 */
public int getTypeReferenceCount(@NonNull final ElementHandle<? extends TypeElement> type) {
    Parameters.notNull("binaryName", type);   //NOI18N
    if (!type.getKind().isClass() &&
        !type.getKind().isInterface() &&
         type.getKind() != ElementKind.OTHER) {
        throw new IllegalArgumentException(type.toString());
    }
    try {
        init();
        final Integer count = typeFreqs.get(SourceUtils.getJVMSignature(type)[0]);
        return count == null ? 0 : count;
    } catch (InterruptedException ie) {
        return 0;
    }
}
 
Example 2
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@CheckForNull
public static <T extends Element> ElementHandle<T> createElementHandle(@NonNull final T element) {
    //ElementKind OTHER represents errors <any>, <none>.
    //These are special errors which cannot be resolved
    if (element.getKind() == ElementKind.OTHER) {
        return null;
    }
    try {
        return ElementHandle.create(element);
    } catch (IllegalArgumentException e) {
        LOG.log(
                Level.INFO,
                "Unresolvable element: {0}, reason: {1}",    //NOI18N
                new Object[]{
                    element,
                    e.getMessage()
                });
        return null;
    }
}
 
Example 3
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static boolean signatureEquals(
    @NonNull final ElementHandle<Element> handle,
    @NonNull final Element element) {
    if (handle == null) {
        return false;
    }
    //ElementKind OTHER represents errors <any>, <none>.
    //These are special errors which cannot be resolved
    if (element.getKind() == ElementKind.OTHER) {
        return false;
    }
    try {
        return handle.signatureEquals(element);
    } catch (IllegalArgumentException e) {
        LOG.log(
            Level.INFO,
            "Unresolvable element: {0}, reason: {1}",    //NOI18N
            new Object[]{
                element,
                e.getMessage()
            });
        return false;
    }
}
 
Example 4
Source File: ElementNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
static Description directive(
        @NonNull final ClassMemberPanelUI ui,
        @NonNull final String name,
        @NonNull final TreePathHandle treePathHandle,
        @NonNull final ModuleElement.DirectiveKind kind,
        @NonNull final ClasspathInfo cpInfo,
        final long pos,
        @NonNull final Openable openable) {
    return new Description(
            ui,
            name,
            Union2.<ElementHandle<?>,TreePathHandle>createSecond(treePathHandle),
            ElementKind.OTHER,
            directivePosInKind(kind),
            cpInfo,
            EnumSet.of(Modifier.PUBLIC),
            pos,
            false,
            false,
            ()->ElementIcons.getModuleDirectiveIcon(kind),
            openable);
}
 
Example 5
Source File: ElementNode.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
static Description directive(
        @NonNull final ClassMemberPanelUI ui,
        @NonNull final String name,
        @NonNull final ModuleElement.DirectiveKind kind,
        @NonNull final ClasspathInfo cpInfo,
        @NonNull final Openable openable) {
    return new Description(
            ui,
            name,
            null,
            ElementKind.OTHER,
            directivePosInKind(kind),
            cpInfo,
            EnumSet.of(Modifier.PUBLIC),
            -1,
            false,
            false,
            ()->ElementIcons.getModuleDirectiveIcon(kind),
            openable);
}
 
Example 6
Source File: ClassMemberPanelUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@CheckForNull
@Override
public Node findNode(@NonNull final Point loc) {
    final TreePath path = tree.getPathForLocation( loc.x, loc.y );
    if( null == path ) {
        return null;
    }
    final Node node = Visualizer.findNode( path.getLastPathComponent());
    if (!(node instanceof ElementNode)) {
        return null;
    }
    final ElementNode enode = (ElementNode) node;
    final ElementNode.Description desc = enode.getDescritption();
    //Other and module do not have javadoc
    return desc.kind != ElementKind.OTHER
        && desc.kind != ElementKind.MODULE ?
            node :
            null;
}
 
Example 7
Source File: ElementHandle.java    From netbeans with 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 8
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a binary name of the {@link TypeElement} represented by this
 * {@link ElementHandle}. When the {@link ElementHandle} doesn't represent
 * a {@link TypeElement} it throws a {@link IllegalStateException}
 * @return the qualified name
 * @throws an {@link IllegalStateException} when this {@link ElementHandle} 
 * isn't created for the {@link TypeElement}.
 */
public @NonNull String getBinaryName () throws IllegalStateException {
    if ((this.kind.isClass() && !isArray(signatures[0])) ||
            this.kind.isInterface() ||
            this.kind == ElementKind.MODULE ||
            this.kind == ElementKind.OTHER) {
        return this.signatures[0];
    }
    else {
        throw new IllegalStateException ();
    }
}
 
Example 9
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a qualified name of the {@link TypeElement} represented by this
 * {@link ElementHandle}. When the {@link ElementHandle} doesn't represent
 * a {@link TypeElement} it throws a {@link IllegalStateException}
 * @return the qualified name
 * @throws an {@link IllegalStateException} when this {@link ElementHandle} 
 * isn't creatred for the {@link TypeElement}.
 */
public @NonNull String getQualifiedName () throws IllegalStateException {
    if ((this.kind.isClass() && !isArray(signatures[0])) ||
            this.kind.isInterface() ||
            this.kind == ElementKind.MODULE ||
            this.kind == ElementKind.OTHER) {
        return this.signatures[0].replace (Target.DEFAULT.syntheticNameChar(),'.');    //NOI18N
    }
    else {
        throw new IllegalStateException ();
    }
}
 
Example 10
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: CodeStyle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the group number of the class member. Elements with the same
 * number form a group. Groups with lower numbers should be positioned
 * higher in the class member list.
 * @param tree the member tree
 * @return the group number
 * @since 0.96
 */
public int getGroupId(Tree tree) {
    ElementKind kind = ElementKind.OTHER;
    Set<Modifier> modifiers = null;
    switch (tree.getKind()) {
        case ANNOTATION_TYPE:
        case CLASS:
        case ENUM:
        case INTERFACE:
            kind = ElementKind.CLASS;
            modifiers = ((ClassTree)tree).getModifiers().getFlags();
            break;
        case METHOD:
            MethodTree mt = (MethodTree)tree;
            if (mt.getName().contentEquals("<init>")) { //NOI18N
                kind = ElementKind.CONSTRUCTOR;
            } else {
                kind = ElementKind.METHOD;
            }
            modifiers = mt.getModifiers().getFlags();
            break;
        case VARIABLE:
            kind = ElementKind.FIELD;
            modifiers = ((VariableTree)tree).getModifiers().getFlags();
            break;
        case BLOCK:
            kind = ((BlockTree)tree).isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
            break;
    }
    for (Info info : infos) {
        if (info.check(kind, modifiers))
            return info.groupId;
    }
    return infos.length;
}
 
Example 12
Source File: ElementNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Action[] getActions( boolean context ) {
    if ( context || description.name == null ) {
        return description.ui.getActions();
    } else {
        final Action panelActions[] = description.ui.getActions();
        final List<? extends Action> standardActions;
        final List<? extends Action> additionalActions;
        if (description.kind == ElementKind.OTHER) {
            standardActions = Collections.singletonList(getOpenAction());
            additionalActions = Collections.<Action>emptyList();
        } else {
            standardActions = Arrays.asList(new Action[] {
                getOpenAction(),
                RefactoringActionsFactory.whereUsedAction(),
                RefactoringActionsFactory.popupSubmenuAction()
            });
            additionalActions = Utilities.actionsForPath(ACTION_FOLDER);
        }
        final int standardActionsSize = standardActions.isEmpty() ? 0 : standardActions.size() + 1;
        final int additionalActionSize = additionalActions.isEmpty() ? 0 : additionalActions.size() + 1;
        final List<Action> actions = new ArrayList<>(standardActionsSize + additionalActionSize + panelActions.length);
        if (standardActionsSize > 0) {
            actions.addAll(standardActions);
            actions.add(null);
        }
        if (additionalActionSize > 0) {
            actions.addAll(additionalActions);
            actions.add(null);
        }
        actions.addAll(Arrays.asList(panelActions));
        return actions.toArray(new Action[actions.size()]);
    }
}
 
Example 13
Source File: CaretListeningTask.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void updateNavigatorSelection(CompilationInfo ci, TreePath tp) throws Exception {
    final ClassMemberPanel cmp = ClassMemberPanel.getInstance();
    if (cmp == null) {
        return;
    }
    final ClassMemberPanelUI cmpUi = cmp.getClassMemberPanelUI();
    if (!cmpUi.isAutomaticRefresh()) {
        cmpUi.getTask().runImpl(ci, false);
        lastEhForNavigator = null;
    }
    // Try to find the declaration we are in
    final Pair<Element,TreePath> p = outerElement(ci, tp);
    if (p != null) {
        final Element e = p.first();
        Runnable action = null;
        if (e == null) {
            //Directive
            lastEhForNavigator = null;
            action = () -> {
                cmp.selectTreePath(TreePathHandle.create(p.second(), ci));
            };
        } else if (e.getKind() != ElementKind.OTHER) {
            final ElementHandle<Element> eh = ElementHandle.create(e);
            if (lastEhForNavigator != null && eh.signatureEquals(lastEhForNavigator)) {
                return;
            }
            lastEhForNavigator = eh;
            action = () -> {
                cmp.selectElement(eh);
            };
        }
        if (action != null) {
            SwingUtilities.invokeLater(action);
        }
    }
}
 
Example 14
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private DocCommentDuo getDocCommentTuple(Element element) {
    // prevent nasty things downstream with overview element
    if (element.getKind() != ElementKind.OTHER) {
        TreePath path = getTreePath(element);
        if (path != null) {
            DocCommentTree docCommentTree = docTrees.getDocCommentTree(path);
            return new DocCommentDuo(path, docCommentTree);
        }
    }
    return null;
}
 
Example 15
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean isOverviewElement(Element e) {
    return e.getKind() == ElementKind.OTHER;
}
 
Example 16
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Retrieves the doc comments for a given element.
 * @param element
 * @return DocCommentTree for the Element
 */
public DocCommentTree getDocCommentTree0(Element element) {

    DocCommentDuo duo = null;

    ElementKind kind = element.getKind();
    if (kind == ElementKind.PACKAGE || kind == ElementKind.OTHER) {
        duo = dcTreeCache.get(element); // local cache
        if (!isValidDuo(duo) && kind == ElementKind.PACKAGE) {
            // package-info.java
            duo = getDocCommentTuple(element);
        }
        if (!isValidDuo(duo)) {
            // package.html or overview.html
            duo = configuration.cmtUtils.getHtmlCommentDuo(element); // html source
        }
    } else {
        duo = configuration.cmtUtils.getSyntheticCommentDuo(element);
        if (!isValidDuo(duo)) {
            duo = dcTreeCache.get(element); // local cache
        }
        if (!isValidDuo(duo)) {
            duo = getDocCommentTuple(element); // get the real mccoy
        }
    }

    DocCommentTree docCommentTree = isValidDuo(duo) ? duo.dcTree : null;
    TreePath path = isValidDuo(duo) ? duo.treePath : null;
    if (!dcTreeCache.containsKey(element)) {
        if (docCommentTree != null && path != null) {
            if (!configuration.isAllowScriptInComments()) {
                try {
                    javaScriptScanner.scan(docCommentTree, path, p -> {
                        throw new JavaScriptScanner.Fault();
                    });
                } catch (JavaScriptScanner.Fault jsf) {
                    String text = configuration.getText("doclet.JavaScript_in_comment");
                    throw new UncheckedDocletException(new SimpleDocletException(text, jsf));
                }
            }
            configuration.workArounds.runDocLint(path);
        }
        dcTreeCache.put(element, duo);
    }
    return docCommentTree;
}
 
Example 17
Source File: Symbol.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    return ElementKind.OTHER;       // most unkind
}
 
Example 18
Source File: OverviewElement.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public ElementKind getKind() {
    return ElementKind.OTHER;
}
 
Example 19
Source File: Symbol.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    return ElementKind.OTHER;       // most unkind
}
 
Example 20
Source File: PersistentClassIndex.java    From netbeans with 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);
    }
}