Skip to content

Commit

Permalink
Get annotation processing going with Eclipse Compiler for Java (#8913)
Browse files Browse the repository at this point in the history
* Be fault-tolerant when searching for a module

* Handle missing enclosing element of `VariableElement`

* Be fault-tolerant when deleting a `FileTextResource`

* Print stack trace as string if the processing of config metadata fails
  • Loading branch information
arouel authored Oct 22, 2024
1 parent d2fbe8b commit 1ffbb32
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static Optional<ModuleInfo> findModule(Filer filer) {
try (InputStream in = resource.openInputStream()) {
return Optional.of(ModuleInfoSourceParser.parse(in));
}
} catch (IOException ignored) {
} catch (Exception ignored) {
// it is not in sources, let's see if it got generated
}
// generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ public static Optional<TypedElementInfo> createTypedElementInfoFromElement(AptCo
.throwsChecked(thrownChecked)
.parameterArguments(params)
.originatingElement(v);
AptTypeFactory.createTypeName(v.getEnclosingElement()).ifPresent(builder::enclosingType);

// To be failure-tolerant, as the ECJ may not provide an enclosing element for a VariableElement.
Element enclosingElement = v.getEnclosingElement();
if (enclosingElement != null) {
AptTypeFactory.createTypeName(enclosingElement).ifPresent(builder::enclosingType);
}

Optional.ofNullable(defaultValue).ifPresent(builder::defaultValue);

return mapElement(ctx, builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public void lines(List<String> newLines) {
public void write() {
if (modified) {
if (originalResource != null) {
originalResource.delete();
try {
originalResource.delete();
} catch (Exception ignored) {
// The resource cannot be deleted, e.g. because ECJ has not implemented this method.
}
}
try {
FileObject newResource = filer.createResource(StandardLocation.CLASS_OUTPUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -49,7 +52,7 @@
/*
* This class is separated so javac correctly reports possible errors.
*/
class ConfigMetadataHandler {
class ConfigMetadataHandler {
/*
* Configuration metadata file location.
*/
Expand Down Expand Up @@ -94,8 +97,7 @@ boolean process(RoundEnvironment roundEnv) {
return doProcess(roundEnv);
} catch (Exception e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Failed to process config metadata annotation processor. "
+ toMessage(e));
e.printStackTrace();
+ stackTraceAsString(e));
return false;
}
}
Expand Down Expand Up @@ -210,7 +212,21 @@ private void storeMetadata() {
}
}

private String toMessage(Exception e) {
return e.getClass().getName() + ": " + e.getMessage();
@Retention(RetentionPolicy.CLASS)
private @interface SuppressFBWarnings {

String[] value() default {};

String justification() default "";
}

@SuppressFBWarnings(
value = "INFORMATION_EXPOSURE_THROUGH_AN_ERROR_MESSAGE",
justification = "During compile time this isn't a problem.")
private static String stackTraceAsString(Throwable e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}

0 comments on commit 1ffbb32

Please sign in to comment.