Java Code Examples for org.netbeans.api.java.classpath.ClassPath#SOURCE

The following examples show how to use org.netbeans.api.java.classpath.ClassPath#SOURCE . 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: GlobalClassPathProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static int type2Index(String type, boolean excludeTests) {
    int index;
    switch (type) {
        case ClassPath.BOOT:
            index = BOOT;
            break;
        case ClassPath.SOURCE:
            index = SOURCE;
            break;
        case ClassPath.COMPILE:
            index = COMPILE;
            break;
        case ClassPath.EXECUTE:
            index = RUNTIME;
            break;
        default:
            index = -1;
    }

    return (index >= 0) && excludeTests ? index + 1 : index;
}
 
Example 2
Source File: GlobalClassPathProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassPath getClassPath(String type, boolean excludeTests) {
    int index = type2Index(type, excludeTests);
    if (index < 0) return null;
    ClassPath cp = cache[index];
    if (cp == null) {
        switch (type) {
            case ClassPath.BOOT:
                cp = createClassPath(new BootClassPathImpl(project));
                break;
            case ClassPath.SOURCE:
                cp = createClassPath(new GradleGlobalClassPathImpl.ProjectSourceClassPathImpl(project, excludeTests));
                break;
            case ClassPath.COMPILE:
                cp = createClassPath(new GradleGlobalClassPathImpl.ProjectCompileClassPathImpl(project, excludeTests));
                break;
            case ClassPath.EXECUTE:
                cp = createClassPath(new GradleGlobalClassPathImpl.ProjectRuntimeClassPathImpl(project, excludeTests));
                break;
        }
        cache[index] = cp;
    }
    return cp;
}
 
Example 3
Source File: ScriptClassPathProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public ClassPath findClassPath(FileObject fo, String type) {
    if ("classpath/html5".equals(type)) {
        return null;
    }
    boolean isGradle = "gradle".equals(fo.getExt());
    if (fo.isFolder() && (FileUtil.toFile(fo) != null)) {
        isGradle = GradleProjects.testForProject(FileUtil.toFile(fo));
    }

    if (isGradle) {
        switch (type) {
            case ClassPath.BOOT:    return BOOT_CP;
            case ClassPath.SOURCE:  return SOURCE_CP;
            case ClassPath.COMPILE: return GRADLE_CP;
            case ClassPath.EXECUTE: return GRADLE_CP;
            }
            }
    return null;
}
 
Example 4
Source File: JShellSourcePathTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized ClassPath findClassPath(
        final FileObject file,
        final String type) {
    if (root != null && file != null && (root.equals(file) || FileUtil.isParentOf(root, file))) {
        switch (type) {
            case ClassPath.BOOT:
                return cps[0];
            case ClassPath.COMPILE:
                return cps[1];
            case ClassPath.SOURCE:
                return cps[2];
        }
    }
    return null;
}
 
Example 5
Source File: JavaOperationsImplTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public ClassPath findClassPath(
        FileObject file,
        String type) {
    for (FileObject srcRoot : srcPath.getRoots()) {
        if (srcRoot.equals(file) || FileUtil.isParentOf(srcRoot, file)) {
            if (type == ClassPath.SOURCE) {
                return srcPath;
            } else if (type == ClassPath.BOOT) {
                return bootPath;
            } else if (type == ClassPath.COMPILE) {
                return compilePath;
            }
        }
    }
    return null;
}
 
Example 6
Source File: HintTestBase.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ClassPath findClassPath(FileObject file, String type) {
    try {
    if (ClassPath.BOOT == type) {
        // XXX simpler to use JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries()
        return ClassPathSupport.createClassPath(getBootClassPath().toArray(new URL[0]));
    }

    if (ClassPath.SOURCE == type) {
        return sourcePath;
    }

    if (ClassPath.COMPILE == type) {
        return compileClassPath;
    }

    if (ClassPath.EXECUTE == type) {
        return ClassPathSupport.createClassPath(new FileObject[] {
            buildRoot
        });
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: JavaTypeProviderTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(final FileObject file, final String type) {
    final FileObject[] roots = sourcePath.getRoots();
    for (FileObject root : roots) {
        if (root.equals(file) || FileUtil.isParentOf(root, file)) {
            if (type == ClassPath.SOURCE) {
                return sourcePath;
            }
            if (type == ClassPath.COMPILE) {
                return compilePath;
            }
            if (type == ClassPath.BOOT) {
                return bootPath;
            }
        }
    }
    if (libSrc2.equals(file) || FileUtil.isParentOf(libSrc2, file)) {
        if (type == ClassPath.SOURCE) {
            return ClassPathSupport.createClassPath(new FileObject[]{libSrc2});
        }
        if (type == ClassPath.COMPILE) {
            return ClassPathSupport.createClassPath(new URL[0]);
        }
        if (type == ClassPath.BOOT) {
            return bootPath;
        }
    }
    return null;
}
 
Example 8
Source File: UiUtilsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(FileObject file, String type) {
    try {
        if (type == ClassPath.SOURCE) {
            return createSourcePath(FileUtil.toFileObject(getWorkDir()));
        } else if (type == ClassPath.BOOT) {
            return createBootClassPath();
        }
    } catch (IOException ioe) {
        //Skeep it
    }
    return ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
}
 
Example 9
Source File: HintTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(FileObject file, String type) {
    try {
    if (ClassPath.BOOT == type) {
        return BootClassPathUtil.getBootClassPath();
    }

    if (JavaClassPathConstants.MODULE_BOOT_PATH == type) {
        return BootClassPathUtil.getModuleBootPath();
    }

    if (ClassPath.SOURCE == type) {
        return sourcePath;
    }

    if (ClassPath.COMPILE == type) {
        return compileClassPath;
    }

    if (ClassPath.EXECUTE == type) {
        return ClassPathSupport.createClassPath(new FileObject[] {
            buildRoot
        });
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 10
Source File: ComputeOverridersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(FileObject file, String type) {
    try {
        FileObject root = findRoot(file);

        if (root == null) {
            return null;
        }
        
        if (ClassPath.BOOT == type) {
            return ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL[0]));
        }

        if (ClassPath.SOURCE == type) {
            return ClassPathSupport.createClassPath(new FileObject[] {
                root
            });
        }

        if (ClassPath.COMPILE == type) {
            return root2ClassPath.get(root);
        }

        if (ClassPath.EXECUTE == type) {
            return ClassPathSupport.createProxyClassPath(
                    root2ClassPath.get(root),
                    ClassPathSupport.createClassPath(new FileObject[] {
                        root2BuildRoot.get(root)
                    })
                   );
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 11
Source File: ShellClasspathProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public ClassPath findClassPath(FileObject file, String type) {
    JShellEnvironment jshe = ShellRegistry.get().getOwnerEnvironment(file);
    if (jshe == null) {
        return null;
    }
    ShellSession ss = jshe.getSession();
    if (ss == null) {
        return null;
    }
    ClasspathInfo cpi = ss.getClasspathInfo();
    if (cpi == null) {
        return null;
    }
    switch (type) {
        case JavaClassPathConstants.MODULE_BOOT_PATH:
            return cpi.getClassPath(PathKind.MODULE_BOOT);
            
        case JavaClassPathConstants.MODULE_COMPILE_PATH:
            return cpi.getClassPath(PathKind.MODULE_COMPILE);
            
        case JavaClassPathConstants.MODULE_CLASS_PATH:
            return cpi.getClassPath(PathKind.MODULE_CLASS);
            
        case JavaClassPathConstants.MODULE_SOURCE_PATH:
            return cpi.getClassPath(PathKind.MODULE_SOURCE);
            
        case ClassPath.COMPILE:
            return cpi.getClassPath(PathKind.COMPILE);
        case ClassPath.SOURCE:
            return cpi.getClassPath(PathKind.SOURCE);
        case ClassPath.BOOT:
            return cpi.getClassPath(PathKind.BOOT);
    }
    return null;
}
 
Example 12
Source File: GeneratorTestMDRCompat.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    ClassPathProvider cpp = new ClassPathProvider() {
        public ClassPath findClassPath(FileObject file, String type) {
            if (type == ClassPath.SOURCE)
                return ClassPathSupport.createClassPath(getSourcePath());
                if (type == ClassPath.COMPILE)
                    return ClassPathSupport.createClassPath(new FileObject[0]);
                if (type == ClassPath.BOOT)
                    return BootClassPathUtil.getBootClassPath();
                return null;
        }
    };
    SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true);
    SourceLevelQueryImplementation slq = new SourceLevelQueryImplementation() {
        @Override public String getSourceLevel(FileObject javaFile) {
            return GeneratorTestMDRCompat.this.getSourceLevel();
        }
    };
    SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/source/resources/layer.xml"}, new Object[] {loader, cpp});
    MockMimeLookup.setInstances(MimePath.get("text/x-java"), new Reindenter.Factory());
    
    TestUtil.setupEditorMockServices();
    JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit");
    File cacheFolder = new File(getWorkDir(), "var/cache/index");
    cacheFolder.mkdirs();
    IndexUtil.setCacheFolder(cacheFolder);
}
 
Example 13
Source File: JavaSourceTaskFactoryTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    JavaSourceTaskFactory.SYNCHRONOUS_EVENTS = true;
    cpp = new ClassPathProvider() {
        public ClassPath findClassPath(FileObject file, String type) {
            if (type == ClassPath.SOURCE)
                return ClassPathSupport.createClassPath(new FileObject[] {FileUtil.toFileObject(getDataDir())});
                if (type == ClassPath.COMPILE)
                    return ClassPathSupport.createClassPath(new FileObject[0]);
                if (type == ClassPath.BOOT)
                    return createBootPath();
                return null;
        }
    };
    SourceUtilsTestUtil.setLookup(new Object[] {
        JavaDataLoader.getLoader(JavaDataLoader.class),
        cpp
    }, this.getClass().getClassLoader());


    jstf = new JavaSourceTaskFactoryImplImpl();
    JavaSourceTaskFactory.ACCESSOR2 = new AccessorImpl();
    testDir = SourceUtilsTestUtil.makeScratchDir(this);
    testFile1 = testDir.createData("test1.java");
    testFile2 = testDir.createData("test2.java");
    task1 = new DummyCancellableTask<CompilationInfo>();
    task2 = new DummyCancellableTask<CompilationInfo>();

    file2Task.put(testFile1, task1);
    file2Task.put(testFile2, task2);

    assertNotNull(JavaSource.forFileObject(testFile1));
    assertNotNull(JavaSource.forFileObject(testFile2));

    assertEquals(2, file2Task.size());

    JavaSourceTaskFactoryManager.register();
}
 
Example 14
Source File: AutoAndroidSourceForBinaryQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public ClassPath findClassPath(FileObject file, String type) {
    FileObject srcRoot = FileUtil.getArchiveFile(file);
    if (srcRoot != null) {
        AndroidClassPathProvider provider = backReferenceMap.get(srcRoot);
        if (provider != null) {
            switch (type) {
                case ClassPath.SOURCE:

                    ClassPath classPath = cache.get(srcRoot);
                    if (classPath == null) {
                        ClassPath srcClassPath = ClassPathSupport.createClassPath(FileUtil.getArchiveRoot(srcRoot));
                        GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, new ClassPath[]{srcClassPath});
                        classPath = ClassPathSupport.createProxyClassPath(srcClassPath);
                        cache.put(srcRoot, classPath);
                    }
                    return classPath;
                case ClassPath.BOOT:
                    return provider.getClassPath(ClassPath.BOOT);
                case ClassPath.COMPILE:
                    return provider.getClassPath(ClassPath.COMPILE);

            }
        }
    }
    return null;
}
 
Example 15
Source File: IndexerTransactionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(final FileObject file, final String type) {
    final FileObject[] roots = sourcePath.getRoots();
    for (FileObject root : roots) {
        if (root.equals(file) || FileUtil.isParentOf(root, file)) {
            if (type == ClassPath.SOURCE) {
                return sourcePath;
            }
            if (type == ClassPath.COMPILE) {
                return compilePath;
            }
            if (type == ClassPath.BOOT) {
                return bootPath;
            }
        }
    }
    if (libSrc2.equals(file) || FileUtil.isParentOf(libSrc2, file)) {
        if (type == ClassPath.SOURCE) {
                return ClassPathSupport.createClassPath(new FileObject[]{libSrc2});
            }
            if (type == ClassPath.COMPILE) {
                return ClassPathSupport.createClassPath(new URL[0]);
            }
            if (type == ClassPath.BOOT) {
                return bootPath;
            }
    }
    return null;
}
 
Example 16
Source File: SourceAnalyzerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public ClassPath findClassPath(FileObject file, String type) {
    final FileObject _root = root;
    if (_root != null && (_root.equals(file) || FileUtil.isParentOf(_root, file))) {
        switch (type) {
            case ClassPath.SOURCE:
                return ClassPathSupport.createClassPath(_root);
            case ClassPath.COMPILE:
                return ClassPath.EMPTY;
            case ClassPath.BOOT:
                return BootClassPathUtil.getBootClassPath();
        }
    }
    return null;
}
 
Example 17
Source File: TestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ClassPath findClassPath(FileObject file, String type) {
    if (type == ClassPath.SOURCE) {
        return getSourceClassPath();
    } else if (type == ClassPath.COMPILE) {
        return getCompileClassPath();
    } else if (type == ClassPath.BOOT) {
        return getBootClassPath();
    }
    return null;
}
 
Example 18
Source File: JavaTypeProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
private static URL[] translate(@NonNull final URL root, @NonNull final String[] cpType) {
    final SourceForBinaryQuery.Result2 res = SourceForBinaryQuery.findSourceRoots2(root);
    FileObject[] roots;
    if (res.preferSources() && (roots = res.getRoots()).length > 0) {
        cpType[0] = ClassPath.SOURCE;
        return asURLs(roots);
    }
    return new URL[] {root};
}
 
Example 19
Source File: GeneratorTestBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void setUp() throws Exception {
    super.setUp();
    SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
    // ensure JavaKit is present, so that NbEditorDocument is eventually created.
    // it handles PositionRefs differently than PlainDocument/PlainEditorKit.
    MockMimeLookup.setInstances(MimePath.get("text/x-java"), 
            new Reindenter.Factory(), new JavaKit());
    dataDir = SourceUtilsTestUtil.makeScratchDir(this);
    FileObject dataTargetPackage = FileUtil.createFolder(dataDir, getSourcePckg());
    assertNotNull(dataTargetPackage);
    FileObject dataSourceFolder = FileUtil.toFileObject(getDataDir()).getFileObject(getSourcePckg());
    assertNotNull(dataSourceFolder);
    deepCopy(dataSourceFolder, dataTargetPackage);
    ClassPathProvider cpp = new ClassPathProvider() {
        public ClassPath findClassPath(FileObject file, String type) {
            if (type == ClassPath.SOURCE)
                return ClassPathSupport.createClassPath(new FileObject[] {dataDir});
                if (type == ClassPath.COMPILE)
                    return ClassPathSupport.createClassPath(new FileObject[0]);
                if (type == ClassPath.BOOT)
                    return BootClassPathUtil.getBootClassPath();
                return null;
        }
    };
    SharedClassObject loader = JavaDataLoader.findObject(JavaDataLoader.class, true);
    
    SourceUtilsTestUtil.prepareTest(
            new String[] {
                "org/netbeans/modules/java/project/ui/layer.xml", 
                "org/netbeans/modules/project/ui/resources/layer.xml",
                "META-INF/generated-layer.xml"
            },
            new Object[] {loader, cpp}
    );
    
    JEditorPane.registerEditorKitForContentType("text/x-java", "org.netbeans.modules.editor.java.JavaKit");
    File cacheFolder = new File(getWorkDir(), "var/cache/index");
    cacheFolder.mkdirs();
    IndexUtil.setCacheFolder(cacheFolder);
    ensureRootValid(dataDir.getURL());
    TestUtil.setupEditorMockServices();
    Main.initializeURLFactory();
}
 
Example 20
Source File: ResourceBundles.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Map<String, ResourceBundleInfo> createResourceBundleMapAndFileChangeListeners() {
    Map<String, ResourceBundleInfo> result = new HashMap<>();
    ClassPathProvider provider = project.getLookup().lookup(ClassPathProvider.class);
    if (provider == null) {
        return null;
    }

    Sources sources = ProjectUtils.getSources(project);
    if (sources == null) {
        return null;
    }

    SourceGroup[] sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
    for (ResourceBundle bundle : getBundles(new ResolverContext())) {
        String bundleFile = bundle.getBaseName();
        for (SourceGroup sourceGroup : sourceGroups) {
            FileObject rootFolder = sourceGroup.getRootFolder();

            for (String classPathType : new String[]{ClassPath.SOURCE, ClassPath.COMPILE}) {
                ClassPath classPath = ClassPath.getClassPath(rootFolder, classPathType);
                if (classPath == null) {
                    continue;
                }
                ClassLoader classLoader = classPath.getClassLoader(false);
                try {
                    // TODO - rewrite listening on all (localized) files
                    String resourceFileName = new StringBuilder()
                            .append(bundleFile.replace(".", "/"))
                            .append(".properties")
                            .toString(); //NOI18N
                    
                    URL url = classLoader.getResource(resourceFileName);
                    if(url != null) {
                        LOGGER.finer(String.format("Found %s URL for resource bundle %s", url, resourceFileName ));
                        FileObject fileObject = URLMapper.findFileObject(url);
                        if(fileObject != null) {
                            if (fileObject.canWrite()) {
                                fileObject.addFileChangeListener(
                                        WeakListeners.create(FileChangeListener.class, FILE_CHANGE_LISTENER, fileObject));
                                LOGGER.finer(String.format("Added FileChangeListener to file %s", fileObject ));
                            }
                        } else {
                            LOGGER.fine(String.format("Cannot map %s URL to FileObject!", url));
                        }
                    }
                    
                    java.util.ResourceBundle found = java.util.ResourceBundle.getBundle(bundleFile, Locale.getDefault(), classLoader);
                    result.put(bundleFile, new ResourceBundleInfo(bundle.getFiles(), found, bundle.getVar()));
                    break; // found the bundle in source cp, skip searching compile cp
                } catch (MissingResourceException exception) {
                    continue;
                }
            }

        }
    }
    return result;
}