Java Code Examples for org.netbeans.api.java.classpath.ClassPath#findAllResources()

The following examples show how to use org.netbeans.api.java.classpath.ClassPath#findAllResources() . 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: SourceUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NonNull
private static List<Pair<FileObject, ClassPath>> findAllResources(
        @NonNull final String resourceName,
        @NonNull final Predicate<? super FileObject> rootsFilter,
        @NonNull final ClassPath... cps) {
    final List<Pair<FileObject,ClassPath>> result = new ArrayList<>();
    for (ClassPath cp : cps) {
        for (FileObject fo : cp.findAllResources(resourceName)) {
            final FileObject root = cp.findOwnerRoot(fo);
            if (root != null && rootsFilter.test(root)) {
                result.add(Pair.<FileObject,ClassPath>of(fo, cp));
            }
        }
    }
    return result;
}
 
Example 2
Source File: DefaultITPlugin.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Location getOppositeLocation(
    final Location sourceLocation,
    final ClassPath fileObjCp,
    final String oppoResourceName,
    final boolean sourceToTest) {
FileObject fileObj = sourceLocation.getFileObject();
FileObject fileObjRoot;

if ((fileObjRoot = fileObjCp.findOwnerRoot(fileObj)) == null) {
    return null;
}

URL[] oppoRootsURLs = sourceToTest
	? UnitTestForSourceQuery.findUnitTests(fileObjRoot)
	: UnitTestForSourceQuery.findSources(fileObjRoot);
//if (sourceToTest && (oppoRootsURLs.length == 0)) {
//    PENDING - offer creation of new unit tests root
//}
if ((oppoRootsURLs == null) || (oppoRootsURLs.length == 0)) {
    return null;
}

ClassPath oppoRootsClassPath = ClassPathSupport
	.createClassPath(oppoRootsURLs);
final List<FileObject> oppoFiles = oppoRootsClassPath
	.findAllResources(oppoResourceName);
if (oppoFiles.isEmpty()) {
    //if (sourceToTest) {
    //    PENDING - offer creation of new test class
    //}
    return null;
}

return new Location(oppoFiles.get(0)/*, null*/);
   }
 
Example 3
Source File: DefaultPlugin.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
     *
     */
    private static Location getOppositeLocation(
                                    final Location sourceLocation,
                                    final ClassPath fileObjCp,
                                    final String oppoResourceName,
                                    final boolean sourceToTest) {
        FileObject fileObj = sourceLocation.getFileObject();
        FileObject fileObjRoot;
        
        if ((fileObjRoot = fileObjCp.findOwnerRoot(fileObj)) == null) {
            return null;
        }
        
        URL[] oppoRootsURLs = sourceToTest
                              ? UnitTestForSourceQuery.findUnitTests(fileObjRoot)
                              : UnitTestForSourceQuery.findSources(fileObjRoot);
        //if (sourceToTest && (oppoRootsURLs.length == 0)) {
        //    PENDING - offer creation of new unit tests root
        //}
        if ((oppoRootsURLs == null) || (oppoRootsURLs.length == 0)) {
            return null;
        }
        
        ClassPath oppoRootsClassPath = ClassPathSupport
                                           .createClassPath(oppoRootsURLs);
        final List<FileObject> oppoFiles = oppoRootsClassPath
                                           .findAllResources(oppoResourceName);
        if (oppoFiles.isEmpty()) {
            //if (sourceToTest) {
            //    PENDING - offer creation of new test class
            //}
            return null;
        }
        
//        final ElementHandle elementHandle = sourceLocation.getElementHandle();
//        if (elementHandle == null) {
            return new Location(oppoFiles.get(0)/*, null*/);
//        }
        
//        /* Build SOURCE classpath: */
//        ClassPath[] srcCpDelegates = new ClassPath[2];
//        if (sourceToTest) {
//            srcCpDelegates[0] = fileObjCp;
//            srcCpDelegates[1] = oppoRootsClassPath;
//        } else {
//            srcCpDelegates[0] = oppoRootsClassPath;
//            srcCpDelegates[1] = fileObjCp;
//        }
//        ClassPath srcClassPath
//                = ClassPathSupport.createProxyClassPath(srcCpDelegates);
//        
//        /* Build COMPILE classpath: */
//        FileObject[] oppoRoots = oppoRootsClassPath.getRoots();
//        ClassPath[] compCpDelegates = new ClassPath[oppoRoots.length + 1];
//        int delegateIndex = 0;
//        if (sourceToTest) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(fileObjRoot, COMPILE);
//        }
//        for (FileObject oppoRoot : oppoRoots) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(oppoRoot, COMPILE);
//        }
//        if (!sourceToTest) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(fileObjRoot, COMPILE);
//        }
//        ClassPath compClassPath
//                = ClassPathSupport.createProxyClassPath(compCpDelegates);
//        
//        /* Obtain the BOOT classpath: */
//        ClassPath bootClassPath = ClassPath.getClassPath(fileObj, BOOT);
//        
//        ClasspathInfo cpInfo = ClasspathInfo.create(bootClassPath,
//                                                    compClassPath,
//                                                    srcClassPath);
//        List<FileObject> files = new ArrayList<FileObject>(oppoFiles.size() + 1);
//        files.add(fileObj);
//        files.addAll(oppoFiles);
//        JavaSource javaSource = JavaSource.create(cpInfo, files);
//        
//        try {
//            MatchFinder matchFinder = new MatchFinder(sourceLocation,
//                                                      oppoFiles,
//                                                      sourceToTest);
//            javaSource.runUserActionTask(matchFinder, true);
//            return matchFinder.getResult();
//        } catch (IOException ex) {
//            Logger.getLogger("global").log(Level.SEVERE, null, ex);     //NOI18N
//            return null;
//        }
    }
 
Example 4
Source File: DefaultPlugin.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
     *
     */
    private static Location getOppositeLocation(
                                    final Location sourceLocation,
                                    final ClassPath fileObjCp,
                                    final String oppoResourceName,
                                    final boolean sourceToTest) {
        FileObject fileObj = sourceLocation.getFileObject();
        FileObject fileObjRoot;
        
        if ((fileObjRoot = fileObjCp.findOwnerRoot(fileObj)) == null) {
            return null;
        }
        
        URL[] oppoRootsURLs = sourceToTest
                              ? UnitTestForSourceQuery.findUnitTests(fileObjRoot)
                              : UnitTestForSourceQuery.findSources(fileObjRoot);
        //if (sourceToTest && (oppoRootsURLs.length == 0)) {
        //    PENDING - offer creation of new unit tests root
        //}
        if ((oppoRootsURLs == null) || (oppoRootsURLs.length == 0)) {
            return null;
        }
        
        ClassPath oppoRootsClassPath = ClassPathSupport
                                           .createClassPath(oppoRootsURLs);
        final List<FileObject> oppoFiles = oppoRootsClassPath
                                           .findAllResources(oppoResourceName);
        if (oppoFiles.isEmpty()) {
            //if (sourceToTest) {
            //    PENDING - offer creation of new test class
            //}
            return null;
        }
        
//        final ElementHandle elementHandle = sourceLocation.getElementHandle();
//        if (elementHandle == null) {
            return new Location(oppoFiles.get(0)/*, null*/);
//        }
        
//        /* Build SOURCE classpath: */
//        ClassPath[] srcCpDelegates = new ClassPath[2];
//        if (sourceToTest) {
//            srcCpDelegates[0] = fileObjCp;
//            srcCpDelegates[1] = oppoRootsClassPath;
//        } else {
//            srcCpDelegates[0] = oppoRootsClassPath;
//            srcCpDelegates[1] = fileObjCp;
//        }
//        ClassPath srcClassPath
//                = ClassPathSupport.createProxyClassPath(srcCpDelegates);
//        
//        /* Build COMPILE classpath: */
//        FileObject[] oppoRoots = oppoRootsClassPath.getRoots();
//        ClassPath[] compCpDelegates = new ClassPath[oppoRoots.length + 1];
//        int delegateIndex = 0;
//        if (sourceToTest) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(fileObjRoot, COMPILE);
//        }
//        for (FileObject oppoRoot : oppoRoots) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(oppoRoot, COMPILE);
//        }
//        if (!sourceToTest) {
//            compCpDelegates[delegateIndex++]
//                    = ClassPath.getClassPath(fileObjRoot, COMPILE);
//        }
//        ClassPath compClassPath
//                = ClassPathSupport.createProxyClassPath(compCpDelegates);
//        
//        /* Obtain the BOOT classpath: */
//        ClassPath bootClassPath = ClassPath.getClassPath(fileObj, BOOT);
//        
//        ClasspathInfo cpInfo = ClasspathInfo.create(bootClassPath,
//                                                    compClassPath,
//                                                    srcClassPath);
//        List<FileObject> files = new ArrayList<FileObject>(oppoFiles.size() + 1);
//        files.add(fileObj);
//        files.addAll(oppoFiles);
//        JavaSource javaSource = JavaSource.create(cpInfo, files);
//        
//        try {
//            MatchFinder matchFinder = new MatchFinder(sourceLocation,
//                                                      oppoFiles,
//                                                      sourceToTest);
//            javaSource.runUserActionTask(matchFinder, true);
//            return matchFinder.getResult();
//        } catch (IOException ex) {
//            Logger.getLogger("global").log(Level.SEVERE, null, ex);     //NOI18N
//            return null;
//        }
    }
 
Example 5
Source File: OutputUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Called when some sort of action is performed on a line.
 * @param ev the event describing the line
 */
@Override
@NbBundle.Messages({
    "# {0} - class name",
    "NotFound=Class \"{0}\" not found on classpath", 
    "# {0} - file name",
    "NoSource=Source file not found for \"{0}\""
})
public void outputLineAction(OutputEvent ev) {
    StacktraceAttributes sa = matchStackTraceLine(ev.getLine());
    if(sa == null || sa.method == null || sa.file == null) {
        Logger.getLogger(OutputUtils.class.getName()).log(Level.WARNING, "No file found for output line {0}", ev.getLine()); // NOI18N
        StatusDisplayer.getDefault().setStatusText(Bundle.NoSource(ev.getLine()));
        return;
    } 
    
    ClassPath classPath = getClassPath();
    int index = sa.method.indexOf(sa.file);
    String packageName = sa.method.substring(0, index).replace('.', '/'); //NOI18N
    String resourceName = packageName + sa.file + ".class"; //NOI18N
    // issue #258546; have to check all resources. javafx unpacks all classes to target,
    // SourceForBinaryQuery then fails to find the according java file ...            
    List<FileObject> resources = classPath.findAllResources(resourceName);
    if (resources != null) {
        for (FileObject resource : resources) {                    
            FileObject root = classPath.findOwnerRoot(resource);
            if (root != null) {
                URL url = URLMapper.findURL(root, URLMapper.INTERNAL);
                SourceForBinaryQuery.Result res = SourceForBinaryQuery.findSourceRoots(url);
                FileObject[] rootz = res.getRoots();
                for (int i = 0; i < rootz.length; i++) {
                    String path = packageName + sa.file + ".java"; //NOI18N
                    FileObject javaFo = rootz[i].getFileObject(path);
                    if (javaFo != null) {
                        try {
                            DataObject obj = DataObject.find(javaFo);
                            EditorCookie cookie = obj.getLookup().lookup(EditorCookie.class);
                            if(cookie != null) {
                                int lineInt = Integer.parseInt(sa.lineNum);
                                try {
                                    cookie.getLineSet().getCurrent(lineInt - 1).show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
                                } catch (IndexOutOfBoundsException x) { // #155880
                                    cookie.open();
                                }
                            } else {
                                Logger.getLogger(OutputUtils.class.getName()).log(Level.WARNING, "No cookie found for dataobject {0}", obj); // NOI18N
                            }
                            return;
                        } catch (DataObjectNotFoundException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    }
                }
            }
        }                
        StatusDisplayer.getDefault().setStatusText(Bundle.NoSource(sa.file));
    } else {
        StatusDisplayer.getDefault().setStatusText(Bundle.NotFound(sa.file));
    }
}