private static void runUserActionTask(JTextComponent target, Task<CompilationController> aTask) { FileObject fobj = getFileObject(target); if (fobj == null) { return; } try { JavaSource src = JavaSource.create(ClasspathInfo.create(fobj)); if (src != null) src.runUserActionTask(aTask, false); } catch (IOException e) { Exceptions.printStackTrace(e); } }
private static boolean containsAnnotatedJsfResource(CompilationController parameter) { if (jsfResourcesElementHandles == null) { loadJsfResourcesElementsHandles(parameter); } ClassIndex classIndex = parameter.getClasspathInfo().getClassIndex(); for (ElementHandle jsfResourceElementHandle : jsfResourcesElementHandles) { Set<ElementHandle<TypeElement>> elements = classIndex.getElements( jsfResourceElementHandle, EnumSet.of(ClassIndex.SearchKind.TYPE_REFERENCES), EnumSet.of(ClassIndex.SearchScope.SOURCE)); for (ElementHandle<TypeElement> handle : elements) { TypeElement element = handle.resolve(parameter); if (element != null) { List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (ElementHandle.create(annotationMirror.getAnnotationType().asElement()) .equals(jsfResourceElementHandle)) { return true; } } } } } return false; }
String src = info.getText(); FileObject file = info.getFileObject(); String fileName = FileUtil.getFileDisplayName(file); String origName = file.getName(); File f = new File(dumpDir + origName + ".dump"); // NOI18N boolean dumpSucceeded = false; writer.println("----- Classpath: ---------------------------------------------"); // NOI18N final ClassPath bootPath = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT); final ClassPath classPath = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); final ClassPath sourcePath = info.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); writer.println("bootPath: " + (bootPath != null ? bootPath.toString() : "null")); writer.println("classPath: " + (classPath != null ? classPath.toString() : "null")); writer.println("sourcePath: " + (sourcePath != null ? sourcePath.toString() : "null")); Throwable t = Exceptions.attachMessage(exc, "An error occurred during parsing of \'" + fileName + "\'. Please report a bug against java/source and attach dump file '" // NOI18N + f.getAbsolutePath() + "'."); // NOI18N Exceptions.printStackTrace(t); } catch (RuntimeException re) { Exceptions.printStackTrace(exc);
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; }
cpInfo = ClasspathInfo.create(sourceFile); final ClassPath cp = cpInfo.getClassPath(PathKind.SOURCE); assert cp != null; this.root = cp.findOwnerRoot(sourceFile); if (singleSource) { if (oldInfo != null && weakCpListener != null) { oldInfo.removeChangeListener(weakCpListener); this.weakCpListener = null; cpInfo.addChangeListener (this.weakCpListener); if (scp != cpInfo.getClassPath(PathKind.SOURCE)) { new Object[]{ this.file, owner == null ? "null" : (FileUtil.getFileDisplayName(owner.getProjectDirectory())+" ("+owner.getClass()+")"), cpInfo.getClassPath(PathKind.SOURCE), //NOI18N scp }); if (this.weakCpListener != null) { cpInfo.removeChangeListener(weakCpListener); cpInfo = ClasspathInfo.create(this.file); final ClassPath cp = cpInfo.getClassPath(PathKind.SOURCE); assert cp != null; this.root = cp.findOwnerRoot(this.file); this.weakCpListener = WeakListeners.change(cpInfoListener, cpInfo); cpInfo.addChangeListener (this.weakCpListener); JavaSourceAccessor.getINSTANCE().invalidateCachedClasspathInfo(this.file);
: subwordsPattern != null ? ClassIndex.NameKind.REGEXP : Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX; for (ElementHandle<TypeElement> handle : controller.getClasspathInfo().getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) { TypeElement te = handle.resolve(controller); if (te != null && trees.isAccessible(scope, te) && types.isSubtype(types.getDeclaredType(te), baseType)) { subtypes.add(types.getDeclaredType(te)); LinkedList<DeclaredType> bases = new LinkedList<>(); bases.add(baseType); ClassIndex index = controller.getClasspathInfo().getClassIndex(); while (!bases.isEmpty()) { DeclaredType head = bases.remove(); boolean isRaw = !tas.iterator().hasNext(); subtypes: for (ElementHandle<TypeElement> eh : index.getElements(ElementHandle.create(elem), EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS), EnumSet.allOf(ClassIndex.SearchScope.class))) { TypeElement e = eh.resolve(controller); if (e != null) { if (trees.isAccessible(scope, e)) { Logger.getLogger("global").log(Level.FINE, String.format("Cannot resolve: %s on bootpath: %s classpath: %s sourcepath: %s\n", eh.toString(), controller.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT), controller.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE), controller.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE)));
case EXTENDS: case SUPER: addTypes(env, EnumSet.of(CLASS, INTERFACE, ENUM, ANNOTATION_TYPE, TYPE_PARAMETER), null); break; boolean insideNew = false; if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getKind()) && ((ClassTree) parent).getExtendsClause() == fa) { kinds = EnumSet.of(CLASS); env.afterExtends(); } else if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getKind()) && ((ClassTree) parent).getImplementsClause().contains(fa)) { kinds = EnumSet.of(INTERFACE); } else if (parent.getKind() == Tree.Kind.IMPORT) { inImport = true; if (results.isEmpty() && ((PackageElement) el).getQualifiedName() == el.getSimpleName()) { ClassIndex ci = controller.getClasspathInfo().getClassIndex(); if (el.getEnclosedElements().isEmpty() && ci.getPackageNames(el.getSimpleName() + ".", true, EnumSet.allOf(ClassIndex.SearchScope.class)).isEmpty()) { Trees trees = controller.getTrees(); Scope scope = env.getScope(); for (ElementHandle<TypeElement> teHandle : ci.getDeclaredTypes(el.getSimpleName().toString(), ClassIndex.NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class))) { TypeElement te = teHandle.resolve(controller); if (te != null && trees.isAccessible(scope, te)) { addMembers(env, te.asType(), te, kinds, baseType, inImport, insideNew, true);
excludeHandles = new HashSet<>(excludes.size()); for (Element el : excludes) { excludeHandles.add(ElementHandle.create(el)); if (!kinds.contains(ElementKind.CLASS) && !kinds.contains(ElementKind.INTERFACE)) { Set<ElementHandle<TypeElement>> declaredTypes = controller.getClasspathInfo().getClassIndex().getDeclaredTypes(EMPTY, ClassIndex.NameKind.PREFIX, EnumSet.allOf(ClassIndex.SearchScope.class)); Map<String, ElementHandle<TypeElement>> removed = new HashMap<>(declaredTypes.size()); Set<String> doNotRemove = new HashSet<>(); continue; if (!kinds.contains(name.getKind()) && !doNotRemove.contains(name.getQualifiedName())) { removed.put(name.getQualifiedName(), name); continue; : subwordsPattern != null ? ClassIndex.NameKind.REGEXP : Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX; Set<ElementHandle<TypeElement>> declaredTypes = controller.getClasspathInfo().getClassIndex().getDeclaredTypes(subwordsPattern != null ? subwordsPattern : prefix != null ? prefix : EMPTY, kind, EnumSet.allOf(ClassIndex.SearchScope.class)); results.ensureCapacity(results.size() + declaredTypes.size()); for (ElementHandle<TypeElement> name : declaredTypes) {
if (ANONYMOUS.matcher(i.next().getBinaryName()).find()) { i.remove(); final ClassIndexImpl ciImpl = ClassIndexManager.getDefault().getUsagesQuery(depRoot, true); if (ciImpl != null) { final ClassIndex index = ClasspathInfo.create(EMPTY, EMPTY, ClassPathSupport.createClassPath(depRoot)).getClassIndex(); final Collection<Map<URL,List<URL>>> depMaps = new ArrayList<Map<URL,List<URL>>>(2); if (sourceDeps != null) { final ElementHandle<TypeElement> e = queue.poll(); if (toHandle.add(e)) queue.addAll(index.getElements(e, EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS), EnumSet.of(ClassIndex.SearchScope.SOURCE))); files.addAll(index.getResources(e, EnumSet.complementOf(EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS)), EnumSet.of(ClassIndex.SearchScope.SOURCE))); urls.add(file.getURL());
return Collections.emptyMap (); final SourceGroup sourceGroup = MidpProjectSupport.getSourceGroup (project); final Set<ElementHandle<TypeElement>> allHandles = info.getClassIndex ().getDeclaredTypes ("", ClassIndex.NameKind.PREFIX, EnumSet.of (ClassIndex.SearchScope.SOURCE, ClassIndex.SearchScope.DEPENDENCIES)); // NOI18N final Map<String, ComponentDescriptor> registry = resolveRegistryMap (project); final HashMap<String, Item> result = new HashMap<String, Item> ();
protected final boolean isValidPackage(ClasspathInfo pathInfo, String pkg) { assert pathInfo != null : "ClasspathInfo can not be null"; // the following statement gives us all the packages *starting* with the // first parameter. We have to check for exact matches ourselves. See # 142372 Set<String> pkgSet = pathInfo.getClassIndex().getPackageNames(pkg, true, EnumSet.allOf(ClassIndex.SearchScope.class)); if (pkgSet.size() > 0) { LOG.log(Level.FINEST, "Packages with prefix : {0}", pkg); LOG.log(Level.FINEST, " found : {0}", pkgSet); for (String singlePkg : pkgSet) { if (singlePkg.equals(pkg)){ LOG.log(Level.FINEST, "Exact match found."); return true; } } return false; } else { return false; } }
private static Set<ImportCandidate> findJavaImportCandidates(FileObject fo, String packageName, String missingClass) { final Set<ImportCandidate> candidates = new HashSet<>(); final ClasspathInfo pathInfo = createClasspathInfo(fo); Set<ElementHandle<TypeElement>> typeNames = pathInfo.getClassIndex().getDeclaredTypes( missingClass, NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class)); for (ElementHandle<TypeElement> typeName : typeNames) { ElementKind kind = typeName.getKind(); // Skip classes within the same package String pkgName = GroovyUtils.stripClassName(typeName.getQualifiedName()); if (packageName == null && pkgName == null) { // Probably both in default package continue; } if (packageName != null && packageName.equals(pkgName)) { continue; } if (kind == ElementKind.CLASS || kind == ElementKind.INTERFACE || kind == ElementKind.ANNOTATION_TYPE) { candidates.add(createImportCandidate(missingClass, typeName.getQualifiedName(), kind)); } } return candidates; }
TypeElement te = (TypeElement) ((DeclaredType) err).asElement(); if (te.getQualifiedName() == te.getSimpleName()) { ClassIndex ci = controller.getClasspathInfo().getClassIndex(); for (ElementHandle<TypeElement> eh : ci.getDeclaredTypes(te.getSimpleName().toString(), ClassIndex.NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class))) { te = eh.resolve(controller); if (te != null) { st.add(types.erasure(te.asType()));
FileObject targetFO; if (target.isAbsolute()) { targetFO = FileUtil.toFileObject(FileUtil.normalizeFile(target)); } else { targetFO = fo.getParent().getFileObject(file); if (targetFO.isData() && targetFO.isValid() && !targetFO.isVirtual()) { openInEditor(targetFO); } else { final ClasspathInfo cp = ClasspathInfo.create(doc); JavaSource js = JavaSource.create(cp, Collections.EMPTY_LIST); try { Exceptions.printStackTrace(ex);
private void addAllStaticMemberNames(final Env env) { String prefix = env.getPrefix(); if (prefix != null && prefix.length() > 0) { CompilationController controller = env.getController(); Set<? extends Element> excludes = env.getExcludes(); Set<ElementHandle<Element>> excludeHandles = null; if (excludes != null) { excludeHandles = new HashSet<>(excludes.size()); for (Element el : excludes) { excludeHandles.add(ElementHandle.create(el)); } } ClassIndex.NameKind kind = Utilities.isCaseSensitive() ? ClassIndex.NameKind.PREFIX : ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX; Iterable<Symbols> declaredSymbols = controller.getClasspathInfo().getClassIndex().getDeclaredSymbols(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class)); for (Symbols symbols : declaredSymbols) { if (Utilities.isExcluded(symbols.getEnclosingType().getQualifiedName()) || excludeHandles != null && excludeHandles.contains(symbols.getEnclosingType()) || isAnnonInner(symbols.getEnclosingType())) { continue; } for (String name : symbols.getSymbols()) { if (!Utilities.isExcludeMethods() || !Utilities.isExcluded(symbols.getEnclosingType().getQualifiedName() + '.' + name)) { results.add(itemFactory.createStaticMemberItem(symbols.getEnclosingType(), name, anchorOffset, env.addSemicolon(), env.getReferencesCount(), controller.getSnapshot().getSource())); } } } } }
throw new IllegalArgumentException ("fileObject == null"); //NOI18N if (!fileObject.isValid()) { LOGGER.log(Level.FINE, "FileObject ({0}) passed to JavaSource.forFileObject is invalid", fileObject.toURI().toString()); return null; if ( fileObject.getFileSystem().isDefault() && fileObject.getAttribute("javax.script.ScriptEngine") != null && fileObject.getAttribute("template") == Boolean.TRUE) { ClassPath compilePath = ClassPath.getClassPath(fileObject, ClassPath.COMPILE); if (compilePath == null) { compilePath = ClassPathSupport.createClassPath(new URL[0]); ClassPath srcPath = ClassPath.getClassPath(fileObject, ClassPath.SOURCE); if (srcPath == null) { srcPath = ClassPathSupport.createClassPath(new URL[0]); bootPath = ClassPathSupport.createProxyClassPath(execPath, bootPath); final ClasspathInfo info = ClasspathInfo.create(bootPath, compilePath, srcPath); FileObject root = ClassPathSupport.createProxyClassPath( ClassPathSupport.createClassPath(CachingArchiveProvider.getDefault().ctSymRootsFor(bootPath)), Exceptions.printStackTrace(ioe);
FileObject f = q.poll(); if (f.isData() && JAVA_MIME_TYPE.equals(FileUtil.getMIMEType(f))) { toProcess.add(f); if (f.isFolder()) { q.addAll(Arrays.asList(f.getChildren())); handle.switchToDeterminate(toProcess.size()); ClasspathInfo cpInfo = ClasspathInfo.create(toProcess.get(0)); JavaSource js = JavaSource.create(cpInfo, toProcess); final AtomicInteger f = new AtomicInteger(); Exceptions.printStackTrace(ex);
final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT); final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); final String message = String.format("SuperOnePassCompileWorker caused an exception\nFile: %s\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N tuple.indexable.getURL().toString(), FileUtil.getFileDisplayName(context.getRoot()), bootPath == null ? null : bootPath.toString(), classPath == null ? null : classPath.toString(), sourcePath == null ? null : sourcePath.toString() ); modifiedTypes.addAll(aTypes); final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT); final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); final String message = String.format("SuperOnePassCompileWorker caused an exception\nRoot: %s\nBootpath: %s\nClasspath: %s\nSourcepath: %s", //NOI18N FileUtil.getFileDisplayName(context.getRoot()), final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT); final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE); Level level = t instanceof FatalError ? Level.FINEST : Level.WARNING; if (JavaIndex.LOG.isLoggable(level)) { final ClassPath bootPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.BOOT); final ClassPath classPath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.COMPILE); final ClassPath sourcePath = javaContext.getClasspathInfo().getClassPath(ClasspathInfo.PathKind.SOURCE);
for (final FileObject root : sourceRoots) { try { final File rootFile = FileUtil.toFile(root); ClassPath bootPath = ClassPath.getClassPath(root, ClassPath.BOOT); ClassPath compilePath = ClassPath.getClassPath(root, ClassPath.COMPILE); ClassPath srcPath = ClassPathSupport.createClassPath(new FileObject[] {root}); ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, srcPath); JavaSource js = JavaSource.create(cpInfo); js.runUserActionTask(new Task<CompilationController>() { Exceptions.printStackTrace(ioe); return Collections.<ElementHandle<TypeElement>>emptySet();
FileObject file = SourceUtils.getFile(ElementHandle.create(typeElement), ClasspathInfo.create(getModel().getModelUnit().getBootPath() , ClassPath.EMPTY, getModel().getModelUnit().getSourcePath())); FileObject resource = classPath.findResource( path ); if ( resource != null ){ FileObject root = classPath.findOwnerRoot( resource ); if ( root == null ){ return false; if ( FileUtil.isArchiveFile( root )){ FileObject archiveFile = FileUtil.getArchiveFile(root); String ext = archiveFile.getExt(); if ( "war".equalsIgnoreCase( ext)){ // NOI18N return root.getFileObject("WEB-INF/beans.xml") == null; // NOI18N