本文整理汇总了Java中org.netbeans.api.java.classpath.ClassPath.getRoots方法的典型用法代码示例。如果您正苦于以下问题:Java ClassPath.getRoots方法的具体用法?Java ClassPath.getRoots怎么用?Java ClassPath.getRoots使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.api.java.classpath.ClassPath
的用法示例。
在下文中一共展示了ClassPath.getRoots方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSourcePathImplementation
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
public void testSourcePathImplementation () throws Exception {
ClassPathProviderImpl cpProvider = pp.getClassPathProvider();
ClassPath[] cps = cpProvider.getProjectClassPaths(ClassPath.SOURCE);
ClassPath cp = cps[0];
FileObject[] roots = cp.getRoots();
assertNotNull ("Roots can not be null",roots);
assertEquals("There must be one source root", 1, roots.length);
assertEquals("There must be src root",roots[0],sources);
TestListener tl = new TestListener();
cp.addPropertyChangeListener (tl);
FileObject newRoot = SourceRootsTest.addSourceRoot(helper, projdir,"src.other.dir","other");
assertTrue("Classpath must fire PROP_ENTRIES and PROP_ROOTS", tl.getEvents().containsAll(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)));
roots = cp.getRoots();
assertNotNull ("Roots can not be null",roots);
assertEquals("There must be two source roots", 2, roots.length);
assertEquals("There must be src root",roots[0],sources);
assertEquals("There must be other root",roots[1],newRoot);
cp.removePropertyChangeListener(tl);
}
示例2: searchBinaryPath
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private static FileObject searchBinaryPath(String classPathID, String respath, URL url) {
Set<ClassPath> cpaths = GlobalPathRegistry.getDefault().getPaths(classPathID);
for (ClassPath cpath: cpaths) {
FileObject[] cpRoots = cpath.getRoots();
for (int i = 0; i < cpRoots.length; i++) {
SourceForBinaryQuery.Result result = SourceForBinaryQuery.findSourceRoots(URLMapper.findURL(cpRoots[i], URLMapper.EXTERNAL));
FileObject[] srcRoots = result.getRoots();
for (int j = 0; j < srcRoots.length; j++) {
FileObject fo = srcRoots[j].getFileObject(respath);
if (fo != null && isJavadocAssigned(cpath, url)) {
return fo;
}
}
}
}
return null;
}
示例3: addRoots
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private String addRoots(String prev, ClassPath cp) {
FileObject[] roots = cp.getRoots();
StringBuilder sb = new StringBuilder(prev);
for (FileObject r : roots) {
FileObject ar = FileUtil.getArchiveFile(r);
if (ar == null) {
ar = r;
}
File f = FileUtil.toFile(ar);
if (f != null) {
if (sb.length() > 0) {
sb.append(File.pathSeparatorChar);
}
sb.append(f.getPath());
}
}
return sb.toString();
}
示例4: findProjectImportedModules
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
/**
* Returns set of modules imported by the project. Adds to the passed collection
* if not null. Module names from `required' clause will be returned
*
* @param project the project
* @param in optional; the collection
* @return original collection or a new one with imported modules added
*/
public static Collection<String> findProjectImportedModules(Project project, Collection<String> in) {
Collection<String> result = in != null ? in : new HashSet<>();
if (project == null) {
return result;
}
for (SourceGroup sg : org.netbeans.api.project.ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) {
if (isNormalRoot(sg)) {
ClasspathInfo cpi = ClasspathInfo.create(sg.getRootFolder());
ClassPath mcp = cpi.getClassPath(PathKind.COMPILE);
for (FileObject r : mcp.getRoots()) {
URL u = URLMapper.findURL(r, URLMapper.INTERNAL);
String modName = SourceUtils.getModuleName(u);
if (modName != null) {
result.add(modName);
}
}
}
}
return result;
}
示例5: addRequires
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private void addRequires(FileObject createdFile, Set<String> requiredModuleNames) throws IOException {
if (requiredModuleNames == null) {
requiredModuleNames = new LinkedHashSet<>();
ClassPath modulePath = ClassPath.getClassPath(createdFile, JavaClassPathConstants.MODULE_COMPILE_PATH);
for (FileObject root : modulePath.getRoots()) {
String name = SourceUtils.getModuleName(root.toURL(), true);
if (name != null) {
requiredModuleNames.add(name);
}
}
}
if (!requiredModuleNames.isEmpty()) {
final JavaSource src = JavaSource.forFileObject(createdFile);
if (src != null) {
final Set<String> mNames = requiredModuleNames;
src.runModificationTask((WorkingCopy copy) -> {
copy.toPhase(JavaSource.Phase.RESOLVED);
TreeMaker tm = copy.getTreeMaker();
ModuleTree modle = (ModuleTree) copy.getCompilationUnit().getTypeDecls().get(0);
ModuleTree newModle = modle;
for (String mName : mNames) {
newModle = tm.addModuleDirective(newModle, tm.Requires(false, false, tm.QualIdent(mName)));
}
copy.rewrite(modle, newModle);
}).commit();
}
}
}
示例6: getEjbModulesOfClasspath
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private Set<Project> getEjbModulesOfClasspath(ClassPath classPath) {
Set<Project> ejbProjects = new HashSet<Project>();
for (FileObject fileObject : classPath.getRoots()) {
Project rootOwner = FileOwnerQuery.getOwner(fileObject);
if (rootOwner != null) {
if (Util.isEjbModule(rootOwner)) {
ejbProjects.add(rootOwner);
}
}
}
return ejbProjects;
}
示例7: addSourceRoots
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private static void addSourceRoots(ClassPath ecp,
List<FileObject> allSourceRoots,
Set<FileObject> preferredRoots) {
FileObject[] sourceRoots = ecp.getRoots();
for (FileObject fr : sourceRoots) {
if (!preferredRoots.contains(fr) && !fr.isVirtual()) {
allSourceRoots.add(fr);
preferredRoots.add(fr);
}
}
}
示例8: getSourceRoots
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private String[] getSourceRoots(ClassPath classPath) {
FileObject[] sourceRoots = classPath.getRoots();
List<String> roots = new ArrayList<String>(sourceRoots.length);
for (FileObject fo : sourceRoots) {
String root = getRoot(fo);
if (root != null) {
roots.add(root);
}
}
return roots.toArray(new String[0]);
}
示例9: testSourcePath
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
public void testSourcePath() throws Exception {
ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.SOURCE);
assertNotNull("have some SOURCE classpath for src/", cp);
FileObject[] roots = cp.getRoots();
assertEquals("have one entry in " + cp, 1, roots.length);
assertEquals("that is src/", simple.getProjectDirectory().getFileObject("src"), roots[0]);
cp = ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE);
assertNotNull("have some SOURCE classpath for antsrc/", cp);
roots = cp.getRoots();
assertEquals("have one entry", 1, roots.length);
assertEquals("that is antsrc/", simple.getProjectDirectory().getFileObject("antsrc"), roots[0]);
cp = ClassPath.getClassPath(buildProperties, ClassPath.SOURCE);
assertNull("have no SOURCE classpath for build.properties", cp);
}
示例10: projects2RegisteredContent
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
static Map<Project, Map<FileObject, ClassPath>> projects2RegisteredContent(AtomicBoolean cancel) {
Set<String> sourceIds = new HashSet<String>();
for (PathRecognizer pr : Lookup.getDefault().lookupAll(PathRecognizer.class)) {
Set<String> ids = pr.getSourcePathIds();
if (ids == null) continue;
sourceIds.addAll(ids);
}
Map<Project, Map<FileObject, ClassPath>> sourceRoots = new IdentityHashMap<Project, Map<FileObject, ClassPath>>();
for (String id : sourceIds) {
for (ClassPath sCP : GlobalPathRegistry.getDefault().getPaths(id)) {
for (FileObject root : sCP.getRoots()) {
if (cancel.get()) return null;
Project owner = FileOwnerQuery.getOwner(root);
if (owner != null) {
Map<FileObject, ClassPath> projectSources = sourceRoots.get(owner);
if (projectSources == null) {
sourceRoots.put(owner, projectSources = new HashMap<FileObject, ClassPath>());
}
projectSources.put(root, sCP);
}
}
}
}
return sourceRoots;
}
示例11: getSourceRootsSet
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private static Set<String> getSourceRootsSet(ClassPath classPath) {
FileObject[] sourceRoots = classPath.getRoots();
Set<String> roots = new HashSet<String>(sourceRoots.length);
for (FileObject fo : sourceRoots) {
String root = getRoot(fo);
if (root != null) {
roots.add(root);
}
}
return roots;
}
示例12: getFO
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
/** Get a file object by name. */
public static FileObject getFO(FileObject file, String pkg, String name, String ext) throws Exception {
ClassPath cp=ClassPath.getClassPath(file, ClassPath.SOURCE);
String nam=(pkg != null && pkg.length() > 0)?pkg:"";
nam+=(name != null && name.length() > 0)?".":"";
nam=nam.replace('.', '/');
nam+=(name != null && name.length() > 0)?name:"";
nam+=(ext != null && ext.length() > 0)?"."+ext:"";
//check
FileObject[] root=cp.getRoots();
FileObject ret=cp.findResource(nam);
return ret;
}
示例13: testMerge
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
/**
* Test of merge method, of class ClassPathProviderMerger.
*/
public void testMerge() {
InstanceContent ic = new InstanceContent();
Lookup lookup = new AbstractLookup(ic);
ProviderImpl defaultCP = new ProviderImpl();
//for some weird reason the specific path doesn't work in this module.
// it worked fine in Java Support APIs module before moving here
// URL url = createURLReference("org/netbeans/modules/java/project/");
URL url = createURLReference("");
defaultCP.paths.put(ClassPath.COMPILE, ClassPathSupport.createClassPath(url));
ClassPathProviderMerger instance = new ClassPathProviderMerger(defaultCP);
ClassPathProvider result = instance.merge(lookup);
ClassPath cp = result.findClassPath(null, ClassPath.BOOT);
assertNull(cp);
ClassPath compile = result.findClassPath(null, ClassPath.COMPILE);
assertNotNull(compile);
FileObject[] fos = compile.getRoots();
assertNotNull(fos);
assertEquals(1, fos.length);
final AtomicInteger count = new AtomicInteger();
compile.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
final String propName = evt.getPropertyName();
if (ClassPath.PROP_ENTRIES.equals(propName) ||
ClassPath.PROP_ROOTS.equals(propName)) {
count.incrementAndGet();
}
}
});
ProviderImpl additional = new ProviderImpl();
//for some weird reason the specific path doesn't work in this module.
// it worked fine in Java Support APIs module before moving here
// additional.paths.put(ClassPath.COMPILE, ClassPathSupport.createClassPath(createURLReference("org/netbeans/spi/java/project/classpath/")));
// additional.paths.put(ClassPath.BOOT, ClassPathSupport.createClassPath(createURLReference("org/netbeans/spi/java/project/support/")));
additional.paths.put(ClassPath.COMPILE, ClassPathSupport.createClassPath(createURLReference("")));
additional.paths.put(ClassPath.BOOT, ClassPathSupport.createClassPath(createURLReference("")));
ic.add(additional);
fos = compile.getRoots();
assertNotNull(fos);
assertEquals(2, fos.length);
assertEquals(2, count.get()); // why 2 changes are fired?
cp = result.findClassPath(null, ClassPath.COMPILE);
assertEquals(cp, compile);
cp = result.findClassPath(null, ClassPath.BOOT);
assertNotNull(cp);
fos = cp.getRoots();
assertNotNull(fos);
assertEquals(fos.length, 1);
ic.remove(additional);
fos = compile.getRoots();
assertNotNull(fos);
assertEquals(1, fos.length);
assertEquals(4, count.get()); // why 2 changes are fired?
}
示例14: getValidBundleName
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private String getValidBundleName(String name) {
if (name == null) {
return null;
}
if (name.contains("..") || name.contains("//")) { // NOI18N
return null;
}
name = name.trim();
if ("".equals(name)) { // NOI18N
return null;
}
if (name.toLowerCase().endsWith(".java")) { // NOI18N
return null;
}
if (name.startsWith("/")) { // NOI18N
name = name.substring(1);
}
// We prefer if the name (without extension) can be used as a class name, but
// that can be too strict, so we allow to use any file that already exists.
int i = name.lastIndexOf('.');
String withPropertiesExt = i < 0 ? name + ".properties" : null; // NOI18N
FileObject sourceFile = getSourceFile();
ClassPath cp = ClassPath.getClassPath(sourceFile, ClassPath.SOURCE);
for (FileObject r : cp.getRoots()) {
if (FileUtil.isParentOf(r, sourceFile)) {
if (r.getFileObject(name) != null
|| (withPropertiesExt != null && r.getFileObject(withPropertiesExt) != null)) {
return name; // it exists
}
break;
}
}
String withoutExt;
String ext;
if (i >= 0) {
withoutExt = name.substring(0, i);
ext = name.substring(i+1); // one dot considered as extension
} else {
withoutExt = name;
ext = null;
}
if (withoutExt.contains(".") || withoutExt.length() == 0) { // NOI18N
return null; // likely entered dot-separated class name, but that's not allowed here
}
if (!SourceVersion.isName(withoutExt.replace('/', '.'))
|| (ext != null && !SourceVersion.isIdentifier(ext))) {
return null;
}
return name;
}
示例15: getProjectClasspath
import org.netbeans.api.java.classpath.ClassPath; //导入方法依赖的package包/类
private ClassPath getProjectClasspath(Project p) {
ClassPath result = null;
Set<FileObject> roots = new HashSet<FileObject>();
Sources sources = ProjectUtils.getSources(p);
if (sources != null) {
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
for (SourceGroup group : groups) {
roots.add(group.getRootFolder());
}
}
ClassPathProvider cpp = p.getLookup().lookup(ClassPathProvider.class);
Set<ClassPath> setCP = new HashSet<ClassPath>();
if (cpp != null) {
for (FileObject file : roots) {
ClassPath path = cpp.findClassPath(file, ClassPath.COMPILE);
setCP.add(path);
}
}
for (ClassPath cp : setCP) {
FileObject[] rootsCP = cp.getRoots();
for (FileObject fo : rootsCP) {
try {
FileObject[] aaa = SourceForBinaryQuery.findSourceRoots(fo.getURL()).getRoots();
roots.addAll(Arrays.asList(aaa));
} catch (Exception e) {
}
}
}
String platformId = null;
try {
Method evalMethod = p.getClass().getDeclaredMethod("evaluator"); //NOI18N
PropertyEvaluator evaluator = (PropertyEvaluator) evalMethod.invoke(p);
if (evaluator != null) {
platformId = evaluator.getProperty("platform.active");
}
} catch (Exception ex) {
}
JavaPlatform platform = OutputUtils.getActivePlatform(platformId); //NOI18N
if (platform != null) {
roots.addAll(Arrays.asList(platform.getSourceFolders().getRoots()));
}
result = ClassPathSupport.createClassPath(roots.toArray(new FileObject[roots.size()]));
return result;
}