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

The following are Jave code examples for showing how to use isInterface() 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: OpenJSharp   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 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: 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 9
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 10
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 11
Project: incubator-netbeans   File: SourceUtilsEx.java   View Source Code Vote up 5 votes
/**
 * Returns a source file in which the passed element
 * is declared in. This tuned up version of {@code SourceUtils.getFile}
 * is necessary as sequential invocations of {@code SourceUtils.getFile} are
 * excessively slow.
 *
 * @param element an element to find {@link FileObject} for
 * @param cpInfo scope where the file will be searched
 * @param cache a cache
 * @return the source file
 * @see SourceUtils#getFile(org.netbeans.api.java.source.ElementHandle, org.netbeans.api.java.source.ClasspathInfo) SourceUtils.getFile
 */
public static FileObject getFile(final Element element, final ClasspathInfo cpInfo, final Cache cache) {
    Parameters.notNull("element", element); //NOI18N
    Parameters.notNull("cpInfo", cpInfo);   //NOI18N
    Parameters.notNull("cache", cache);   //NOI18N

    Element current = element;
    Element prev = current.getKind() == ElementKind.PACKAGE ? current : null;
    while (current.getKind() != ElementKind.PACKAGE) {
        prev = current;
        current = current.getEnclosingElement();
    }
    if (prev == null) {
        return null;
    }

    final ElementKind kind = prev.getKind();
    String fqn;
    if (kind.isClass() || kind.isInterface()) {
        fqn = ((TypeElement) prev).getQualifiedName().toString();
    } else if (kind == ElementKind.PACKAGE) {
        fqn = ((PackageElement) prev).getQualifiedName().toString();
    } else {
        return null;
    }

    Object cached = cache.cacheOfSrcFiles.get(fqn);
    if (cached == null) {
        final ElementHandle<? extends Element> handle = ElementHandle.create(prev);
        cached = SourceUtils.getFile(handle, cpInfo);
        cache.cacheOfSrcFiles.put(fqn, cached != null ? cached : Cache.NULL);
    } else if (cached == Cache.NULL) {
        cached = null;
    }
    return (FileObject) cached;
}
 
Example 12
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 13
Project: incubator-netbeans   File: AutoImport.java   View Source Code Vote up 5 votes
@Override
public Void visitDeclared(DeclaredType type, Void p) {
    TypeElement element = (TypeElement)type.asElement();
    String name = element.getQualifiedName().toString();
    ElementKind kind = element.getEnclosingElement().getKind();
    if (kind.isClass() || kind.isInterface() || kind == ElementKind.PACKAGE) {
        try {
            String s = SourceUtils.resolveImport(info, path, name);
            int idx = s.indexOf('.');
            if (idx < 0) {
                importedTypes.add(name);
            } else {
                importedTypes.add(name.substring(0, name.length() - s.length() + idx));
            }
            name = s;
        } catch (Exception e) {
            Logger.getLogger("global").log(Level.INFO, null, e); //NOI18N
        }
    }
    builder.append(name);
    Iterator<? extends TypeMirror> it = type.getTypeArguments().iterator();
    if (it.hasNext()) {
        builder.append('<'); //NOI18N
        while(it.hasNext()) {
            visit(it.next());
            if (it.hasNext())
                builder.append(", "); //NOI18N
        }
        builder.append('>'); //NOI18N
    }
    return null;
}
 
Example 14
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 15
Project: openjdk-jdk10   File: TraverseProc.java   View Source Code Vote up 5 votes
void addPublicTypes(List<TypeElement> list, Element e) {
    ElementKind kind = e.getKind();
    if ((kind.isClass() || kind.isInterface())
            && e.getModifiers().contains(Modifier.PUBLIC)) {
        list.add((TypeElement)e);
        for (Element enc : e.getEnclosedElements()) {
            addPublicTypes(list, enc);
        }
    }
}
 
Example 16
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 17
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 18
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 19
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 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());
}