@Override public void register(JavaFileObject fileObject) { this.javaFileObjects.put(fileObject.toUri(), fileObject); }
private DescriptionBasedDiff( JCCompilationUnit compilationUnit, boolean ignoreOverlappingFixes, ImportOrganizer importOrganizer) { this.compilationUnit = checkNotNull(compilationUnit); URI sourceFileUri = compilationUnit.getSourceFile().toUri(); this.sourcePath = (sourceFileUri.isAbsolute() && Objects.equals(sourceFileUri.getScheme(), "file")) ? Paths.get(sourceFileUri).toAbsolutePath().toString() : sourceFileUri.getPath(); this.ignoreOverlappingFixes = ignoreOverlappingFixes; this.importsToAdd = new LinkedHashSet<>(); this.importsToRemove = new LinkedHashSet<>(); this.endPositions = compilationUnit.endPositions; this.importOrganizer = importOrganizer; }
/** * 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()); }
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Creating " + fileObject.toUri()); Writer writer = fileObject.openWriter(); try {
public static SourceFile create(JavaFileObject fileObject) throws IOException { return new SourceFile(fileObject.toUri().getPath(), fileObject.getCharContent(false)); }
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); }
@Override public boolean matchesSafely( Diagnostic<? extends JavaFileObject> item, Description mismatchDescription) { if (item.getSource() == null) { mismatchDescription .appendText("diagnostic not attached to a file: ") .appendValue(item.getMessage(ENGLISH)); return false; } if (!item.getSource().toUri().equals(fileURI)) { mismatchDescription.appendText("diagnostic not in file ").appendValue(fileURI); return false; } if (item.getLineNumber() != line) { mismatchDescription .appendText("diagnostic not on line ") .appendValue(item.getLineNumber()); return false; } return true; }
@Override public boolean matchesSafely( Diagnostic<? extends JavaFileObject> item, Description mismatchDescription) { if (item.getSource() == null) { mismatchDescription .appendText("diagnostic not attached to a file: ") .appendValue(item.getMessage(ENGLISH)); return false; } if (!item.getSource().toUri().equals(fileURI)) { mismatchDescription.appendText("diagnostic not in file ").appendValue(fileURI); return false; } if (item.getLineNumber() != line) { mismatchDescription .appendText("diagnostic not on line ") .appendValue(item.getLineNumber()); return false; } if (!matcher.apply(item.getMessage(Locale.getDefault()))) { mismatchDescription.appendText("diagnostic does not match ").appendValue(matcher); return false; } return true; }
@Override public DescriptionListener getDescriptionListener(Log log, JCCompilationUnit compilation) { URI sourceFile = compilation.getSourceFile().toUri(); DelegatingDescriptionListener delegate = new DelegatingDescriptionListener( descriptionsFactory.getDescriptionListener(log, compilation), DescriptionBasedDiff.createIgnoringOverlaps(compilation, importOrganizer)); foundSources.put(sourceFile, delegate); return delegate; }
@Override public void finished(TaskEvent event) { if (event.getKind() != Kind.GENERATE) { return; } RefactoringResult refactoringResult; try { refactoringResult = refactoringCollection.applyChanges(event.getSourceFile().toUri()); } catch (Exception e) { PrintWriter out = Log.instance(context).getWriter(WriterKind.ERROR); out.println(e.getMessage()); out.flush(); return; } if (refactoringResult.type() == RefactoringCollection.RefactoringResultType.CHANGED) { PrintWriter out = Log.instance(context).getWriter(WriterKind.NOTICE); out.println(refactoringResult.message()); out.flush(); } } }
for (Predicate<? super String> predicate : predicates) { Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> patternMatcher = hasItem(diagnosticOnLine(source.toUri(), lineNumber, predicate)); assertWithMessage( "Did not see an error on line %s matching %s. %s", hasItem( diagnosticOnLine( source.toUri(), lineNumber, new SimpleStringContains("[" + checkName + "]"))); assertWithMessage( "Did not see an error on line %s containing [%s]. %s", int lineNumber = reader.getLineNumber(); Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticOnLine(source.toUri(), lineNumber)); if (matcher.matches(diagnostics)) { fail("Saw unexpected error on line " + lineNumber + ". " + allErrors(diagnostics));
if (!argAnnotCache.containsKey(className)) { URLConnection uc = klass.classfile.toUri().toURL().openConnection(); if (!(uc instanceof JarURLConnection)) return false; JarURLConnection juc = (JarURLConnection) uc;
for (int i = 0; i < fileObjects.size(); i++) { final JavaFileObject oldFile = fileObjects.get(i); if (modifiedFile.toUri().equals(oldFile.toUri())) { DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER); fileObjects.set( i, new SimpleJavaFileObject(sourceURI(modifiedFile.toUri()), Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
@Override public String inferBinaryName(Location location, JavaFileObject file) { if (file.toUri().getScheme().equals("mem")) { return file.getName(); } else { return super.inferBinaryName(location, file); } }
@Test public void test1_UniqueNames() { JavaFileObject source1 = new TestBuilder().build().selectName(seenNames); assertThat(source1.toUri().toString()).endsWith( "/org/inferred/freebuilder/processor/source/testing/generatedcode/TestBuilderTest.java"); JavaFileObject source2 = new TestBuilder().build().selectName(seenNames); assertThat(source2.toUri().toString()).endsWith("TestBuilderTest__2.java"); assertEquals(2, seenNames.size()); }
@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()); } })
protected File getServerSideDestFile(File sourceDir, JavaFileObject sourceFile, TypeElement declaration) { File destDir = sourceDir; String packageName = this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration).getQualifiedName().toString(); for (StringTokenizer packagePaths = new StringTokenizer(packageName, "."); packagePaths.hasMoreTokens();) { String packagePath = packagePaths.nextToken(); destDir = new File(destDir, packagePath); } destDir.mkdirs(); String simpleFilename = sourceFile.toUri().toString(); simpleFilename = simpleFilename.substring(simpleFilename.lastIndexOf('/')); return new File(destDir, simpleFilename); }
@Override public JavaFileObject createClassFile(CharSequence name, Element... originatingElements) throws IOException { JavaFileObject jfo = _fileManager.getJavaFileForOutput( StandardLocation.CLASS_OUTPUT, name.toString(), JavaFileObject.Kind.CLASS, null); URI uri = jfo.toUri(); if (_createdFiles.contains(uri)) { throw new FilerException("Class file already created : " + name); //$NON-NLS-1$ } _createdFiles.add(uri); return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this); }
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; }