/** * Source files generated during compilation. * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public ImmutableList<JavaFileObject> generatedSourceFiles() { return generatedFiles() .stream() .filter(generatedFile -> generatedFile.getKind().equals(JavaFileObject.Kind.SOURCE)) .collect(toImmutableList()); }
/** * Source files generated during compilation. * * @throws IllegalStateException for {@linkplain #status() failed compilations}, since the state * of the generated files is undefined in that case */ public ImmutableList<JavaFileObject> generatedSourceFiles() { return generatedFiles() .stream() .filter(generatedFile -> generatedFile.getKind().equals(JavaFileObject.Kind.SOURCE)) .collect(toImmutableList()); }
/** * Returns the file at {@code path} 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 path) { // We're relying on the implementation of location.getName() to be equivalent to the first // part of the path. String expectedFilename = String.format("%s/%s", location.getName(), path); return generatedFiles() .stream() .filter(generated -> generated.toUri().getPath().endsWith(expectedFilename)) .findFirst(); }
/** * Returns the file at {@code path} 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 path) { // We're relying on the implementation of location.getName() to be equivalent to the first // part of the path. String expectedFilename = String.format("%s/%s", location.getName(), path); return generatedFiles() .stream() .filter(generated -> generated.toUri().getPath().endsWith(expectedFilename)) .findFirst(); }
private JavaFileObjectSubject checkGeneratedFile( Optional<JavaFileObject> generatedFile, Location location, String format, Object... args) { if (!generatedFile.isPresent()) { StringBuilder builder = new StringBuilder("generated the file "); builder.append(args.length == 0 ? format : String.format(format, args)); builder.append("; it generated:\n"); for (JavaFileObject generated : actual().generatedFiles()) { if (generated.toUri().getPath().contains(location.getName())) { builder.append(" ").append(generated.toUri().getPath()).append('\n'); } } fail(builder.toString()); return ignoreCheck().about(javaFileObjects()).that(ALREADY_FAILED); } return check().about(javaFileObjects()).that(generatedFile.get()); }
private JavaFileObjectSubject checkGeneratedFile( Optional<JavaFileObject> generatedFile, Location location, String format, Object... args) { if (!generatedFile.isPresent()) { StringBuilder builder = new StringBuilder("generated the file "); builder.append(args.length == 0 ? format : String.format(format, args)); builder.append("; it generated:\n"); for (JavaFileObject generated : actual().generatedFiles()) { if (generated.toUri().getPath().contains(location.getName())) { builder.append(" ").append(generated.toUri().getPath()).append('\n'); } } fail(builder.toString()); return ignoreCheck().about(javaFileObjects()).that(ALREADY_FAILED); } return check().about(javaFileObjects()).that(generatedFile.get()); }
boolean wasGenerated(JavaFileObject expected) { ByteSource expectedByteSource = JavaFileObjects.asByteSource(expected); for (JavaFileObject generated : compilation.generatedFiles()) { try { if (generated.getKind().equals(expected.getKind()) && expectedByteSource.contentEquals(JavaFileObjects.asByteSource(generated))) { return true; } } catch (IOException e) { throw new RuntimeException(e); } } return false; }
boolean wasGenerated(JavaFileObject expected) { ByteSource expectedByteSource = JavaFileObjects.asByteSource(expected); for (JavaFileObject generated : compilation.generatedFiles()) { try { if (generated.getKind().equals(expected.getKind()) && expectedByteSource.contentEquals(JavaFileObjects.asByteSource(generated))) { return true; } } catch (IOException e) { throw new RuntimeException(e); } } return false; }
@Test(expected = IllegalStateException.class) public void missing_default_value_for_object() { final ImmutableList<JavaFileObject> telemetryWrapper = Compiler.javac() .withProcessors(new TelemetryAnnotationProcessor()) .compile( JavaFileObjects.forSourceLines( "TelemetryWrapper", "package com.bumptech.glide.test;", "import org.mozilla.telemetry.annotation.TelemetryDoc;\n", "import org.mozilla.telemetry.annotation.TelemetryExtra;\n", "class TelemetryWrapper {" + "@TelemetryDoc(\n" + " name = \"n\",\n" + " category = \"a\",\n" + " method = \"m\",\n" + // " object = \"o\",\n" + " value = \"v\",\n" + " extras = {@TelemetryExtra(name = \"a\", value = \"v\")})", " public void send(){" + "}", "}" )).generatedFiles(); assert (telemetryWrapper.size() == 1); assert (new File(TelemetryAnnotationProcessor.FILE_README).exists()); }
@Test public void should_generate_one_document() { final ImmutableList<JavaFileObject> telemetryWrapper = Compiler.javac() .withProcessors(new TelemetryAnnotationProcessor()) .compile( JavaFileObjects.forSourceLines( "TelemetryWrapper", "package com.bumptech.glide.test;", "import org.mozilla.telemetry.annotation.TelemetryDoc;\n", "import org.mozilla.telemetry.annotation.TelemetryExtra;\n", "class TelemetryWrapper {" + "@TelemetryDoc(\n" + " name = \"n\",\n" + " category = \"a\",\n" + " method = \"m\",\n" + " object = \"o\",\n" + " value = \"v\",\n" + " extras = {@TelemetryExtra(name = \"a\", value = \"v\")})", " public void send(){" + "}", "}" )).generatedFiles(); assert (telemetryWrapper.size() == 1); }