Java Code Examples for com.sun.source.tree.CompilationUnitTree#getPackageName()
The following examples show how to use
com.sun.source.tree.CompilationUnitTree#getPackageName() .
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: TreeAnalyzer.java From meghanada-server with GNU General Public License v3.0 | 6 votes |
private static void analyzePackageName( CompilationUnitTree cut, Source src, EndPosTable endPosTable) { ExpressionTree packageExpr = cut.getPackageName(); if (isNull(packageExpr)) { src.setPackageName(""); } else { src.setPackageName(packageExpr.toString()); } if (packageExpr instanceof JCTree.JCIdent) { JCTree.JCIdent ident = (JCTree.JCIdent) packageExpr; int startPos = ident.getPreferredPosition(); int endPos = ident.getEndPosition(endPosTable); Range range = Range.create(src, startPos + 1, endPos); long pkgLine = range.begin.line; src.setPackageStartLine(pkgLine); addPackageIndex(src, pkgLine, 8, src.getPackageName()); } }
Example 2
Source File: IsSigMethodCriterion.java From annotation-tools with MIT License | 6 votes |
private static Context initImports(TreePath path) { CompilationUnitTree topLevel = path.getCompilationUnit(); Context result = contextCache.get(topLevel); if (result != null) { return result; } ExpressionTree packageTree = topLevel.getPackageName(); String packageName; if (packageTree == null) { packageName = ""; // the default package } else { packageName = packageTree.toString(); } List<String> imports = new ArrayList<>(); for (ImportTree i : topLevel.getImports()) { String imported = i.getQualifiedIdentifier().toString(); imports.add(imported); } result = new Context(packageName, imports); contextCache.put(topLevel, result); return result; }
Example 3
Source File: StringLiteralTemplateProcessor.java From manifold with Apache License 2.0 | 6 votes |
private boolean isTypeExcluded( JCTree.JCClassDecl classDef, CompilationUnitTree compilationUnit ) { if( _processorGates.isEmpty() ) { return false; } ExpressionTree pkgName = compilationUnit.getPackageName(); if( pkgName == null ) { return false; } String simpleName = classDef.name.toString(); String fqn = pkgName.toString() + '.' + simpleName; return _processorGates.stream().anyMatch( gate -> gate.exclude( fqn ) ); }
Example 4
Source File: TypeEnumerator.java From compile-testing with Apache License 2.0 | 6 votes |
@Override public Set<String> visitCompilationUnit(CompilationUnitTree reference, Void v) { Set<String> packageSet = reference.getPackageName() == null ? ImmutableSet.of("") : scan(reference.getPackageName(), v); if (packageSet.size() != 1) { throw new AssertionError("Internal error in NameFinder. Expected to find at most one " + "package identifier. Found " + packageSet); } final String packageName = packageSet.isEmpty() ? "" : packageSet.iterator().next(); Set<String> typeDeclSet = scan(reference.getTypeDecls(), v); return FluentIterable.from(typeDeclSet) .transform(new Function<String, String>() { @Override public String apply(String typeName) { return packageName.isEmpty() ? typeName : String.format("%s.%s", packageName, typeName); } }).toSet(); }
Example 5
Source File: ASTIndex.java From annotation-tools with MIT License | 5 votes |
public static Tree getNode(CompilationUnitTree cut, ASTRecord rec) { Map<Tree, ASTRecord> fwdIndex = ((ASTIndex) indexOf(cut)).back; Map<ASTRecord, Tree> revIndex = ((BiMap<Tree, ASTRecord>) fwdIndex).inverse(); ExpressionTree et = cut.getPackageName(); String pkg = et == null ? "" : et.toString(); if (!pkg.isEmpty() && rec.className.indexOf('.') < 0) { rec = new ASTRecord(cut, pkg + "." + rec.className, rec.methodName, rec.varName, rec.astPath); } return revIndex.get(rec); }
Example 6
Source File: DocLint.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 7
Source File: DocLint.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 8
Source File: DocLint.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 9
Source File: Imports.java From netbeans with Apache License 2.0 | 5 votes |
private static List<TreePathHandle> getAllImportsOfKind(CompilationInfo ci, ImportHintKind kind) { //allow only default and samepackage assert (kind == ImportHintKind.DEFAULT_PACKAGE || kind == ImportHintKind.SAME_PACKAGE); CompilationUnitTree cut = ci.getCompilationUnit(); TreePath topLevel = new TreePath(cut); List<TreePathHandle> result = new ArrayList<TreePathHandle>(3); List<? extends ImportTree> imports = cut.getImports(); for (ImportTree it : imports) { if (it.isStatic()) { continue; // XXX } if (it.getQualifiedIdentifier() instanceof MemberSelectTree) { MemberSelectTree ms = (MemberSelectTree) it.getQualifiedIdentifier(); if (kind == ImportHintKind.DEFAULT_PACKAGE) { if (ms.getExpression().toString().equals(DEFAULT_PACKAGE)) { result.add(TreePathHandle.create(new TreePath(topLevel, it), ci)); } } if (kind == ImportHintKind.SAME_PACKAGE) { ExpressionTree packageName = cut.getPackageName(); if (packageName != null && ms.getExpression().toString().equals(packageName.toString())) { result.add(TreePathHandle.create(new TreePath(topLevel, it), ci)); } } } } return result; }
Example 10
Source File: DocLint.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 11
Source File: CompileWorker.java From netbeans with Apache License 2.0 | 5 votes |
protected void computeFQNs(final Map<JavaFileObject, List<String>> file2FQNs, CompilationUnitTree cut, CompileTuple tuple) { String pack; if (cut.getPackageName() != null) { pack = cut.getPackageName().toString() + "."; //XXX } else { pack = ""; } String path = tuple.indexable.getRelativePath(); int i = path.lastIndexOf('.'); if (i >= 0) path = path.substring(0, i); path = FileObjects.convertFolder2Package(path); List<String> fqns = new LinkedList<String>(); boolean hasClassesLivingElsewhere = false; for (Tree t : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) { String fqn = pack + ((ClassTree) t).getSimpleName().toString(); fqns.add(fqn); if (!path.equals(fqn)) { hasClassesLivingElsewhere = true; } } } if (hasClassesLivingElsewhere) { file2FQNs.put(tuple.jfo, fqns); } }
Example 12
Source File: PackageReferenceCollector.java From deptective with Apache License 2.0 | 5 votes |
@Override public boolean onEnteringCompilationUnit(CompilationUnitTree tree) { ExpressionTree packageNameTree = tree.getPackageName(); // TODO deal with default package if (packageNameTree == null) { return false; } currentPackageName = packageNameTree.toString(); packagesOfCurrentCompilation.add(currentPackageName); try { currentComponent = declaredComponents.getComponentByPackage(currentPackageName); } catch (PackageAssignedToMultipleComponentsException e) { log.report( ReportingPolicy.ERROR, DeptectiveMessages.PACKAGE_CONTAINED_IN_MULTIPLE_COMPONENTS, String.join( ", ", e.getMatchingComponents() .stream() .map(Component::getName) .sorted() .collect(Collectors.toList()) ), currentPackageName ); createOutputFile = false; return false; } if (currentComponent == null) { builder.addContains(currentPackageName, PackagePattern.getPattern(currentPackageName)); } return true; }
Example 13
Source File: DocLint.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 14
Source File: DocLint.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void ignore) { if (tree.getPackageName() != null) { visitDecl(tree, null); } return super.visitCompilationUnit(tree, ignore); }
Example 15
Source File: PackageReferenceCollector.java From deptective with Apache License 2.0 | 5 votes |
@Override public boolean onEnteringCompilationUnit(CompilationUnitTree tree) { ExpressionTree packageNameTree = tree.getPackageName(); // TODO deal with default package if (packageNameTree == null) { return false; } currentPackageName = packageNameTree.toString(); packagesOfCurrentCompilation.add(currentPackageName); try { currentComponent = declaredComponents.getComponentByPackage(currentPackageName); } catch (PackageAssignedToMultipleComponentsException e) { log.report( ReportingPolicy.ERROR, DeptectiveMessages.PACKAGE_CONTAINED_IN_MULTIPLE_COMPONENTS, String.join( ", ", e.getMatchingComponents() .stream() .map(Component::getName) .sorted() .collect(Collectors.toList()) ), currentPackageName ); createOutputFile = false; return false; } if (currentComponent == null) { builder.addContains(currentPackageName, PackagePattern.getPattern(currentPackageName)); } return true; }
Example 16
Source File: Context.java From netbeans with Apache License 2.0 | 4 votes |
/**Returns name of package in which the current file is located. Default package * is represented by an empty string. * * @return the name of the enclosing package */ public @NonNull String enclosingPackage() { CompilationUnitTree cut = ctx.getInfo().getCompilationUnit(); return cut.getPackageName() != null ? cut.getPackageName().toString() : ""; }
Example 17
Source File: UseNbBundleMessages.java From netbeans with Apache License 2.0 | 4 votes |
@Override protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { WorkingCopy wc = ctx.getWorkingCopy(); TreePath treePath = ctx.getPath(); TreeMaker make = wc.getTreeMaker(); if (treePath.getLeaf().getKind() == Kind.METHOD_INVOCATION) { MethodInvocationTree mit = (MethodInvocationTree) treePath.getLeaf(); CompilationUnitTree cut = wc.getCompilationUnit(); boolean imported = false; String importBundleStar = cut.getPackageName() + ".Bundle.*"; for (ImportTree it : cut.getImports()) { if (it.isStatic() && it.getQualifiedIdentifier().toString().equals(importBundleStar)) { imported = true; break; } } if (!imported) { wc.rewrite(cut, make.addCompUnitImport(cut, make.Import(make.Identifier(importBundleStar), true))); } List<? extends ExpressionTree> args = mit.getArguments(); List<? extends ExpressionTree> params; if (args.size() == 3 && args.get(2).getKind() == Kind.NEW_ARRAY) { params = ((NewArrayTree) args.get(2)).getInitializers(); } else { params = args.subList(2, args.size()); } wc.rewrite(mit, make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.Identifier(toIdentifier(key)), params)); } // else annotation value, nothing to change if (!isAlreadyRegistered) { EditableProperties ep = new EditableProperties(true); InputStream is = ctx.getResourceContent(bundleProperties); try { ep.load(is); } finally { is.close(); } List<ExpressionTree> lines = new ArrayList<ExpressionTree>(); for (String comment : ep.getComment(key)) { lines.add(make.Literal(comment)); } lines.add(make.Literal(key + '=' + ep.remove(key))); TypeElement nbBundleMessages = wc.getElements().getTypeElement("org.openide.util.NbBundle.Messages"); if (nbBundleMessages == null) { throw new IllegalArgumentException("cannot resolve org.openide.util.NbBundle.Messages"); } GeneratorUtilities gu = GeneratorUtilities.get(wc); Tree enclosing = findEnclosingElement(wc, treePath); Tree modifiers; Tree nueModifiers; ExpressionTree[] linesA = lines.toArray(new ExpressionTree[lines.size()]); switch (enclosing.getKind()) { case METHOD: modifiers = wc.resolveRewriteTarget(((MethodTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); break; case VARIABLE: modifiers = wc.resolveRewriteTarget(((VariableTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); break; case COMPILATION_UNIT: modifiers = wc.resolveRewriteTarget(enclosing); nueModifiers = gu.appendToAnnotationValue((CompilationUnitTree) modifiers, nbBundleMessages, "value", linesA); break; default: modifiers = wc.resolveRewriteTarget(((ClassTree) enclosing).getModifiers()); nueModifiers = gu.appendToAnnotationValue((ModifiersTree) modifiers, nbBundleMessages, "value", linesA); } wc.rewrite(modifiers, nueModifiers); // XXX remove NbBundle import if now unused OutputStream os = ctx.getResourceOutput(bundleProperties); try { ep.store(os); } finally { os.close(); } } // XXX after JavaFix rewrite, Savable.save (on DataObject.find(src)) no longer works (JG13 again) }
Example 18
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 4 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree node, Void unused) { boolean first = true; if (node.getPackageName() != null) { markForPartialFormat(); visitPackage(node.getPackageName(), node.getPackageAnnotations()); builder.forcedBreak(); first = false; } if (!node.getImports().isEmpty()) { if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } for (ImportTree importDeclaration : node.getImports()) { markForPartialFormat(); builder.blankLineWanted(PRESERVE); scan(importDeclaration, null); builder.forcedBreak(); } first = false; } dropEmptyDeclarations(); for (Tree type : node.getTypeDecls()) { if (type.getKind() == Tree.Kind.IMPORT) { // javac treats extra semicolons in the import list as type declarations // TODO(cushon): remove this if https://bugs.openjdk.java.net/browse/JDK-8027682 is fixed continue; } if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } markForPartialFormat(); scan(type, null); builder.forcedBreak(); first = false; dropEmptyDeclarations(); } // set a partial format marker at EOF to make sure we can format the entire file markForPartialFormat(); return null; }
Example 19
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 4 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree node, Void unused) { boolean first = true; if (node.getPackageName() != null) { markForPartialFormat(); visitPackage(node.getPackageName(), node.getPackageAnnotations()); builder.forcedBreak(); first = false; } dropEmptyDeclarations(); if (!node.getImports().isEmpty()) { if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } for (ImportTree importDeclaration : node.getImports()) { markForPartialFormat(); builder.blankLineWanted(PRESERVE); scan(importDeclaration, null); builder.forcedBreak(); } first = false; } dropEmptyDeclarations(); for (Tree type : node.getTypeDecls()) { if (type.getKind() == Tree.Kind.IMPORT) { // javac treats extra semicolons in the import list as type declarations // TODO(cushon): remove this if https://bugs.openjdk.java.net/browse/JDK-8027682 is fixed continue; } if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } markForPartialFormat(); scan(type, null); builder.forcedBreak(); first = false; dropEmptyDeclarations(); } // set a partial format marker at EOF to make sure we can format the entire file markForPartialFormat(); return null; }
Example 20
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
@Override public Void visitCompilationUnit(CompilationUnitTree node, Void unused) { boolean first = true; if (node.getPackageName() != null) { markForPartialFormat(); visitPackage(node.getPackageName(), node.getPackageAnnotations()); builder.forcedBreak(); first = false; } if (!node.getImports().isEmpty()) { if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } for (ImportTree importDeclaration : node.getImports()) { markForPartialFormat(); builder.blankLineWanted(PRESERVE); scan(importDeclaration, null); builder.forcedBreak(); } first = false; } dropEmptyDeclarations(); for (Tree type : node.getTypeDecls()) { if (type.getKind() == Tree.Kind.IMPORT) { // javac treats extra semicolons in the import list as type declarations // TODO(cushon): remove this if https://bugs.openjdk.java.net/browse/JDK-8027682 is fixed continue; } if (!first) { builder.blankLineWanted(BlankLineWanted.YES); } markForPartialFormat(); scan(type, null); builder.forcedBreak(); first = false; dropEmptyDeclarations(); } // set a partial format marker at EOF to make sure we can format the entire file markForPartialFormat(); return null; }