Skip to content

Commit 1e27d65

Browse files
authored
107 Disable constant and enum parsing if Tree not available (#109)
* Disable annotation processing by default
1 parent 33963fa commit 1e27d65

File tree

11 files changed

+41
-19
lines changed

11 files changed

+41
-19
lines changed

annotation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-parent</artifactId>
7-
<version>0.10.0-SNAPSHOT</version>
7+
<version>0.9.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

doc/Usage.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ Add sharedtype dependency, the annotation `@SharedType` is only used at compile
2222
Add Maven properties:
2323
```xml
2424
<properties>
25-
<sharedtype.compilerArg /> <!-- Placeholder -->
26-
<sharedtype.version>0.2.0</sharedtype.version>
25+
<compilerArg /> <!-- Placeholder -->
26+
<sharedtype.version>0.10.0</sharedtype.version>
27+
<sharedtype.enabled>false</sharedtype.enabled> <!-- Disable by default so not to participate in every compilation -->
2728
</properties>
2829
```
2930
Setup annotation processing:
@@ -41,7 +42,9 @@ Setup annotation processing:
4142
</annotationProcessorPaths>
4243
<showWarnings>true</showWarnings> <!-- Show annotation processing info log -->
4344
<compilerArgs>
44-
<arg>${sharedtype.compilerArg}</arg> <!-- supplied as a property from cmd -->
45+
<!-- supplied as properties from cmd -->
46+
<arg>${compilerArg}</arg>
47+
<arg>-Asharedtype.enabled=${sharedtype.enabled}</arg>
4548
</compilerArgs>
4649
</configuration>
4750
</plugin>
@@ -57,7 +60,7 @@ record User(String name, int age, String email) {}
5760
```
5861

5962
Execute annotation processing:
60-
* maven: `./mvnw clean compile "-Dsharedtype.compilerArg=-proc:only"`
63+
* maven: `./mvnw compile -DcompilerArg=-proc:only -Dsharedtype.enabled=true`
6164

6265
By default, below code will be generated:
6366
```typescript

it/java17/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-it-parent</artifactId>
7-
<version>0.10.0-SNAPSHOT</version>
7+
<version>0.9.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

it/java8/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-it-parent</artifactId>
7-
<version>0.10.0-SNAPSHOT</version>
7+
<version>0.9.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

it/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-parent</artifactId>
7-
<version>0.10.0-SNAPSHOT</version>
7+
<version>0.9.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

@@ -13,7 +13,7 @@
1313
<packaging>pom</packaging>
1414

1515
<properties>
16-
<sharedtype.compilerArg />
16+
<compilerArg />
1717
<sharedtype.propsFile>it/sharedtype.properties</sharedtype.propsFile>
1818
<compileClasses/>
1919
</properties>
@@ -100,8 +100,9 @@
100100
</annotationProcessorPaths>
101101
<showWarnings>true</showWarnings>
102102
<compilerArgs>
103-
<arg>${sharedtype.compilerArg}</arg> <!-- supplied from cmd "-Dsharedtype.compilerArg=-proc:only" -->
103+
<arg>${compilerArg}</arg> <!-- supplied from cmd "-Dsharedtype.compilerArg=-proc:only" -->
104104
<arg>-Asharedtype.propsFile=${sharedtype.propsFile}</arg>
105+
<arg>-Asharedtype.enabled=true</arg>
105106
</compilerArgs>
106107
<includes>${compileClasses}</includes>
107108
</configuration>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>online.sharedtype</groupId>
77
<artifactId>sharedtype-parent</artifactId>
8-
<version>0.10.0-SNAPSHOT</version>
8+
<version>0.9.1-SNAPSHOT</version>
99
<packaging>pom</packaging>
1010

1111
<name>SharedType Parent</name>

processor/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-parent</artifactId>
7-
<version>0.10.0-SNAPSHOT</version>
7+
<version>0.9.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

processor/src/main/java/online/sharedtype/processor/AnnotationProcessorImpl.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
* @author Cause Chung
3535
*/
3636
@SupportedAnnotationTypes("online.sharedtype.SharedType")
37-
@SupportedOptions({"sharedtype.propsFile"})
37+
@SupportedOptions({"sharedtype.propsFile", "sharedtype.enabled"})
3838
@AutoService(Processor.class)
3939
public final class AnnotationProcessorImpl extends AbstractProcessor {
4040
private static final String PROPS_FILE_OPTION_NAME = "sharedtype.propsFile";
4141
private static final String DEFAULT_USER_PROPS_FILE = "sharedtype.properties";
4242
private static final boolean ANNOTATION_CONSUMED = true;
43+
private boolean enabled;
4344
Context ctx;
4445
TypeDefParser parser;
4546
TypeResolver resolver;
@@ -54,15 +55,18 @@ public SourceVersion getSupportedSourceVersion() {
5455
public synchronized void init(ProcessingEnvironment processingEnv) {
5556
super.init(processingEnv);
5657
String configFile = processingEnv.getOptions().getOrDefault(PROPS_FILE_OPTION_NAME, DEFAULT_USER_PROPS_FILE);
57-
ctx = new Context(processingEnv, PropsFactory.loadProps(Paths.get(configFile)));
58-
parser = TypeDefParser.create(ctx);
59-
resolver = TypeResolver.create(ctx, parser);
60-
writer = TypeWriter.create(ctx);
58+
enabled = isEnabled(processingEnv);
59+
if (enabled) {
60+
ctx = new Context(processingEnv, PropsFactory.loadProps(Paths.get(configFile)));
61+
parser = TypeDefParser.create(ctx);
62+
resolver = TypeResolver.create(ctx, parser);
63+
writer = TypeWriter.create(ctx);
64+
}
6165
}
6266

6367
@Override
6468
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
65-
if (annotations.isEmpty()) {
69+
if (!enabled || annotations.isEmpty()) {
6670
return ANNOTATION_CONSUMED;
6771
}
6872
if (annotations.size() > 1) {
@@ -97,4 +101,9 @@ void doProcess(Set<? extends Element> elements) {
97101
throw new SharedTypeException("Failed to write,", e);
98102
}
99103
}
104+
105+
private static boolean isEnabled(ProcessingEnvironment processingEnv) {
106+
String enabledExpr = processingEnv.getOptions().getOrDefault("sharedtype.enabled", "false");
107+
return enabledExpr.equalsIgnoreCase("true") || enabledExpr.equalsIgnoreCase("yes");
108+
}
100109
}

processor/src/main/java/online/sharedtype/processor/context/Context.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public Context(ProcessingEnvironment processingEnv, Props props) {
4848
try {
4949
trees = Trees.instance(processingEnv);
5050
} catch (IllegalArgumentException e) {
51-
error("The provided processingEnv '%s' does not support Tree API.", processingEnv);
51+
warn("The provided processingEnv '%s' (%s) does not support Tree API, some features may not work. Error message: %s",
52+
processingEnv, processingEnv.getClass(), e.getMessage());
5253
}
5354
this.trees = trees;
5455
}

processor/src/main/java/online/sharedtype/processor/parser/ConstantTypeDefParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public List<TypeDef> parse(TypeElement typeElement) {
4242
if (!SUPPORTED_ELEMENT_KIND.contains(typeElement.getKind().name())) {
4343
return Collections.emptyList();
4444
}
45+
if (ctx.getTrees() == null) {
46+
ctx.info("Skip parsing constants for type %s, because tree is not available.", typeElement);
47+
return Collections.emptyList();
48+
}
4549

4650
String qualifiedName = typeElement.getQualifiedName().toString();
4751
List<TypeDef> cachedDefs = ctx.getTypeStore().getTypeDefs(qualifiedName);

0 commit comments

Comments
 (0)