Skip to content

Commit 6ed7980

Browse files
authored
Fix Gradle annotation processing configuration and update Doma plugin (#1336)
2 parents 5b2df8f + 91beb9a commit 6ed7980

File tree

23 files changed

+69
-36
lines changed

23 files changed

+69
-36
lines changed

doma-processor/src/main/java/org/seasar/doma/internal/apt/DomaProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
Options.METAMODEL_SUFFIX,
7575
Options.RESOURCES_DIR,
7676
Options.SQL_VALIDATION,
77-
Options.TEST,
77+
Options.TEST_INTEGRATION,
78+
Options.TEST_UNIT,
7879
Options.TRACE,
7980
Options.VERSION_VALIDATION,
8081
})

doma-processor/src/main/java/org/seasar/doma/internal/apt/Options.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
public final class Options {
3434

35-
public static final String TEST = "doma.test";
35+
public static final String TEST_UNIT = "doma.test.unit";
36+
37+
public static final String TEST_INTEGRATION = "doma.test.integration";
3638

3739
public static final String METAMODEL_ENABLED = "doma.metamodel.enabled";
3840

@@ -106,7 +108,7 @@ private static Map<String, String> loadConfig(Resources resources, String path)
106108
}
107109

108110
public boolean isTestEnabled() {
109-
String test = getOption(TEST);
111+
String test = getOption(TEST_UNIT);
110112
return Boolean.parseBoolean(test);
111113
}
112114

doma-processor/src/main/java/org/seasar/doma/internal/apt/ProcessingContext.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,31 @@ public void init() {
4343
moreElements = new MoreElements(this, env.getElementUtils());
4444
moreTypes = new MoreTypes(this, env.getTypeUtils());
4545
reporter = new Reporter(env.getMessager());
46-
resources = new Resources(env.getFiler(), env.getOptions().get(Options.RESOURCES_DIR));
46+
resources =
47+
new Resources(
48+
env.getFiler(),
49+
reporter,
50+
env.getOptions().get(Options.RESOURCES_DIR),
51+
isRunningOnEclipse(env),
52+
isDebug(env));
4753
options = new Options(env.getOptions(), resources);
4854
initialized = true;
4955
}
5056

57+
private boolean isRunningOnEclipse(ProcessingEnvironment env) {
58+
var envClassName = env.getClass().getName();
59+
var unitTest = env.getOptions().get(Options.TEST_UNIT);
60+
var integrationTest = env.getOptions().get(Options.TEST_INTEGRATION);
61+
return envClassName.startsWith("org.eclipse.")
62+
&& !Boolean.parseBoolean(unitTest)
63+
&& !Boolean.parseBoolean(integrationTest);
64+
}
65+
66+
private boolean isDebug(ProcessingEnvironment env) {
67+
var debug = env.getOptions().get(Options.DEBUG);
68+
return Boolean.parseBoolean(debug);
69+
}
70+
5171
public RoundContext createRoundContext(
5272
Set<? extends TypeElement> annotationElements, RoundEnvironment roundEnvironment) {
5373
Objects.requireNonNull(annotationElements);

doma-processor/src/main/java/org/seasar/doma/internal/apt/Resources.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ public class Resources {
3232

3333
private final Filer filer;
3434

35+
private final Reporter reporter;
36+
3537
private final String resourcesDir;
3638

37-
Resources(Filer filer, String resourcesDir) {
38-
assertNotNull(filer);
39+
private final boolean isRunningOnEclipse;
40+
41+
private final boolean isDebug;
42+
43+
Resources(
44+
Filer filer,
45+
Reporter reporter,
46+
String resourcesDir,
47+
boolean isRunningOnEclipse,
48+
boolean isDebug) {
49+
assertNotNull(filer, reporter);
3950
this.filer = filer;
51+
this.reporter = reporter;
4052
this.resourcesDir = resourcesDir;
53+
this.isRunningOnEclipse = isRunningOnEclipse;
54+
this.isDebug = isDebug;
4155
}
4256

4357
public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements)
@@ -47,11 +61,30 @@ public JavaFileObject createSourceFile(CharSequence name, Element... originating
4761

4862
public FileObject getResource(String relativePath) throws IOException {
4963
assertNotNull(relativePath);
64+
65+
// Prefer the directory specified by the annotation processor option.
5066
if (resourcesDir != null) {
5167
Path path = Paths.get(resourcesDir, relativePath);
5268
return new FileObjectImpl(path);
5369
}
54-
return filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativePath);
70+
71+
// Since Eclipse doesn’t support SOURCE_PATH, use CLASS_OUTPUT.
72+
if (isRunningOnEclipse) {
73+
return filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativePath);
74+
}
75+
76+
// To leverage Gradle’s incremental annotation processing, we recommend using the --source-path
77+
// option of javac.
78+
try {
79+
return filer.getResource(StandardLocation.SOURCE_PATH, "", relativePath);
80+
} catch (Exception e) {
81+
if (isDebug) {
82+
reporter.debug(
83+
"Fall back from SOURCE_PATH to CLASS_OUTPUT: " + relativePath + ", exception: " + e);
84+
}
85+
// If a file cannot be found in SOURCE_PATH, fall back to CLASS_OUTPUT.
86+
return filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativePath);
87+
}
5588
}
5689

5790
protected static class FileObjectImpl implements FileObject {
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
org.seasar.doma.internal.apt.processor.DomainProcessor,isolating
2-
org.seasar.doma.internal.apt.processor.DataTypeProcessor,isolating
3-
org.seasar.doma.internal.apt.processor.ExternalDomainProcessor,isolating
4-
org.seasar.doma.internal.apt.processor.DomainConvertersProcessor,isolating
5-
org.seasar.doma.internal.apt.processor.EmbeddableProcessor,isolating
6-
org.seasar.doma.internal.apt.processor.EntityProcessor,isolating
7-
org.seasar.doma.internal.apt.processor.DaoProcessor,isolating
8-
org.seasar.doma.internal.apt.processor.ScopeProcessor,isolating
1+
org.seasar.doma.internal.apt.DomaProcessor,isolating

doma-processor/src/test/java/org/seasar/doma/internal/apt/AptinaTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ class AptinaTestCase
111111

112112
@Override
113113
public void beforeEach(ExtensionContext context) {
114+
addOption("-Adoma.test.unit=true");
114115
addSourcePath("src/test/java");
115-
addOption("-Adoma.resources.dir=src/test/resources");
116+
addSourcePath("src/test/resources");
116117
charset = StandardCharsets.UTF_8;
117118
locale = Locale.ENGLISH;
118119
TimeZone.setDefault(TimeZone.getTimeZone("GMT+9"));

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/aggregate/AggregateStrategyProcessorTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class AggregateStrategyProcessorTest extends AbstractCompilerTest {
4040

4141
@BeforeEach
4242
void beforeEach() {
43-
addOption("-Adoma.test=true");
44-
4543
addProcessor(new DomaProcessor());
4644
addCompilationUnit(Emp.class);
4745
addCompilationUnit(Dept.class);

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/dao/DaoProcessorTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class DaoProcessorTest extends AbstractCompilerTest {
4848

4949
@BeforeEach
5050
void beforeEach() {
51-
addOption("-Adoma.test=true");
52-
5351
addProcessor(new DomaProcessor());
5452

5553
// Add the dependent entities

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/dao/aggregate/AggregateStrategyTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class AggregateStrategyTest extends AbstractCompilerTest {
4141

4242
@BeforeEach
4343
void beforeEach() {
44-
addOption("-Adoma.test=true");
45-
4644
addProcessor(new DomaProcessor());
4745

4846
// Add the dependent entities

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/dao/experimental/SqlTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class SqlTest extends AbstractCompilerTest {
4040

4141
@BeforeEach
4242
void beforeEach() {
43-
addOption("-Adoma.test=true");
4443
addProcessor(new DomaProcessor());
4544
}
4645

0 commit comments

Comments
 (0)