Skip to content

Commit bbcfc08

Browse files
committed
Spotless
1 parent c42514d commit bbcfc08

4 files changed

Lines changed: 70 additions & 86 deletions

File tree

java/src/main/java/com/github/copilot/CopilotExperimental.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* Marks a type or method as experimental. Experimental APIs may change or be
1515
* removed in future versions without notice.
1616
*
17-
* <p>By default, referencing an experimental API from consumer code causes a
17+
* <p>
18+
* By default, referencing an experimental API from consumer code causes a
1819
* compile-time error. To opt in, pass the compiler option:
19-
* <pre>-Acopilot.experimental.allowed=true</pre>
20+
*
21+
* <pre>
22+
* -Acopilot.experimental.allowed=true
23+
* </pre>
2024
*
2125
* @since 1.0.0
2226
*/

java/src/main/java/com/github/copilot/CopilotExperimentalProcessor.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import javax.annotation.processing.SupportedSourceVersion;
1414
import javax.lang.model.SourceVersion;
1515
import javax.lang.model.element.Element;
16-
import javax.lang.model.element.ElementKind;
1716
import javax.lang.model.element.ExecutableElement;
1817
import javax.lang.model.element.TypeElement;
1918
import javax.lang.model.element.VariableElement;
@@ -27,12 +26,14 @@
2726
/**
2827
* Annotation processor that enforces compile-time gating of experimental APIs.
2928
*
30-
* <p>Any declaration-level reference to a type or method annotated with
31-
* {@link CopilotExperimental} in consumer source code causes a compilation error
32-
* unless the compiler option {@code -Acopilot.experimental.allowed=true} is
33-
* provided.
29+
* <p>
30+
* Any declaration-level reference to a type or method annotated with
31+
* {@link CopilotExperimental} in consumer source code causes a compilation
32+
* error unless the compiler option {@code -Acopilot.experimental.allowed=true}
33+
* is provided.
3434
*
35-
* <p>This processor uses only standard JSR 269 APIs ({@code javax.lang.model.*})
35+
* <p>
36+
* This processor uses only standard JSR 269 APIs ({@code javax.lang.model.*})
3637
* and works with any Java compiler (javac, ECJ, etc.). It checks declarations
3738
* (field types, method parameters, return types, supertypes, thrown types) but
3839
* does not inspect method body expressions.
@@ -79,7 +80,8 @@ private void checkElement(Element element) {
7980
case CLASS, INTERFACE, ENUM, RECORD -> checkTypeElement((TypeElement) element);
8081
case METHOD, CONSTRUCTOR -> checkExecutable((ExecutableElement) element);
8182
case FIELD, ENUM_CONSTANT -> checkField((VariableElement) element);
82-
default -> { }
83+
default -> {
84+
}
8385
}
8486

8587
// Recurse into enclosed elements
@@ -143,18 +145,14 @@ private boolean isExperimental(Element element) {
143145
}
144146
// If the enclosing type is experimental, members are implicitly experimental
145147
Element enclosing = element.getEnclosingElement();
146-
return enclosing != null
147-
&& enclosing.getAnnotation(CopilotExperimental.class) != null;
148+
return enclosing != null && enclosing.getAnnotation(CopilotExperimental.class) != null;
148149
}
149150

150151
private void reportError(Element experimentalElement, Element usageSite, String context) {
151152
Messager messager = processingEnv.getMessager();
152-
messager.printMessage(
153-
Diagnostic.Kind.ERROR,
154-
"Use of experimental API '" + experimentalElement.getSimpleName()
155-
+ "' in " + context + " is not allowed. Add compiler option"
156-
+ " -Acopilot.experimental.allowed=true to opt in.",
157-
usageSite
158-
);
153+
messager.printMessage(Diagnostic.Kind.ERROR,
154+
"Use of experimental API '" + experimentalElement.getSimpleName() + "' in " + context
155+
+ " is not allowed. Add compiler option" + " -Acopilot.experimental.allowed=true to opt in.",
156+
usageSite);
159157
}
160158
}

java/src/main/java/module-info.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@
2525
opens com.github.copilot.generated.rpc to com.fasterxml.jackson.databind;
2626
opens com.github.copilot.rpc to com.fasterxml.jackson.databind;
2727

28-
provides javax.annotation.processing.Processor
29-
with com.github.copilot.CopilotExperimentalProcessor;
28+
provides javax.annotation.processing.Processor with com.github.copilot.CopilotExperimentalProcessor;
3029
}

java/src/test/java/com/github/copilot/CopilotExperimentalProcessorTest.java

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -30,93 +30,79 @@
3030
class CopilotExperimentalProcessorTest {
3131

3232
private static final String EXPERIMENTAL_TYPE_SOURCE = """
33-
package test;
34-
import com.github.copilot.CopilotExperimental;
35-
@CopilotExperimental
36-
public class ExperimentalType {
37-
public void doSomething() {}
38-
}
39-
""";
33+
package test;
34+
import com.github.copilot.CopilotExperimental;
35+
@CopilotExperimental
36+
public class ExperimentalType {
37+
public void doSomething() {}
38+
}
39+
""";
4040

4141
private static final String EXPERIMENTAL_METHOD_SOURCE = """
42-
package test;
43-
import com.github.copilot.CopilotExperimental;
44-
public class StableType {
45-
@CopilotExperimental
46-
public static void experimentalMethod() {}
47-
}
48-
""";
42+
package test;
43+
import com.github.copilot.CopilotExperimental;
44+
public class StableType {
45+
@CopilotExperimental
46+
public static void experimentalMethod() {}
47+
}
48+
""";
4949

5050
private static final String CONSUMER_USES_TYPE_IN_DECLARATIONS = """
51-
package consumer;
52-
import test.ExperimentalType;
53-
public class Consumer {
54-
private ExperimentalType field;
55-
public ExperimentalType getIt() { return field; }
56-
public void setIt(ExperimentalType value) { this.field = value; }
57-
}
58-
""";
51+
package consumer;
52+
import test.ExperimentalType;
53+
public class Consumer {
54+
private ExperimentalType field;
55+
public ExperimentalType getIt() { return field; }
56+
public void setIt(ExperimentalType value) { this.field = value; }
57+
}
58+
""";
5959

6060
private static final String CONSUMER_EXTENDS_TYPE = """
61-
package consumer;
62-
import test.ExperimentalType;
63-
public class Consumer extends ExperimentalType {
64-
}
65-
""";
61+
package consumer;
62+
import test.ExperimentalType;
63+
public class Consumer extends ExperimentalType {
64+
}
65+
""";
6666

6767
@Test
6868
void failsByDefault_whenFieldOrSignatureUsesExperimentalType() {
6969
DiagnosticCollector<JavaFileObject> diagnostics = compile(
70-
List.of(
71-
inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
72-
inMemorySource("consumer.Consumer", CONSUMER_USES_TYPE_IN_DECLARATIONS)
73-
),
74-
Collections.emptyList()
75-
);
70+
List.of(inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
71+
inMemorySource("consumer.Consumer", CONSUMER_USES_TYPE_IN_DECLARATIONS)),
72+
Collections.emptyList());
7673

7774
boolean hasError = diagnostics.getDiagnostics().stream()
78-
.anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR
79-
&& d.getMessage(null).contains("experimental API"));
80-
assertTrue(hasError, "Expected compile error for experimental type in declarations, got: "
81-
+ diagnostics.getDiagnostics());
75+
.anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR && d.getMessage(null).contains("experimental API"));
76+
assertTrue(hasError,
77+
"Expected compile error for experimental type in declarations, got: " + diagnostics.getDiagnostics());
8278
}
8379

8480
@Test
8581
void failsByDefault_whenExtendingExperimentalType() {
8682
DiagnosticCollector<JavaFileObject> diagnostics = compile(
87-
List.of(
88-
inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
89-
inMemorySource("consumer.Consumer", CONSUMER_EXTENDS_TYPE)
90-
),
91-
Collections.emptyList()
92-
);
83+
List.of(inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
84+
inMemorySource("consumer.Consumer", CONSUMER_EXTENDS_TYPE)),
85+
Collections.emptyList());
9386

9487
boolean hasError = diagnostics.getDiagnostics().stream()
95-
.anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR
96-
&& d.getMessage(null).contains("experimental API"));
97-
assertTrue(hasError, "Expected compile error for extending experimental type, got: "
98-
+ diagnostics.getDiagnostics());
88+
.anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR && d.getMessage(null).contains("experimental API"));
89+
assertTrue(hasError,
90+
"Expected compile error for extending experimental type, got: " + diagnostics.getDiagnostics());
9991
}
10092

10193
@Test
10294
void passes_whenOptInFlagIsProvided() {
10395
DiagnosticCollector<JavaFileObject> diagnostics = compile(
104-
List.of(
105-
inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
106-
inMemorySource("test.StableType", EXPERIMENTAL_METHOD_SOURCE),
107-
inMemorySource("consumer.Consumer", CONSUMER_USES_TYPE_IN_DECLARATIONS)
108-
),
109-
List.of("-Acopilot.experimental.allowed=true")
110-
);
96+
List.of(inMemorySource("test.ExperimentalType", EXPERIMENTAL_TYPE_SOURCE),
97+
inMemorySource("test.StableType", EXPERIMENTAL_METHOD_SOURCE),
98+
inMemorySource("consumer.Consumer", CONSUMER_USES_TYPE_IN_DECLARATIONS)),
99+
List.of("-Acopilot.experimental.allowed=true"));
111100

112-
boolean hasError = diagnostics.getDiagnostics().stream()
113-
.anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR);
114-
assertFalse(hasError, "Expected no errors with opt-in flag, got: "
115-
+ diagnostics.getDiagnostics());
101+
boolean hasError = diagnostics.getDiagnostics().stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR);
102+
assertFalse(hasError, "Expected no errors with opt-in flag, got: " + diagnostics.getDiagnostics());
116103
}
117104

118-
private DiagnosticCollector<JavaFileObject> compile(
119-
List<JavaFileObject> sources, List<String> extraOptions) {
105+
private DiagnosticCollector<JavaFileObject> compile(List<JavaFileObject> sources, List<String> extraOptions) {
120106
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
121107
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
122108

@@ -127,8 +113,7 @@ private DiagnosticCollector<JavaFileObject> compile(
127113
options.addAll(List.of("-d", System.getProperty("java.io.tmpdir")));
128114
options.addAll(extraOptions);
129115

130-
JavaCompiler.CompilationTask task = compiler.getTask(
131-
null, null, diagnostics, options, null, sources);
116+
JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, options, null, sources);
132117
task.setProcessors(List.of(new CopilotExperimentalProcessor()));
133118
task.call();
134119

@@ -156,10 +141,8 @@ private static String resolveClasspath() {
156141
}
157142

158143
private static JavaFileObject inMemorySource(String className, String code) {
159-
return new SimpleJavaFileObject(
160-
URI.create("string:///" + className.replace('.', '/') + ".java"),
161-
JavaFileObject.Kind.SOURCE
162-
) {
144+
return new SimpleJavaFileObject(URI.create("string:///" + className.replace('.', '/') + ".java"),
145+
JavaFileObject.Kind.SOURCE) {
163146
@Override
164147
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
165148
return code;

0 commit comments

Comments
 (0)