/** * Extract the filename from a {@link CompilationUnitTree}, with special handling for jar files. * The return value is normalized to always use '/' to separate elements of the path and to always * have a leading '/'. */ @Nullable public static String getFileName(CompilationUnitTree tree) { return getFileNameFromUri(tree.getSourceFile().toUri()); }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getSourceFile() == null) { return NO_MATCH; } String name = tree.getSourceFile().getName(); int idx = name.lastIndexOf('/'); if (idx != -1) { name = name.substring(idx + 1); } if (!name.equals("package-info.java")) { return NO_MATCH; } if (tree.getTypeDecls().isEmpty()) { return NO_MATCH; } return describeMatch(tree.getTypeDecls().get(0)); } }
/** * Gets the current source file. * * @return the source file as a sequence of characters, or null if it is not available */ @Nullable public CharSequence getSourceCode() { try { return getPath().getCompilationUnit().getSourceFile().getCharContent(false); } catch (IOException e) { return null; } }
private Boolean reportError(String msg, Tree node) { SourcePositions srcPos = verifier.getSourcePositions(); CompilationUnitTree compUnit = verifier.getCompilationUnit(); if (compUnit != null) { long pos = srcPos.getStartPosition(compUnit, node); long line = compUnit.getLineMap().getLineNumber(pos); String name = compUnit.getSourceFile().getName(); msg = String.format("%s:%d:%s", name, line, Messages.get(msg)); verifier.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, getElement(node)); } else { verifier.getMessager().printMessage(Diagnostic.Kind.ERROR, msg); } return Boolean.FALSE; }
/** * Handles an exception thrown by an individual BugPattern. By default, wraps the exception in an * {@link ErrorProneError} and rethrows. May be overridden by subclasses, for example to log the * error and continue. */ @Override protected void handleError(Suppressible s, Throwable t) { if (t instanceof ErrorProneError) { throw (ErrorProneError) t; } if (t instanceof CompletionFailure) { throw (CompletionFailure) t; } TreePath path = getCurrentPath(); throw new ErrorProneError( s.canonicalName(), t, (DiagnosticPosition) path.getLeaf(), path.getCompilationUnit().getSourceFile()); }
@Override public String apply(CompilationUnitTree generated) { return String.format( "- %s in <%s>", actualTypes.get(generated), generated.getSourceFile().toUri().getPath()); } })
@Override public String apply(CompilationUnitTree generated) { return String.format( "- %s in <%s>", actualTypes.get(generated), generated.getSourceFile().toUri().getPath()); } })
/** * Gets the "Foo" portion of "Foo.java" that represents this compilation unit. */ public static String getPrimaryTypeName(CompilationUnitTree cu) { String name = cu.getSourceFile().getName(); int idx = name.lastIndexOf('/'); if(idx>=0) name=name.substring(idx+1); idx = name.lastIndexOf('\\'); if(idx>=0) name=name.substring(idx+1); if(name.endsWith(".java")) name=name.substring(0,name.length()-5); return name; } }
@Override public String apply(CompilationUnitTree generated) { return String.format("- %s in <%s>", actualTypes.get(generated), generated.getSourceFile().toUri().getPath()); } })
/** * Gets the relative path name inside the output directory where * the AST JavaScript should be written. */ public String getRelativePath() { ExpressionTree packageName = compUnit.getPackageName(); String pkg = packageName==null?"":packageName.toString().replace('.','/')+'/'; String name = new File(compUnit.getSourceFile().getName()).getName(); return pkg + name+".js"; }
@Override public Void visitCompilationUnit(CompilationUnitTree node, Trees trees) { URI uri = node.getSourceFile().toUri(); if (!"file".equalsIgnoreCase(uri.getScheme())) { return null; } return super.visitCompilationUnit(node, trees); }
public long findSourceTimestamp(DecoratedProcessingEnvironment env, Element apiElement) { SourcePosition sp = env.findSourcePosition(apiElement); URI uri = sp == null ? null : sp.getPath() == null ? null : sp.getPath().getCompilationUnit() == null ? null : sp.getPath().getCompilationUnit().getSourceFile() == null ? null : sp.getPath().getCompilationUnit().getSourceFile().toUri(); if (uri != null && "file".equalsIgnoreCase(uri.getScheme())) { //it's a file uri. return new File(uri.getPath()).lastModified(); } return 0; }
public FileObject getFileObject() { try { return URLMapper.findFileObject(getController().getCompilationUnit().getSourceFile().toUri().toURL()); } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); } return null; }
public long findSourceTimestamp(DecoratedProcessingEnvironment env, Element apiElement) { SourcePosition sp = env.findSourcePosition(apiElement); URI uri = sp == null ? null : sp.getPath() == null ? null : sp.getPath().getCompilationUnit() == null ? null : sp.getPath().getCompilationUnit().getSourceFile() == null ? null : sp.getPath().getCompilationUnit().getSourceFile().toUri(); if (uri != null && "file".equalsIgnoreCase(uri.getScheme())) { //it's a file uri. return new File(uri.getPath()).lastModified(); } return 0; }
/** * Gets the current source file. * * @return the source file as a sequence of characters, or null if it is not available */ public CharSequence getSourceCode() { try { return getPath().getCompilationUnit().getSourceFile().getCharContent(false); } catch (IOException e) { return null; } }
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void p) { log.useSource(tree.getSourceFile()); boolean proceed = packageReferenceHandler.onEnteringCompilationUnit(tree); return proceed ? super.visitCompilationUnit(tree, p) : null; }
private CompilationUnitTree modifyPackageAnnotation(CompilationUnitTree cut, int index, AnnotationTree annotation, Operation op) { CompilationUnitTree copy = CompilationUnit( c(cut.getPackageAnnotations(), index, annotation, op), cut.getPackageName(), cut.getImports(), cut.getTypeDecls(), cut.getSourceFile() ); return copy; }
private CompilationUnitTree modifyCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index, Tree typeDeclaration, Operation op) { CompilationUnitTree copy = CompilationUnit( compilationUnit.getPackageAnnotations(), compilationUnit.getPackageName(), compilationUnit.getImports(), c(compilationUnit.getTypeDecls(), index, typeDeclaration, op), compilationUnit.getSourceFile() ); return copy; }
private CompilationUnitTree modifyCompUnitImport(CompilationUnitTree compilationUnit, int index, ImportTree importt, Operation op) { CompilationUnitTree copy = CompilationUnit( compilationUnit.getPackageAnnotations(), compilationUnit.getPackageName(), c(compilationUnit.getImports(), index, importt, op), compilationUnit.getTypeDecls(), compilationUnit.getSourceFile() ); return copy; }
@Override public Tree visitCompilationUnit(CompilationUnitTree tree, Void p) { CompilationUnitTree n = make.CompilationUnit(tree.getPackage(), tree.getImports(), tree.getTypeDecls(), tree.getSourceFile()); model.setElement(n, model.getElement(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }