Skip to content

Commit

Permalink
Fix text in FatalError.INCOMPATIBLE_ANNOTATION_FOUND_IN_COMPILE.
Browse files Browse the repository at this point in the history
Also emit the filename in a consistent manner with other errors, and add a test.

PiperOrigin-RevId: 579984675
  • Loading branch information
rluble authored and copybara-github committed Nov 7, 2023
1 parent a8d5e33 commit abaf9fd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
12 changes: 11 additions & 1 deletion transpiler/java/com/google/j2cl/common/Problems.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public enum FatalError {
PACKAGE_INFO_PARSE("Resource '%s' was found but it failed to parse.", 1),
CLASS_PATH_URL("Class path entry '%s' is not a valid url.", 1),
INCOMPATIBLE_ANNOTATION_FOUND_IN_COMPILE(
"@$s annotations found in $s "
"Unexpected @%s annotation found. "
+ "Please run this library through the incompatible annotated code stripper tool.",
1),
LIBRARY_INFO_OUTPUT_ARG_MISSING("-libraryinfooutput option is mandatory", 0),
Expand Down Expand Up @@ -100,6 +100,12 @@ public void fatal(FatalError fatalError, Object... args) {
abort();
}

public void fatal(int lineNumber, String filePath, FatalError fatalError, Object... args) {
checkArgument(fatalError.getNumberOfArguments() == args.length);
problem(Severity.ERROR, lineNumber, filePath, String.format(fatalError.getMessage(), args));
abort();
}

@FormatMethod
public void error(
SourcePosition sourcePosition, @FormatString String detailMessage, Object... args) {
Expand Down Expand Up @@ -152,6 +158,10 @@ private void problem(
@FormatString String detailMessage,
Object... args) {
String message = args.length == 0 ? detailMessage : String.format(detailMessage, args);
problem(severity, lineNumber, filePath, message);
}

private void problem(Severity severity, int lineNumber, String filePath, String message) {
problem(
severity,
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ private boolean hasErrors(
Set<String> filesWithGwtIncompatible =
AnnotatedNodeCollector.filesWithAnnotation(javacCompilationUnits, forbiddenAnnotation);
if (!filesWithGwtIncompatible.isEmpty()) {
// TODO(rluble): retrieve the line number where the annotation is found.
problems.fatal(
-1,
filesWithGwtIncompatible.iterator().next(),
FatalError.INCOMPATIBLE_ANNOTATION_FOUND_IN_COMPILE,
forbiddenAnnotation,
filesWithGwtIncompatible.iterator().next());
forbiddenAnnotation);
}
}
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticCollector.getDiagnostics()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ private boolean compilationHasErrors(
unit.accept(collector);
if (!collector.getNodes().isEmpty()) {
problems.fatal(
FatalError.INCOMPATIBLE_ANNOTATION_FOUND_IN_COMPILE, forbiddenAnnotation, filename);
unit.getLineNumber(collector.getNodes().get(0).getStartPosition()),
filename,
FatalError.INCOMPATIBLE_ANNOTATION_FOUND_IN_COMPILE,
forbiddenAnnotation);
}
}
for (IProblem problem : unit.getProblems()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,24 @@ public void testOutputsToZipFile() throws IOException {
assertNull(zipFile.getEntry("some/thing/Bogus.js"));
}
}

public void testForbiddenAnnotations() {
newTesterWithDefaults()
.addCompilationUnit(
"annotation.GwtIncompatible",
"import java.lang.annotation.*;",
"@Retention(RetentionPolicy.CLASS)",
"@Target({ElementType.METHOD})",
"@interface GwtIncompatible {}")
.addCompilationUnit(
"annotation.ClassWithForbiddenAnnotation",
"import jsinterop.annotations.*;",
"public class ClassWithForbiddenAnnotation {",
" @GwtIncompatible public void nativeInstanceMethod() {}",
"}")
.assertTranspileFails()
.assertErrorsWithoutSourcePosition(
"Unexpected @GwtIncompatible annotation found. Please run this library through the"
+ " incompatible annotated code stripper tool.");
}
}

0 comments on commit abaf9fd

Please sign in to comment.