Java Code Examples for org.netbeans.api.java.source.ClassIndex#getDeclaredTypes()

The following examples show how to use org.netbeans.api.java.source.ClassIndex#getDeclaredTypes() . 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: SourceGroupSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static FileObject getFileObjectFromClassName(String qualifiedClassName, Project project) throws IOException {
    FileObject root = findSourceRoot(project);
    ClasspathInfo cpInfo = ClasspathInfo.create(root);
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = qualifiedClassName.lastIndexOf('.')+1;
    String simple = qualifiedClassName.substring(beginIndex);
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME, 
            Collections.singleton(ClassIndex.SearchScope.SOURCE));
    for (ElementHandle<TypeElement> handle : handles) {
        if (qualifiedClassName.equals(handle.getQualifiedName())) {
            return SourceUtils.getFile(handle, cpInfo);
        }
    }
    return null;
}
 
Example 2
Source File: SourceGroupSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static ElementHandle<TypeElement> getHandleClassName(String qualifiedClassName, 
        Project project) throws IOException 
{
    FileObject root = MiscUtilities.findSourceRoot(project);
    ClassPathProvider provider = project.getLookup().lookup( 
            ClassPathProvider.class);
    ClassPath sourceCp = provider.findClassPath(root, ClassPath.SOURCE);
    ClassPath compileCp = provider.findClassPath(root, ClassPath.COMPILE);
    ClassPath bootCp = provider.findClassPath(root, ClassPath.BOOT);
    ClasspathInfo cpInfo = ClasspathInfo.create(bootCp, compileCp, sourceCp);
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = qualifiedClassName.lastIndexOf('.')+1;
    String simple = qualifiedClassName.substring(beginIndex);
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME, 
            EnumSet.of(ClassIndex.SearchScope.SOURCE, 
            ClassIndex.SearchScope.DEPENDENCIES));
    for (final ElementHandle<TypeElement> handle : handles) {
        if (qualifiedClassName.equals(handle.getQualifiedName())) {
            return handle;
        }
    }
    return null;
}
 
Example 3
Source File: MiscPrivateUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static FileObject getFileObjectFromClassName(Project p, String qualifiedClassName)
        throws IOException
{
    FileObject root = MiscUtilities.findSourceRoot(p);
    ClasspathInfo cpInfo = ClasspathInfo.create(root);
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = qualifiedClassName.lastIndexOf('.')+1;
    String simple = qualifiedClassName.substring(beginIndex);
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME,
            Collections.singleton(ClassIndex.SearchScope.SOURCE));
    if ( handles == null ){
        return null;
    }
    for (ElementHandle<TypeElement> handle : handles) {
        if (qualifiedClassName.equals(handle.getQualifiedName())) {
            return SourceUtils.getFile(handle, cpInfo);
        }
    }
    return null;
}
 
Example 4
Source File: WsitPojectOpenedHook.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public FileObject getFileObjectFromClassName(String fqn) {
    SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(
            JavaProjectConstants.SOURCES_TYPE_JAVA);
    if (sourceGroups == null || sourceGroups.length == 0) {
        return null;
    }
    ClasspathInfo cpInfo = ClasspathInfo.create(sourceGroups[0].getRootFolder());
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = fqn.lastIndexOf('.')+1;
    String simple = fqn.substring(beginIndex);
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME, 
            Collections.singleton(ClassIndex.SearchScope.SOURCE));
    for (ElementHandle<TypeElement> handle : handles) {
        if (fqn.equals(handle.getQualifiedName())) {
            return SourceUtils.getFile(handle, cpInfo);
        }
    }
    return null;
}
 
Example 5
Source File: ProjectHelper.java    From jeddict with Apache License 2.0 6 votes vote down vote up
public static ElementHandle<TypeElement> getHandleClassName(String qualifiedClassName,
        Project project) throws IOException {
    FileObject root = findSourceRoot(project);
    ClassPathProvider provider = project.getLookup().lookup(
            ClassPathProvider.class);
    ClassPath sourceCp = provider.findClassPath(root, ClassPath.SOURCE);
    ClassPath compileCp = provider.findClassPath(root, ClassPath.COMPILE);
    ClassPath bootCp = provider.findClassPath(root, ClassPath.BOOT);
    ClasspathInfo cpInfo = ClasspathInfo.create(bootCp, compileCp, sourceCp);
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = qualifiedClassName.lastIndexOf('.') + 1;
    String simple = qualifiedClassName.substring(beginIndex);
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME,
            EnumSet.of(ClassIndex.SearchScope.SOURCE,
                    ClassIndex.SearchScope.DEPENDENCIES));
    for (final ElementHandle<TypeElement> handle : handles) {
        if (qualifiedClassName.equals(handle.getQualifiedName())) {
            return handle;
        }
    }
    return null;
}
 
Example 6
Source File: ProjectHelper.java    From jeddict with Apache License 2.0 6 votes vote down vote up
public static ElementHandle<TypeElement> getHandleClassName(String qualifiedClassName,
        SourceGroup sourceGroup) throws IOException {
    ClassPath rcp = ClassPathSupport.createClassPath(sourceGroup.getRootFolder());
    ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.EMPTY, ClassPath.EMPTY, rcp);
    ClassIndex ci = cpInfo.getClassIndex();
    int beginIndex = qualifiedClassName.lastIndexOf('.') + 1;
    String simple = qualifiedClassName.substring(beginIndex);

    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes(
            simple, ClassIndex.NameKind.SIMPLE_NAME,
            EnumSet.of(ClassIndex.SearchScope.SOURCE));
    for (final ElementHandle<TypeElement> handle : handles) {
        if (qualifiedClassName.equals(handle.getQualifiedName())) {
            return handle;
        }
    }

    return null;
}
 
Example 7
Source File: IndexerTransactionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks that during the initial scan, types from the source root are hidden,
 * until the source root is fully scanned.
 * 
 * Also check that files are not visible until the source root finishes.
 * 
 * Enforce flush of memory cache after each file, and each added document.
 * 
 * @throws Exception 
 */
public void testInitialWorkRootHidden() throws Exception {
    registerGlobalPath(ClassPath.BOOT, new ClassPath[] {bootPath});
    registerGlobalPath(ClassPath.COMPILE, new ClassPath[] {compilePath});
    registerGlobalPath(ClassPath.SOURCE, new ClassPath[] {sourcePath});
    
    RepositoryUpdater.getDefault().start(true);
    
    // make the initial scan, with 'src'
    logHandler.waitForInitialScan();

    // ensure the cache will flush after each file:
    WriteBackTransaction.disableCache = true;
    // force Lucene index to fluhs after each document
    System.setProperty("test.org.netbeans.modules.parsing.lucene.cacheDisable", Boolean.TRUE.toString());
    
    final ClassPath scp = ClassPathSupport.createClassPath(srcRoot, srcRoot2);
    
    final ClasspathInfo cpInfo = ClasspathInfo.create(
        ClassPathSupport.createClassPath(new URL[0]),
        ClassPathSupport.createClassPath(new URL[0]),
        scp);
    final ClassIndex ci = cpInfo.getClassIndex();
    
    
    Set<ElementHandle<TypeElement>> handles = ci.getDeclaredTypes("TestFile", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));

    assertEquals(1, handles.size());
    
    handles = ci.getDeclaredTypes("SuperClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertTrue(handles.isEmpty());
    
    // now add 'src2' to the set of roots, block the completion (in log handler),
    // check that classes are not available
    final Object blocker = new String("blocker");
    
    logHandler.beforeFinishCallback = new ScanCallback() {

        @Override
        public void scanned(String indexer, String root) {
            if (!indexer.equals("java")) {
                return;
            }
            if (!root.endsWith("src2/")) {
                return;
            }
            
            // notify the test to proceeed, wait for 
            testBlocker.release();
            try {
                // wait for the test to inspect
                parserBlocker.acquire();
            } catch (InterruptedException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    };

    List ll = new ArrayList<PathResourceImplementation>();
    ll.add(ClassPathSupport.createResource(srcRoot.getURL()));
    ll.add(ClassPathSupport.createResource(srcRoot2.getURL()));
    spiSrc.setImpls(ll);
    
    // wait till src2
    testBlocker.acquire();

    // check that SuperClass is STILL not present
    handles = ci.getDeclaredTypes("TestFile", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));

    assertEquals(1, handles.size());
    
    handles = ci.getDeclaredTypes("SuperClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertTrue(handles.isEmpty());
    

    Semaphore ss = new Semaphore(0);
    ci.addClassIndexListener(new RootWatcher(ss));
    
    parserBlocker.release();
    ss.acquire();
    
    handles = ci.getDeclaredTypes("SuperClass", ClassIndex.NameKind.SIMPLE_NAME, Collections.singleton(
            SearchScope.SOURCE));
    assertEquals(1, handles.size());    
}
 
Example 8
Source File: SupportedAnnotationTypesCompletion.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
        ProcessingEnvironment processingEnv = this.processingEnv.get();

        if (processingEnv == null)
            return Collections.emptyList();

        TypeElement annotationObj = processingEnv.getElementUtils().getTypeElement("java.lang.annotation.Annotation");

        if (annotationObj == null)
            return Collections.emptyList();

        Trees trees = Trees.instance(processingEnv);
        TreePath path = trees.getPath(element);

        if (path == null)
            return Collections.emptyList();

        FileObject owner;

        try {
            owner = URLMapper.findFileObject(path.getCompilationUnit().getSourceFile().toUri().toURL());
        } catch (MalformedURLException ex) {
            Exceptions.printStackTrace(ex);
            return Collections.emptyList();
        }

        ClassIndex ci = ClasspathInfo.create(owner).getClassIndex();

        if (ci == null)
            return Collections.emptyList();

        List<Completion> result = new LinkedList<Completion>();

//        for (ElementHandle<TypeElement> eh : ci.getElements(ElementHandle.create(annotationObj), EnumSet.of(SearchKind.IMPLEMENTORS), EnumSet.of(SearchScope.DEPENDENCIES, SearchScope.SOURCE))) {
//            result.add(new CompletionImpl(eh.getQualifiedName()));
//        }

        for (ElementHandle<TypeElement> eh : ci.getDeclaredTypes("", ClassIndex.NameKind.PREFIX, EnumSet.of(SearchScope.DEPENDENCIES, SearchScope.SOURCE))) {
            if (eh.getKind() != ElementKind.ANNOTATION_TYPE) continue;
            
            result.add(new CompletionImpl('\"' + eh.getQualifiedName() + '\"'));
        }

        return result;
    }