@Override public CharSequence extract(ProcessingEnvironment environment, TypeElement typeElement) throws IOException { if (typeElement instanceof ClassSymbol) { ClassSymbol classSymbol = (ClassSymbol) typeElement; if (classSymbol.sourcefile != null) { return classSymbol.sourcefile.getCharContent(true); } } return UNABLE_TO_EXTRACT; }
@Override void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource) throws IOException { assertThat(maybeFormat(refactoredSource.getCharContent(false).toString())) .isEqualTo(maybeFormat(expectedSource.getCharContent(false).toString())); }
public String getRange(JCCompilationUnit unit) { try { CharSequence sequence = unit.getSourceFile().getCharContent(true); return sequence .subSequence(location.getStartPosition(), location.getEndPosition(unit.endPositions)) .toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public String getRange(JCCompilationUnit unit) { try { CharSequence sequence = unit.getSourceFile().getCharContent(true); JCTree firstStatement = statements.get(0); JCTree lastStatement = Iterables.getLast(statements); return sequence .subSequence( firstStatement.getStartPosition(), lastStatement.getEndPosition(unit.endPositions)) .toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
public static SourceFile create(JavaFileObject fileObject) throws IOException { return new SourceFile(fileObject.toUri().getPath(), fileObject.getCharContent(false)); }
private JavacErrorDescriptionListener( Log log, EndPosTable endPositions, JavaFileObject sourceFile, Context context, boolean dontUseErrors) { this.log = log; this.sourceFile = sourceFile; this.context = context; this.dontUseErrors = dontUseErrors; checkNotNull(endPositions); try { CharSequence sourceFileContent = sourceFile.getCharContent(true); fixToAppliedFix = fix -> AppliedFix.fromSource(sourceFileContent, endPositions).apply(fix); } catch (IOException e) { throw new UncheckedIOException(e); } }
private static void assertCompilationDiagnostingOn(Kind expectedDiagnosticKind, File expectedErrorClassFile, String expectedContentInError, CompileResult result) throws IOException { String expectedErrorPath; boolean fileNameOnly = expectedErrorClassFile.getPath().split(Pattern.quote(File.separator)).length == 1; if (fileNameOnly) { // this is just the filename expectedErrorPath = expectedErrorClassFile.getPath(); } else { expectedErrorPath = expectedErrorClassFile.toURI().toString(); } for (Diagnostic<? extends JavaFileObject> diagnostic : result.diagnostics) { if (diagnostic.getKind() == expectedDiagnosticKind) { JavaFileObject source = diagnostic.getSource(); if (source != null) { if (expectedErrorPath.endsWith(source.toUri().toString()) || fileNameOnly && source.toUri().toString().endsWith(expectedErrorPath)) { CharSequence sourceContent = source.getCharContent(true); if (diagnostic.getPosition() != Diagnostic.NOPOS) { CharSequence contentInError = sourceContent.subSequence((int) diagnostic.getStartPosition(), (int) diagnostic.getEndPosition()); if (contentInError.toString().contains(expectedContentInError)) { return; } } } } } } fail("Expected a compilation " + expectedDiagnosticKind + " in " + expectedErrorClassFile.toString() + " on " + expectedContentInError + ", diagnostics: " + result.diagnostics); }
/** * 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; } }
try CharSequence content = source.getCharContent( true ); result.append( String.format( "%4d: ", 1 ) ); for ( int line = 1, i = 0; i < content.length(); i++ )
protected static Pretty pretty(Context context, final Writer writer) { final JCCompilationUnit unit = context.get(JCCompilationUnit.class); try { final String unitContents = unit.getSourceFile().getCharContent(false).toString(); return new Pretty(writer, true) {
final String content = source.getCharContent(true).toString(); final int begin = content.lastIndexOf('\n', (int)diagnostic.getStartPosition()); final int end = content.indexOf('\n', (int)diagnostic.getEndPosition());
final List<Diagnostic<? extends JavaFileObject>> diagnostics = getDiagnostics(); final LineNumberReader reader = new LineNumberReader(CharSource.wrap(source.getCharContent(false)).openStream()); do { String line = reader.readLine();
new SourceFile( modifiedFile.getName(), modifiedFile.getCharContent(false /*ignoreEncodingErrors*/)); } catch (IOException e) { return false;
/** * Get source file for javaFileObject * * @param javaFileObject * @return * @throws IOException */ public static String toString(JavaFileObject javaFileObject) throws IOException { return String.valueOf(javaFileObject.getCharContent(true)); }
@Override public char[] getContents() { try { return javaFileObject.getCharContent(true).toString().toCharArray(); } catch(IOException e) { e.printStackTrace(); throw new AbortCompilationUnit(null, e, null); } } };
protected static char[] getCharContent(JavaFileObject fileObject) throws IOException { CharSequence cs = fileObject.getCharContent(true); if (cs instanceof CharBuffer) { return JavacFileManager.toArray((CharBuffer)cs); } else { return cs.toString().toCharArray(); } }
public String getRange(JCCompilationUnit unit) { try { CharSequence sequence = unit.getSourceFile().getCharContent(true); return sequence .subSequence(location.getStartPosition(), location.getEndPosition(unit.endPositions)) .toString(); } catch (IOException e) { throw new RuntimeException(e); } } }
protected void copyServerSideType(File sourceDir, TypeElement type) throws IOException { SourcePosition source = this.context.getProcessingEnvironment().findSourcePosition(type); JavaFileObject sourceFile = source.getSourceFile(); File destFile = getServerSideDestFile(sourceDir, sourceFile, type); FileWriter writer = new FileWriter(destFile); debug("Writing server-side java type to %s.", destFile); writer.write(sourceFile.getCharContent(false).toString()); writer.flush(); writer.close(); }
protected void copyServerSideType(File sourceDir, TypeElement type) throws IOException { SourcePosition source = this.context.getProcessingEnvironment().findSourcePosition(type); JavaFileObject sourceFile = source.getSourceFile(); File destFile = getServerSideDestFile(sourceDir, sourceFile, type); FileWriter writer = new FileWriter(destFile); debug("Writing server-side java type to %s.", destFile); writer.write(sourceFile.getCharContent(false).toString()); writer.flush(); writer.close(); }
protected void copyServerSideType(File sourceDir, TypeElement type) throws IOException { SourcePosition source = this.context.getProcessingEnvironment().findSourcePosition(type); JavaFileObject sourceFile = source.getSourceFile(); File destFile = getServerSideDestFile(sourceDir, sourceFile, type); FileWriter writer = new FileWriter(destFile); debug("Writing server-side java type to %s.", destFile); writer.write(sourceFile.getCharContent(false).toString()); writer.flush(); writer.close(); }