Skip to content

Commit af91702

Browse files
authored
Merge pull request #519 from sajeerzeji/GHMAVEN2021-annotation_processor_paths_issue
Added support for passing annotation processor paths to the Java compiler
2 parents b30438b + 43b170c commit af91702

2 files changed

Lines changed: 146 additions & 22 deletions

File tree

src/main/java/io/openliberty/tools/common/plugins/util/JavaCompilerOptions.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corporation 2020.
2+
* (C) Copyright IBM Corporation 2020, 2026.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,16 @@ public class JavaCompilerOptions {
4646
*/
4747
private String encoding;
4848

49+
/**
50+
* Annotation processor path
51+
*/
52+
private String annotationProcessorPath;
53+
54+
/**
55+
* Annotation processors (comma-separated list of processor class names)
56+
*/
57+
private String annotationProcessors;
58+
4959
/**
5060
* Get list of options that can be passed to the compiler.
5161
* Options with values are represented as multiple strings in the list
@@ -62,6 +72,8 @@ public List<String> getOptions() {
6272
addStringOption(options, "-target", target);
6373
addStringOption(options, "--release", release);
6474
addStringOption(options, "-encoding", encoding);
75+
addStringOption(options, "-processorpath", annotationProcessorPath);
76+
addStringOption(options, "-processor", annotationProcessors);
6577
return options;
6678
}
6779

@@ -112,4 +124,20 @@ public void setEncoding(String encoding) {
112124
this.encoding = encoding;
113125
}
114126

127+
public String getAnnotationProcessorPath() {
128+
return annotationProcessorPath;
129+
}
130+
131+
public void setAnnotationProcessorPath(String annotationProcessorPath) {
132+
this.annotationProcessorPath = annotationProcessorPath;
133+
}
134+
135+
public String getAnnotationProcessors() {
136+
return annotationProcessors;
137+
}
138+
139+
public void setAnnotationProcessors(String annotationProcessors) {
140+
this.annotationProcessors = annotationProcessors;
141+
}
142+
115143
}

src/test/java/io/openliberty/tools/common/plugins/util/JavaCompilerOptionsTest.java

Lines changed: 117 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ public void testAllOptions() throws Exception {
3434
jco.setEncoding("UTF-8");
3535

3636
List<String> result = jco.getOptions();
37-
int i = 0;
38-
assertTrue(result.get(i++).equals("-source"));
39-
assertTrue(result.get(i++).equals("9"));
40-
assertTrue(result.get(i++).equals("-target"));
41-
assertTrue(result.get(i++).equals("1.8"));
42-
assertTrue(result.get(i++).equals("--release"));
43-
assertTrue(result.get(i++).equals("10"));
44-
assertTrue(result.get(i++).equals("-encoding"));
45-
assertTrue(result.get(i++).equals("UTF-8"));
46-
assertEquals(i, result.size());
37+
assertEquals(8, result.size());
38+
assertTrue(result.contains("-source"));
39+
assertTrue(result.contains("9"));
40+
assertTrue(result.contains("-target"));
41+
assertTrue(result.contains("1.8"));
42+
assertTrue(result.contains("--release"));
43+
assertTrue(result.contains("10"));
44+
assertTrue(result.contains("-encoding"));
45+
assertTrue(result.contains("UTF-8"));
4746
}
4847

4948
@Test
@@ -62,9 +61,9 @@ public void testSource() throws Exception {
6261

6362
List<String> result = jco.getOptions();
6463
assertEquals(3, result.size());
65-
assertTrue(result.get(0).equals("-nowarn"));
66-
assertTrue(result.get(1).equals("-source"));
67-
assertTrue(result.get(2).equals("10"));
64+
assertTrue(result.contains("-nowarn"));
65+
assertTrue(result.contains("-source"));
66+
assertTrue(result.contains("10"));
6867
}
6968

7069
@Test
@@ -74,9 +73,9 @@ public void testTarget() throws Exception {
7473

7574
List<String> result = jco.getOptions();
7675
assertEquals(3, result.size());
77-
assertTrue(result.get(0).equals("-nowarn"));
78-
assertTrue(result.get(1).equals("-target"));
79-
assertTrue(result.get(2).equals("10"));
76+
assertTrue(result.contains("-nowarn"));
77+
assertTrue(result.contains("-target"));
78+
assertTrue(result.contains("10"));
8079
}
8180

8281
@Test
@@ -86,9 +85,9 @@ public void testRelease() throws Exception {
8685

8786
List<String> result = jco.getOptions();
8887
assertEquals(3, result.size());
89-
assertTrue(result.get(0).equals("-nowarn"));
90-
assertTrue(result.get(1).equals("--release"));
91-
assertTrue(result.get(2).equals("10"));
88+
assertTrue(result.contains("-nowarn"));
89+
assertTrue(result.contains("--release"));
90+
assertTrue(result.contains("10"));
9291
}
9392

9493
@Test
@@ -98,9 +97,106 @@ public void testEncoding() throws Exception {
9897

9998
List<String> result = jco.getOptions();
10099
assertEquals(3, result.size());
100+
assertTrue(result.contains("-nowarn"));
101+
assertTrue(result.contains("-encoding"));
102+
assertTrue(result.contains("UTF-8"));
103+
}
104+
105+
@Test
106+
public void testAnnotationProcessorPath() throws Exception {
107+
JavaCompilerOptions jco = new JavaCompilerOptions();
108+
jco.setAnnotationProcessorPath("/path/to/lombok.jar:/path/to/mapstruct.jar");
109+
110+
List<String> result = jco.getOptions();
111+
assertEquals(3, result.size());
112+
assertTrue(result.contains("-nowarn"));
113+
assertTrue(result.contains("-processorpath"));
114+
assertTrue(result.contains("/path/to/lombok.jar:/path/to/mapstruct.jar"));
115+
}
116+
117+
@Test
118+
public void testAnnotationProcessorPathWithOtherOptions() throws Exception {
119+
JavaCompilerOptions jco = new JavaCompilerOptions();
120+
jco.setRelease("17");
121+
jco.setEncoding("UTF-8");
122+
jco.setAnnotationProcessorPath("/path/to/lombok.jar");
123+
124+
List<String> result = jco.getOptions();
125+
assertEquals(7, result.size());
126+
assertTrue(result.contains("-nowarn"));
127+
assertTrue(result.contains("--release"));
128+
assertTrue(result.contains("17"));
129+
assertTrue(result.contains("-encoding"));
130+
assertTrue(result.contains("UTF-8"));
131+
assertTrue(result.contains("-processorpath"));
132+
assertTrue(result.contains("/path/to/lombok.jar"));
133+
}
134+
135+
@Test
136+
public void testAnnotationProcessorPathNull() throws Exception {
137+
JavaCompilerOptions jco = new JavaCompilerOptions();
138+
jco.setAnnotationProcessorPath(null);
139+
140+
List<String> result = jco.getOptions();
141+
assertEquals(1, result.size());
101142
assertTrue(result.get(0).equals("-nowarn"));
102-
assertTrue(result.get(1).equals("-encoding"));
103-
assertTrue(result.get(2).equals("UTF-8"));
143+
}
144+
145+
@Test
146+
public void testAnnotationProcessorPathEmpty() throws Exception {
147+
JavaCompilerOptions jco = new JavaCompilerOptions();
148+
jco.setAnnotationProcessorPath("");
149+
150+
List<String> result = jco.getOptions();
151+
assertEquals(1, result.size());
152+
assertTrue(result.get(0).equals("-nowarn"));
153+
}
154+
155+
@Test
156+
public void testAnnotationProcessors() throws Exception {
157+
JavaCompilerOptions jco = new JavaCompilerOptions();
158+
jco.setAnnotationProcessors("lombok.AnnotationProcessor,org.mapstruct.ap.MappingProcessor");
159+
160+
List<String> result = jco.getOptions();
161+
assertEquals(3, result.size());
162+
assertTrue(result.contains("-nowarn"));
163+
assertTrue(result.contains("-processor"));
164+
assertTrue(result.contains("lombok.AnnotationProcessor,org.mapstruct.ap.MappingProcessor"));
165+
}
166+
167+
@Test
168+
public void testAnnotationProcessorsWithPath() throws Exception {
169+
JavaCompilerOptions jco = new JavaCompilerOptions();
170+
jco.setAnnotationProcessorPath("/path/to/lombok.jar");
171+
jco.setAnnotationProcessors("lombok.AnnotationProcessor");
172+
173+
List<String> result = jco.getOptions();
174+
assertEquals(5, result.size());
175+
assertTrue(result.contains("-nowarn"));
176+
assertTrue(result.contains("-processorpath"));
177+
assertTrue(result.contains("/path/to/lombok.jar"));
178+
assertTrue(result.contains("-processor"));
179+
assertTrue(result.contains("lombok.AnnotationProcessor"));
180+
}
181+
182+
@Test
183+
public void testAnnotationProcessorsNull() throws Exception {
184+
JavaCompilerOptions jco = new JavaCompilerOptions();
185+
jco.setAnnotationProcessors(null);
186+
187+
List<String> result = jco.getOptions();
188+
assertEquals(1, result.size());
189+
assertTrue(result.contains("-nowarn"));
190+
}
191+
192+
@Test
193+
public void testAnnotationProcessorsEmpty() throws Exception {
194+
JavaCompilerOptions jco = new JavaCompilerOptions();
195+
jco.setAnnotationProcessors("");
196+
197+
List<String> result = jco.getOptions();
198+
assertEquals(1, result.size());
199+
assertTrue(result.contains("-nowarn"));
104200
}
105201

106202
}

0 commit comments

Comments
 (0)