/** * Returns the file with name {@code fileName} in package {@code packageName} if one was * generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com.google.myapp", "Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile( Location location, String packageName, String fileName) { return generatedFile( location, packageName.isEmpty() ? fileName : packageName.replace('.', '/') + '/' + fileName); }
/** * Returns the file with name {@code fileName} in package {@code packageName} if one was * generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooClassFile = * compilation.generatedFile(CLASS_OUTPUT, "com.google.myapp", "Foo.class"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedFile( Location location, String packageName, String fileName) { return generatedFile( location, packageName.isEmpty() ? fileName : packageName.replace('.', '/') + '/' + fileName); }
/** * Returns the source file for the type with a given qualified name (no ".java" extension) if one * was generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooSourceFile = * compilation.generatedSourceFile("com.google.myapp.Foo"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedSourceFile(String qualifiedName) { int lastIndexOfDot = qualifiedName.lastIndexOf('.'); String packageName = lastIndexOfDot == -1 ? "" : qualifiedName.substring(0, lastIndexOfDot); String fileName = qualifiedName.substring(lastIndexOfDot + 1) + ".java"; return generatedFile(SOURCE_OUTPUT, packageName, fileName); }
/** * Returns the source file for the type with a given qualified name (no ".java" extension) if one * was generated. * * <p>For example: * * <pre> * {@code Optional<JavaFileObject>} fooSourceFile = * compilation.generatedSourceFile("com.google.myapp.Foo"); * </pre> * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public Optional<JavaFileObject> generatedSourceFile(String qualifiedName) { int lastIndexOfDot = qualifiedName.lastIndexOf('.'); String packageName = lastIndexOfDot == -1 ? "" : qualifiedName.substring(0, lastIndexOfDot); String fileName = qualifiedName.substring(lastIndexOfDot + 1) + ".java"; return generatedFile(SOURCE_OUTPUT, packageName, fileName); }
/** * Asserts that compilation generated a file named {@code fileName} in package {@code * packageName}. */ @CanIgnoreReturnValue public JavaFileObjectSubject generatedFile( Location location, String packageName, String fileName) { return checkGeneratedFile( actual().generatedFile(location, packageName, fileName), location, "named \"%s\" in %s", fileName, packageName.isEmpty() ? "the default package" : String.format("package \"%s\"", packageName)); }
/** * Asserts that compilation generated a file named {@code fileName} in package {@code * packageName}. */ @CanIgnoreReturnValue public JavaFileObjectSubject generatedFile( Location location, String packageName, String fileName) { return checkGeneratedFile( actual().generatedFile(location, packageName, fileName), location, "named \"%s\" in %s", fileName, packageName.isEmpty() ? "the default package" : String.format("package \"%s\"", packageName)); }
/** Asserts that compilation generated a file at {@code path}. */ @CanIgnoreReturnValue public JavaFileObjectSubject generatedFile(Location location, String path) { return checkGeneratedFile(actual().generatedFile(location, path), location, path); }
/** Asserts that compilation generated a file at {@code path}. */ @CanIgnoreReturnValue public JavaFileObjectSubject generatedFile(Location location, String path) { return checkGeneratedFile(actual().generatedFile(location, path), location, path); }
@Override protected Class<?> findClass(final String name) throws ClassNotFoundException { String path = name.replace(".", "/").concat(".class"); JavaFileObject generatedClass = compilation.generatedFile(CLASS_OUTPUT, path).orElse(null); if (generatedClass == null) { return super.findClass(name); } try (InputStream is = generatedClass.openInputStream()) { byte[] classBytes = ByteStreams.toByteArray(is); return defineClass(name, classBytes, 0, classBytes.length); } catch (IOException e) { throw new ClassNotFoundException(name, e); } }
@Override protected URL findResource(String name) { JavaFileObject generatedResource = compilation.generatedFile(CLASS_OUTPUT, name).orElse(null); if (generatedResource == null) { return super.findResource(name); } try { URI uri = generatedResource.toUri(); return new URL("compilation", "", -1, uri.getPath(), new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) { return new URLConnection(u) { @Override public void connect() { connected = true; } @Override public InputStream getInputStream() throws IOException { return generatedResource.openInputStream(); } }; } }); } catch (MalformedURLException e) { // Should never happen, since no validation is performed when stream handler is provided. throw new RuntimeException(e); } } }
@Test public void test() throws URISyntaxException, MalformedURLException { Compilation compilation = Compiler.javac() .withProcessors(new ActionProcessor()) .compile(JavaFileObjects.forSourceString( "test.AnnotatedClassTest", "package test;\n" + "\n" + "@io.syndesis.extension.api.annotations.Action(\n" + " id = \"action-id\",\n" + " name = \"action-name\",\n" + " description = \"action-description\"\n" + ")\n" + "public class AnnotatedClassTest {\n" + "}" ) ); assertTrue(compilation.generatedFile(StandardLocation.SOURCE_OUTPUT, "test/AnnotatedClassTest-action-id.json").isPresent()); } }
@Test public void test() throws URISyntaxException, MalformedURLException { Compilation compilation = Compiler.javac() .withProcessors(new SyndesisExtensionActionProcessor()) .compile(JavaFileObjects.forSourceString( "test.AnnotatedClassTest", "package test;\n" + "\n" + "@io.syndesis.extension.api.SyndesisExtensionAction(\n" + " id = \"action-id\",\n" + " name = \"action-name\",\n" + " description = \"action-description\"\n" + ")\n" + "public class AnnotatedClassTest {\n" + "}" ) ); assertTrue(compilation.generatedFile(StandardLocation.SOURCE_OUTPUT, "test/AnnotatedClassTest-action-id.properties").isPresent()); } }