本文整理汇总了Java中org.netbeans.api.java.source.ClasspathInfo.create方法的典型用法代码示例。如果您正苦于以下问题:Java ClasspathInfo.create方法的具体用法?Java ClasspathInfo.create怎么用?Java ClasspathInfo.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.netbeans.api.java.source.ClasspathInfo
的用法示例。
在下文中一共展示了ClasspathInfo.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFileObjectFromClassName
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
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;
}
示例2: getClasspathInfo
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public ClasspathInfo getClasspathInfo() {
if (cpi != null) {
return cpi;
}
FileObject fileObject = project.getProjectDirectory();
SourceGroup[] grp = org.netbeans.api.project.ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
List<FileObject> roots = new ArrayList<>(grp.length);
for (SourceGroup sg : grp) {
roots.add(sg.getRootFolder());
}
return cpi = ClasspathInfo.create(
ClassPath.getClassPath(fileObject, ClassPath.BOOT), // JDK classes
ClassPath.EMPTY,
ClassPathSupport.createClassPath(roots.toArray(new FileObject[roots.size()]))
);
}
示例3: adjustSpans
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
private void adjustSpans(Iterable<? extends Tree> original, String code) {
if (tree2Tag == null) {
return; //nothing to copy
}
java.util.List<Tree> linearized = new LinkedList<Tree>();
if (!new Linearize().scan(original, linearized) != Boolean.TRUE) {
return; //nothing to copy
}
ClassPath empty = ClassPathSupport.createClassPath(new URL[0]);
ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty);
JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null, Arrays.asList(FileObjects.memoryFileObject("", "Scratch.java", code)));
com.sun.tools.javac.util.Context ctx = javacTask.getContext();
JavaCompiler.instance(ctx).genEndPos = true;
CompilationUnitTree tree = javacTask.parse().iterator().next(); //NOI18N
SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions();
ClassTree clazz = (ClassTree) tree.getTypeDecls().get(0);
new CopyTags(tree, sp).scan(clazz.getModifiers().getAnnotations(), linearized);
}
示例4: inferModuleNames
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
private Set<String> inferModuleNames(FileObject t, List<FileObject> jsources) throws IOException {
ClasspathInfo cpi = ClasspathInfo.create(t);
JavaSource src = JavaSource.create(cpi, jsources);
Set<String> unresolved = new HashSet<>();
src.runUserActionTask(new Task<CompilationController>() {
@Override
public void run(CompilationController parameter) throws Exception {
parameter.toPhase(JavaSource.Phase.RESOLVED);
Scanner s = new Scanner(parameter, unresolved);
s.scan(parameter.getCompilationUnit(), null);
}
}, true);
ClassPath searchPath = ClassPathSupport.createProxyClassPath(
cpi.getClassPath(PathKind.MODULE_BOOT),
cpi.getClassPath(PathKind.MODULE_COMPILE));
Set<String> moduleNames = new HashSet<>();
for (String fqn : unresolved) {
findModuleNames(fqn, searchPath, moduleNames);
}
return moduleNames;
}
示例5: query
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
@Override
public void query(CompletionResultSet resultSet, Document doc, int caretOffset) {
try {
ClasspathInfo cpInfo = ClasspathInfo.create(doc);
ParserManager.parse(Collections.singleton(Source.create(doc)),
createTask(cpInfo,
component,
resultSet,
doc, caretOffset, queryType)
);
resultSet.setHasAdditionalItems(additionalItems);
resultSet.addAllItems(items);
resultSet.finish();
} catch (ParseException ex) {
Exceptions.printStackTrace(ex);
}
}
示例6: testPerformance
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void testPerformance() throws Exception {
// Logger.getLogger(AnnotationScanner.class.getName()).setLevel(Level.FINEST);
final int ENTITY_COUNT = 500;
String template = TestUtilities.copyStreamToString(getClass().getResourceAsStream("Table.javax"));
Map<String, String> args = new HashMap<String, String>();
MapFormat format = new MapFormat(args);
format.setLeftBrace("__");
format.setRightBrace("__");
for (int i = 0; i < ENTITY_COUNT; i++) {
String name = "Table" + i;
args.put("NUM", Integer.toString(i));
args.put("NAME", name);
String contents = format.format(template);
TestUtilities.copyStringToFileObject(srcFO, name + ".java", contents);
}
long startTime = System.nanoTime();
IndexingManager.getDefault().refreshIndexAndWait(srcFO.getURL(), null);
long compilationDoneTime = System.nanoTime();
ClasspathInfo cpi = ClasspathInfo.create(srcFO);
final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
final int[] entityCount = { 0 };
final long initialScanDoneTime[] = { 0L };
final List<ElementHandle<TypeElement>> typeHandles = new ArrayList<ElementHandle<TypeElement>>();
helper.runJavaSourceTask(new Callable<Void>() {
public Void call() throws InterruptedException {
helper.getAnnotationScanner().findAnnotations("javax.persistence.Entity", AnnotationScanner.TYPE_KINDS, new AnnotationHandler() {
public void handleAnnotation(TypeElement type, Element element, AnnotationMirror annotation) {
typeHandles.add(ElementHandle.create(type));
entityCount[0]++;
}
});
initialScanDoneTime[0] = System.nanoTime();
return null;
}
});
assertEquals(ENTITY_COUNT, entityCount[0]);
System.out.println("Compilation time (ms): " + (compilationDoneTime - startTime) / 1e6);
System.out.println("Initial annotation scan time (ms): " + (initialScanDoneTime[0] - compilationDoneTime) / 1e6);
}
示例7: getClasspathInfo
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public ClasspathInfo getClasspathInfo() {
if (cpInfo == null) {
final ClassPath cp = ClassPathSupport.createClassPath(toURL(rootURI));
cpInfo = isBinary ?
ClassPath.BOOT.equals(cpType) ?
ClasspathInfo.create(cp,ClassPath.EMPTY,ClassPath.EMPTY):
ClasspathInfo.create(ClassPath.EMPTY,cp,ClassPath.EMPTY):
ClasspathInfo.create(ClassPath.EMPTY,ClassPath.EMPTY,cp);
}
return cpInfo;
}
示例8: getFileObject
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
@Override
@CheckForNull
public final FileObject getFileObject() {
FileObject res = cachedFo;
if (res == null) {
final ClasspathInfo cpInfo = ClasspathInfo.create(ClassPath.EMPTY,
ClassPath.EMPTY, ClassPathSupport.createClassPath(root));
res = cachedFo = SourceUtils.getFile(owner, cpInfo);
}
return res;
}
示例9: testClass
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void testClass() throws Exception {
TestUtilities.copyStringToFileObject(srcFO, "Annotated.java",
"import java.lang.annotation.*;" +
"@interface Annotation {" +
" Class<?> classValue();" +
" Class<?> classValue2();" +
" Class<?> classValue3();" +
"}" +
"@Annotation(classValue = Object.class, classValue2 = \"error\")" +
"public class Annotated {" +
"}");
IndexingManager.getDefault().refreshIndexAndWait(srcFO.getURL(), null);
ClasspathInfo cpi = ClasspathInfo.create(srcFO);
final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
helper.runJavaSourceTask(new Runnable() {
public void run() {
TypeElement annotated = helper.getCompilationController().getElements().getTypeElement("Annotated");
AnnotationMirror annotation = annotated.getAnnotationMirrors().iterator().next();
AnnotationParser parser = AnnotationParser.create(helper);
parser.expectClass("classValue", AnnotationParser.defaultValue("java.lang.Double"));
parser.expectClass("classValue2", AnnotationParser.defaultValue("java.lang.String"));
parser.expectClass("classValue3", AnnotationParser.defaultValue("java.lang.Integer"));
ParseResult parseResult = parser.parse(annotation);
assertEquals("java.lang.Object", parseResult.get("classValue", String.class));
assertEquals("java.lang.String", parseResult.get("classValue2", String.class));
assertEquals("java.lang.Integer", parseResult.get("classValue3", String.class));
}
});
}
示例10: testJavaContextListener
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void testJavaContextListener() throws Exception {
IndexingManager.getDefault().refreshIndexAndWait(srcFO.getURL(), null);
ClasspathInfo cpi = ClasspathInfo.create(srcFO);
final AnnotationModelHelper helper = AnnotationModelHelper.create(cpi);
final boolean[] contextLeft = { false };
JavaContextListener listener = new JavaContextListener() {
public void javaContextLeft() {
contextLeft[0] = true;
}
};
helper.addJavaContextListener(listener);
Callable<Void> empty = new Callable<Void>() {
public Void call() {
return null;
}
};
helper.runJavaSourceTask(empty);
assertTrue(contextLeft[0]);
contextLeft[0] = false;
helper.runJavaSourceTask(empty, false);
assertFalse(contextLeft[0]);
Future<Void> future = helper.runJavaSourceTaskWhenScanFinished(empty);
if (!future.isDone()) {
SourceUtils.waitScanFinished();
}
assertTrue(contextLeft[0]);
WeakReference<JavaContextListener> listenerRef = new WeakReference<JavaContextListener>(listener);
listener = null;
assertGC("Should be possible to GC listener", listenerRef);
}
示例11: testMixed
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void testMixed() throws Exception {
FileObject fo = URLMapper.findFileObject(src.toURL());
IndexingManager.getDefault().refreshIndexAndWait(src.toURL(), null, true);
final ClasspathInfo cpInfo = ClasspathInfo.create(src);
final Collection<? extends FileObject> res = cpInfo.getClassIndex().getResources(
ElementHandleAccessor.getInstance().create(ElementKind.CLASS, "org.me.Foo"), //NOI18N
EnumSet.allOf(ClassIndex.SearchKind.class),
EnumSet.of(ClassIndex.SearchScope.SOURCE),
EnumSet.of(ClassIndex.ResourceType.SOURCE));
assertEquals(1, res.size());
assertEquals(java, res.iterator().next());
}
示例12: testGetTypeDeclaration
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void testGetTypeDeclaration() throws Exception {
ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
JavacTaskImpl jTask = JavacParser.createJavacTask(ci, (DiagnosticListener) null, (String) null, null, null, null, null, null, Collections.emptyList());
jTask.enter();
List<String> notFound = new LinkedList<String>();
JarFile jf = new JarFile( rtJar );
for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) {
JarEntry je = (JarEntry)entries.nextElement();
String jeName = je.getName();
if ( !je.isDirectory() && jeName.endsWith( ".class" ) ) {
String typeName = jeName.substring( 0, jeName.length() - ".class".length() );
typeName = typeName.replace( "/", "." ); //.replace( "$", "." );
TypeElement te = ElementUtils.getTypeElementByBinaryName(jTask, typeName );
// assertNotNull( "Declaration for " + typeName + " should not be null.", td );
if ( te == null ) {
if (!typeName.endsWith("package-info")) {
notFound.add( typeName );
}
}
}
}
assertTrue( "Should be empty " + notFound, notFound.isEmpty() );
}
示例13: invokeByJavaSource
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
private static void invokeByJavaSource (
@NonNull final Runnable runnable) throws IOException {
Parameters.notNull("runnable", runnable); //NOI18N
final ClasspathInfo info = ClasspathInfo.create(JavaPlatform.getDefault().getBootstrapLibraries(),
ClassPathSupport.createClassPath(new URL[0]),
ClassPathSupport.createClassPath(new URL[0]));
final JavaSource js = JavaSource.create(info);
js.runWhenScanFinished((final CompilationController controller) -> {
runnable.run();
}, true);
}
示例14: testQuery
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public static List<CompletionItem> testQuery(Source s, Document doc, int queryType, int caretOffset) throws ParseException {
Q q = new Q(null, queryType);
ClasspathInfo cpInfo = ClasspathInfo.create(doc);
UserTask t = q.createTask(cpInfo, null, null, doc, caretOffset, queryType);
ParserManager.parse(Collections.singleton(s), t);
return q.items;
}
示例15: doTest197097
import org.netbeans.api.java.source.ClasspathInfo; //导入方法依赖的package包/类
public void doTest197097() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package zoo;\n" +
"\n" +
"public class A {\n" +
" public class I extends java.util.ArrayList {\n" +
" public I(java.util.Collection c) {\n" +
" super(c);\n" +
" }\n" +
" }\n" +
"}\n"
);
FileObject emptyJava = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/Empty.java");
emptyJava.setAttribute("template", Boolean.TRUE);
FileObject classJava = FileUtil.createData(FileUtil.getConfigRoot(), "Templates/Classes/Class.java");
classJava.setAttribute("template", Boolean.TRUE);
Writer w = new OutputStreamWriter(classJava.getOutputStream(), "UTF-8");
w.write("package zoo;\npublic class Template {\n \n}");
w.close();
FileObject testSourceFO = FileUtil.toFileObject(testFile);
assertNotNull(testSourceFO);
ClassPath sourcePath = ClassPath.getClassPath(testSourceFO, ClassPath.SOURCE);
assertNotNull(sourcePath);
FileObject[] roots = sourcePath.getRoots();
assertEquals(1, roots.length);
final FileObject sourceRoot = roots[0];
assertNotNull(sourceRoot);
ClassPath compilePath = ClassPath.getClassPath(testSourceFO, ClassPath.COMPILE);
assertNotNull(compilePath);
ClassPath bootPath = ClassPath.getClassPath(testSourceFO, ClassPath.BOOT);
assertNotNull(bootPath);
ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, sourcePath);
String golden1 =
"package zoo;\n" +
"\n" +
"\n" + //XXX
"public class I extends java.util.ArrayList {\n\n" +
" public I(java.util.Collection c) {\n" +
" super(c);\n" +
" }\n" +
" \n" +//XXX
"}\n";
JavaSource javaSource = JavaSource.create(cpInfo, FileUtil.toFileObject(testFile));
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void cancel() {
}
public void run(WorkingCopy workingCopy) throws Exception {
workingCopy.toPhase(JavaSource.Phase.RESOLVED);
TreeMaker make = workingCopy.getTreeMaker();
Tree classDecl = ((ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0)).getMembers().get(1);
CompilationUnitTree newTree = make.CompilationUnit(
sourceRoot,
"zoo/I.java",
Collections.<ImportTree>emptyList(),
Collections.<Tree>singletonList(classDecl)
);
workingCopy.rewrite(null, newTree);
MethodTree constructor = (MethodTree) ((ClassTree) classDecl).getMembers().get(0);
workingCopy.rewrite(constructor, make.setLabel(constructor, "I"));
}
};
ModificationResult result = javaSource.runModificationTask(task);
result.commit();
String res = TestUtilities.copyFileToString(new File(getDataDir().getAbsolutePath() + "/zoo/I.java"));
System.err.println(res);
assertEquals(res, golden1);
}