protected int diffImport(JCImport oldT, JCImport newT, int[] bounds) { int localPointer = bounds[0]; int[] qualBounds = getBounds(oldT.getQualifiedIdentifier()); if (oldT.staticImport == newT.staticImport) { copyTo(localPointer, qualBounds[0]); } else { if (oldT.staticImport) { //removing "static": moveFwdToToken(tokenSequence, localPointer, JavaTokenId.STATIC); copyTo(localPointer, tokenSequence.offset()); } else { copyTo(localPointer, qualBounds[0]); printer.print("static "); } } localPointer = diffTree(oldT.getQualifiedIdentifier(), newT.getQualifiedIdentifier(), qualBounds); copyTo(localPointer, bounds[1]); return bounds[1]; }
public static void deleteImportFromCompilationUnit(JavacNode node, String name) { if (inNetbeansEditor(node)) return; if (!node.shouldDeleteLombokAnnotations()) return; ListBuffer<JCTree> newDefs = new ListBuffer<JCTree>(); JCCompilationUnit unit = (JCCompilationUnit) node.top().get(); for (JCTree def : unit.defs) { boolean delete = false; if (def instanceof JCImport) { JCImport imp0rt = (JCImport)def; delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name)); } if (!delete) newDefs.append(def); } unit.defs = newDefs.toList(); }
/** * A helper method to create a JCImport stub. * * @param typeName the fully-qualified name of the type being imported * @param isStatic whether the import is static * @param startPos the start position of the import statement * @param endPos the end position of the import statement * @return a new JCImport stub */ private static JCImport stubImport(String typeName, boolean isStatic, int startPos, int endPos) { JCImport result = mock(JCImport.class); when(result.isStatic()).thenReturn(isStatic); when(result.getStartPosition()).thenReturn(startPos); when(result.getEndPosition(anyMapOf(JCTree.class, Integer.class))).thenReturn(endPos); // craft import string StringBuilder returnSB = new StringBuilder("import "); if (isStatic) { returnSB.append("static "); } returnSB.append(typeName); returnSB.append(";\n"); when(result.toString()).thenReturn(returnSB.toString()); return result; }
@Override public void visitTopLevel(JCCompilationUnit node) { CompilationUnit unit = new CompilationUnit(); if (node.pid != null) { PackageDeclaration pkg = new PackageDeclaration(); fillWithIdentifiers(node.pid, pkg.astParts()); unit.astPackageDeclaration(setPos(node.pid, pkg)); fillList(node.packageAnnotations, pkg.rawAnnotations()); } for (JCTree def : node.defs) { if (def instanceof JCImport) { unit.rawImportDeclarations().addToEnd(toTree(def)); } else { unit.rawTypeDeclarations().addToEnd(toTree(def, FlagKey.SKIP_IS_DECL)); } } setConversionStructureInfo(unit, "converted"); set(node, unit); }
public static void deleteImportFromCompilationUnit(JavacNode node, String name) { if (inNetbeansEditor(node)) return; if (!node.shouldDeleteLombokAnnotations()) return; ListBuffer<JCTree> newDefs = ListBuffer.lb(); JCCompilationUnit unit = (JCCompilationUnit) node.top().get(); for (JCTree def : unit.defs) { boolean delete = false; if (def instanceof JCImport) { JCImport imp0rt = (JCImport)def; delete = (!imp0rt.staticImport && imp0rt.qualid.toString().equals(name)); } if (!delete) newDefs.append(def); } unit.defs = newDefs.toList(); }
@Override public boolean hasStarImport(String packageName) { if (pkg != null && pkg.toString().equals(packageName)) return true; if ("java.lang".equals(packageName)) return true; for (JCTree def : defs) { if (!(def instanceof JCImport)) continue; if (((JCImport) def).staticImport) continue; JCTree qual = ((JCImport) def).qualid; if (!(qual instanceof JCFieldAccess)) continue; String simpleName = ((JCFieldAccess) qual).name.toString(); if (!"*".equals(simpleName)) continue; if (packageName.equals(((JCFieldAccess) qual).selected.toString())) return true; } return false; }
public void importAll(Types types, Scope origin, ImportFilter filter, JCImport imp, BiConsumer<JCImport, CompletionFailure> cfHandler) { for (Scope existing : subScopes) { Assert.check(existing instanceof FilterImportScope); FilterImportScope fis = (FilterImportScope) existing; if (fis.origin == origin && fis.filter == filter && fis.imp.staticImport == imp.staticImport) return ; //avoid entering the same scope twice } prependSubScope(new FilterImportScope(types, origin, null, filter, imp, cfHandler)); }
public FilterImportScope(Types types, Scope origin, Name filterName, ImportFilter filter, JCImport imp, BiConsumer<JCImport, CompletionFailure> cfHandler) { super(origin.owner); this.types = types; this.origin = origin; this.filterName = filterName; this.filter = filter; this.imp = imp; this.cfHandler = cfHandler; }
@Override public String getFullyQualifiedNameForSimpleName(String unqualified) { for (JCTree def : defs) { if (!(def instanceof JCImport)) continue; JCTree qual = ((JCImport) def).qualid; if (!(qual instanceof JCFieldAccess)) continue; String simpleName = ((JCFieldAccess) qual).name.toString(); if (simpleName.equals(unqualified)) { return LombokInternalAliasing.processAliases(qual.toString()); } } return null; }
public void visitImport(JCImport that) { try { print("JCImport:"); } catch (Exception e) { } super.visitImport(that); }
/** Print unit consisting of package clause and import statements in toplevel, * followed by class definition. if class definition == null, * print all definitions in toplevel. * @param tree The toplevel tree * @param cdef The class definition, which is assumed to be part of the * toplevel tree. */ public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException { Object dc = getDocComments(tree); loadDocCommentsTable(dc); printDocComment(tree); if (tree.pid != null) { consumeComments(tree.pos, tree); print("package "); printExpr(tree.pid); print(";"); println(); } boolean firstImport = true; for (List<JCTree> l = tree.defs; l.nonEmpty() && (cdef == null || IMPORT.equals(treeTag(l.head))); l = l.tail) { if (IMPORT.equals(treeTag(l.head))) { JCImport imp = (JCImport)l.head; Name name = TreeInfo.name(imp.qualid); if (name == name.table.fromChars(new char[] {'*'}, 0, 1) || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); } printStat(imp); } } else { printStat(l.head); } } if (cdef != null) { printStat(cdef); println(); } }
public void visitImport(JCImport tree) { try { print("import "); if (tree.staticImport) print("static "); printExpr(tree.qualid); print(";"); println(); } catch (IOException e) { throw new UncheckedIOException(e); } }
@SafeVarargs private static List<JCImport> mergeImports(List<JCImport> mergedImports, List<JCImport>... importLists) { for (List<JCImport> imports : importLists) { for (JCImport imp : imports) { mergedImports = appendImport(mergedImports, imp); } } return mergedImports; }
private static List<JCImport> appendImport(List<JCImport> imports, JCImport addImport) { for (JCImport imp : imports) { if (imp.toString().equals(addImport.toString())) { return imports; } } return imports.append(addImport); }
public void addNewImports(String... imports) { TreeMaker maker = environment.getMaker(); List<JCImport> newImports = List.nil(); for (String newImport : imports) { newImports = newImports.append(maker.Import(environment.createParser(newImport).parseType(), false)); } addNewImports(newImports); }
public ImportStatements(JCExpression packageTree, List<JCImport> importTrees, ErrorProneEndPosMap endPosMap) { // find start, end positions for current list of imports (for replacement) if (importTrees.isEmpty()) { // start/end positions are just after the package expression hasExistingImports = false; startPos = endPosMap.getEndPosition(packageTree) + 2; // +2 for semicolon and newline endPos = startPos; } else { // process list of imports and find start/end positions hasExistingImports = true; for (JCImport importTree : importTrees) { int currStartPos = importTree.getStartPosition(); int currEndPos = endPosMap.getEndPosition(importTree); startPos = Math.min(startPos, currStartPos); endPos = Math.max(endPos, currEndPos); } } // sanity check for start/end positions Preconditions.checkState(startPos <= endPos); // convert list of JCImports to list of strings importStrings = new TreeSet<>(IMPORT_ORDERING); importStrings.addAll(Lists.transform(importTrees, new Function<JCImport, String>() { @Override public String apply(JCImport input) { String importExpr = input.toString(); return importExpr.substring(0, importExpr.length() - 2); // snip trailing ";\n" } })); }
/** * Test empty initial import list. Positions should match package end * positions. */ @Test public void emptyImportListShouldGivePositionOfPackageStmt() { ImportStatements imports = new ImportStatements( basePackage, new ArrayList<JCImport>(), FAKE_END_POS_MAP); assertEquals(81, imports.getStartPos()); assertEquals(81, imports.getEndPos()); }
/** * Test empty initial import list. The output string should start and * end with newlines because it is intended to be inserted after the * package statement. */ @Test public void addingToEmptyImportListOutputShouldStartAndEndWithNewlines() { ImportStatements imports = new ImportStatements( basePackage, new ArrayList<JCImport>(), FAKE_END_POS_MAP); imports.add("import org.joda.time.Interval"); assertEquals("\n" + "import org.joda.time.Interval;\n", imports.toString()); }
@Override public void visitImport(JCImport node) { ImportDeclaration imp = new ImportDeclaration(); fillWithIdentifiers(node.getQualifiedIdentifier(), imp.astParts()); Identifier last = imp.astParts().last(); if (last != null && "*".equals(last.astValue())) { imp.astParts().remove(last); imp.astStarImport(true); setConversionPositionInfo(imp, ".*", last.getPosition()); } imp.astStaticImport(node.isStatic()); set(node, imp); }
/** Test empty initial import list. Positions should match package end positions. */ @Test public void emptyImportListShouldGivePositionOfPackageStmt() { ImportStatements imports = createImportStatements(basePackage, new ArrayList<JCImport>()); assertEquals(81, imports.getStartPos()); assertEquals(81, imports.getEndPos()); }
/** * Test empty initial import list. The output string should start and end with newlines because it * is intended to be inserted after the package statement. */ @Test public void addingToEmptyImportListOutputShouldStartAndEndWithNewlines() { ImportStatements imports = createImportStatements(basePackage, new ArrayList<JCImport>()); imports.add("import org.joda.time.Interval"); assertEquals("\n" + "import org.joda.time.Interval;\n", imports.toString()); }
/** Print unit consisting of package clause and import statements in toplevel, * followed by class definition. if class definition == null, * print all definitions in toplevel. * @param tree The toplevel tree * @param cdef The class definition, which is assumed to be part of the * toplevel tree. */ public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException { docComments = tree.docComments; printDocComment(tree); if (tree.pid != null) { print("package "); printExpr(tree.pid); print(";"); println(); } boolean firstImport = true; for (List<JCTree> l = tree.defs; l.nonEmpty() && (cdef == null || l.head.getTag() == JCTree.IMPORT); l = l.tail) { if (l.head.getTag() == JCTree.IMPORT) { JCImport imp = (JCImport)l.head; Name name = TreeInfo.name(imp.qualid); if (name == name.table.names.asterisk || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); } printStat(imp); } } else { printStat(l.head); } } if (cdef != null) { printStat(cdef); println(); } }
public void visitImport(JCImport tree) { JCTree imp = tree.qualid; Name name = TreeInfo.name(imp); TypeSymbol p; // Create a local environment pointing to this tree to disable // effects of other imports in Resolve.findGlobalType Env<AttrContext> localEnv = env.dup(tree); // Attribute qualifying package or class. JCFieldAccess s = (JCFieldAccess) imp; p = attr. attribTree(s.selected, localEnv, tree.staticImport ? TYP : (TYP | PCK), Type.noType).tsym; if (name == names.asterisk) { // Import on demand. chk.checkCanonical(s.selected); if (tree.staticImport) importStaticAll(tree.pos, p, env); else importAll(tree.pos, p, env); } else { // Named type import. if (tree.staticImport) { importNamedStatic(tree.pos(), p, name, localEnv); chk.checkCanonical(s.selected); } else { TypeSymbol c = attribImportType(imp, localEnv).tsym; chk.checkCanonical(imp); importNamed(tree.pos(), c, env); } } }
/** Print unit consisting of package clause and import statements in toplevel, * followed by class definition. if class definition == null, * print all definitions in toplevel. * @param tree The toplevel tree * @param cdef The class definition, which is assumed to be part of the * toplevel tree. */ public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException { docComments = tree.docComments; printDocComment(tree); if (tree.pid != null) { consumeComments(tree.pos); print("package "); printExpr(tree.pid); print(";"); println(); } boolean firstImport = true; for (List<JCTree> l = tree.defs; l.nonEmpty() && (cdef == null || getTag(l.head) == IMPORT); l = l.tail) { if (getTag(l.head) == IMPORT) { JCImport imp = (JCImport)l.head; Name name = TreeInfo.name(imp.qualid); if (name == name.table.fromChars(new char[] {'*'}, 0, 1) || cdef == null || isUsed(TreeInfo.symbol(imp.qualid), cdef)) { if (firstImport) { firstImport = false; println(); } printStat(imp); } } else { printStat(l.head); } } if (cdef != null) { printStat(cdef); println(); } }
@Override public String getFullyQualifiedNameForSimpleName(String unqualified) { for (JCTree def : defs) { if (!(def instanceof JCImport)) continue; JCTree qual = ((JCImport) def).qualid; if (!(qual instanceof JCFieldAccess)) continue; String simpleName = ((JCFieldAccess) qual).name.toString(); if (simpleName.equals(unqualified)) return qual.toString(); } return null; }
@Override public boolean contains(String name) { for (JCTree def : defs) { if (!(def instanceof JCImport)) continue; JCTree qual = ((JCImport) def).qualid; if (!(qual instanceof JCFieldAccess)) continue; JCFieldAccess field = (JCFieldAccess) qual; if (name.equals(field.selected + "." + field.name)) return true; } return false; }
private boolean matchImport(JCImport t1, JCImport t2) { return t1.staticImport == t2.staticImport && treesMatch(t1.qualid, t2.qualid); }
@Override public void visitImport(JCImport tree) { cancelService.abortIfCanceled(); super.visitImport(tree); }
@Override public void visitImport(JCImport tree) { }