Java 类javax.lang.model.util.ElementScanner6 实例源码

项目:incubator-netbeans    文件:InstantRenamePerformer.java   
private static boolean isOverriddenInsideOutermostEnclosingClass(final ExecutableElement ee, final ElementUtilities eu) {
    final boolean[] ret = new boolean[] {false};
    new ElementScanner6<Void, Void>() {
        @Override
        public Void visitType(TypeElement te, Void p) {
            if (ret[0])
                return null;
            if (te != ee.getEnclosingElement() && eu.getImplementationOf(ee, te) != null && !isAnyEncloserPrivate(te))
                ret[0] = true;
            return super.visitType(te, p);
        }            
    }.scan(eu.outermostTypeElement(ee));
    return ret[0];
}
项目:incubator-netbeans    文件:SourceUtils.java   
/**
 * Returns classes declared in the given source file which have the main method.
 * @param fo source file
 * @return the classes containing main method
 * @throws IllegalArgumentException when file does not exist or is not a java source file.
 */
public static Collection<ElementHandle<TypeElement>> getMainClasses (final @NonNull FileObject fo) {
    Parameters.notNull("fo", fo);   //NOI18N
    if (!fo.isValid()) {
        throw new IllegalArgumentException ("FileObject : " + FileUtil.getFileDisplayName(fo) + " is not valid.");  //NOI18N
    }
    if (fo.isVirtual()) {
        throw new IllegalArgumentException ("FileObject : " + FileUtil.getFileDisplayName(fo) + " is virtual.");  //NOI18N
    }
    final JavaSource js = JavaSource.forFileObject(fo);        
    if (js == null) {
        throw new IllegalArgumentException ();
    }
    try {
        final LinkedHashSet<ElementHandle<TypeElement>> result = new LinkedHashSet<> ();
        js.runUserActionTask(new Task<CompilationController>() {            
            @Override
            public void run(final CompilationController control) throws Exception {
                if (control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED).compareTo (JavaSource.Phase.ELEMENTS_RESOLVED)>=0) {
                    final List<TypeElement>  types = new ArrayList<>();
                    final ElementScanner6<Void,Void> visitor = new ElementScanner6<Void, Void>() {
                        @Override
                        public Void visitType(TypeElement e, Void p) {
                            if (e.getEnclosingElement().getKind() == ElementKind.PACKAGE
                               || e.getModifiers().contains(Modifier.STATIC)) {
                                types.add(e);
                                return super.visitType(e, p);
                            } else {
                                return null;
                            }
                        }
                    };
                    visitor.scan(control.getTopLevelElements(), null);
                    for (TypeElement type : types) {
                        for (ExecutableElement exec :  ElementFilter.methodsIn(control.getElements().getAllMembers(type))) {
                            if (SourceUtils.isMainMethod(exec)) {
                                result.add (ElementHandle.create(type));
                            }
                        }
                    }
                }
            }                

        }, true);
        return result;
    } catch (IOException ioe) {
        Exceptions.printStackTrace(ioe);
        return Collections.<ElementHandle<TypeElement>>emptySet();
    }       
}