Skip to content

Commit 96abe7f

Browse files
authored
Merge pull request #179 from microsphere-projects/dev
Release 0.1.3
2 parents 3be2c3d + 3d046ee commit 96abe7f

File tree

313 files changed

+10187
-2562
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

313 files changed

+10187
-2562
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Microsphere Java Framework
22
> The common features used in the other microsphere sub-projects
33
4+
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/microsphere-projects/microsphere-java)
45
[![Maven Build](https://github.com/microsphere-projects/microsphere-java/actions/workflows/maven-build.yml/badge.svg)](https://github.com/microsphere-projects/microsphere-java/actions/workflows/maven-build.yml)
56
[![Codecov](https://codecov.io/gh/microsphere-projects/microsphere-java/branch/main/graph/badge.svg)](https://app.codecov.io/gh/microsphere-projects/microsphere-java)
67
![Maven](https://img.shields.io/maven-central/v/io.github.microsphere-projects/microsphere-java.svg)

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/ConfigurationPropertyAnnotationProcessor.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.microsphere.annotation.ConfigurationProperty;
2121
import io.microsphere.annotation.processor.model.util.ConfigurationPropertyJSONElementVisitor;
2222
import io.microsphere.json.JSONArray;
23+
import io.microsphere.metadata.ConfigurationPropertyJSONGenerator;
2324

2425
import javax.annotation.processing.AbstractProcessor;
2526
import javax.annotation.processing.Messager;
@@ -31,13 +32,15 @@
3132
import javax.lang.model.element.Element;
3233
import javax.lang.model.element.TypeElement;
3334
import java.util.Iterator;
35+
import java.util.List;
3436
import java.util.Set;
3537

36-
import static io.microsphere.annotation.processor.ConfigurationPropertyAnnotationProcessor.CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME;
38+
import static io.microsphere.annotation.processor.model.util.ConfigurationPropertyJSONElementVisitor.CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME;
3739
import static io.microsphere.annotation.processor.util.MessagerUtils.printNote;
3840
import static io.microsphere.constants.SymbolConstants.COMMA_CHAR;
3941
import static io.microsphere.constants.SymbolConstants.LEFT_SQUARE_BRACKET_CHAR;
4042
import static io.microsphere.constants.SymbolConstants.RIGHT_SQUARE_BRACKET_CHAR;
43+
import static io.microsphere.metadata.ConfigurationPropertyLoader.loadAll;
4144
import static javax.lang.model.SourceVersion.latestSupported;
4245
import static javax.tools.StandardLocation.CLASS_OUTPUT;
4346

@@ -63,16 +66,16 @@
6366
*
6467
* @author <a href="mailto:[email protected]">Mercy</a>
6568
* @see ConfigurationProperty
69+
* @see ConfigurationPropertyJSONElementVisitor
70+
* @see ConfigurationPropertyJSONGenerator
71+
* @see ResourceProcessor
72+
* @see Messager
73+
* @see ProcessingEnvironment
6674
* @since 1.0.0
6775
*/
6876
@SupportedAnnotationTypes(value = CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME)
6977
public class ConfigurationPropertyAnnotationProcessor extends AbstractProcessor {
7078

71-
/**
72-
* The {@link Class class} name of {@link ConfigurationProperty}
73-
*/
74-
public static final String CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME = "io.microsphere.annotation.ConfigurationProperty";
75-
7679
/**
7780
* The resource name of {@link ConfigurationProperty} metadata
7881
*/
@@ -84,12 +87,15 @@ public class ConfigurationPropertyAnnotationProcessor extends AbstractProcessor
8487

8588
private ResourceProcessor classPathResourceProcessor;
8689

90+
private ConfigurationPropertyJSONElementVisitor jsonElementVisitor;
91+
8792
@Override
8893
public synchronized void init(ProcessingEnvironment processingEnv) {
8994
super.init(processingEnv);
9095
this.messager = processingEnv.getMessager();
9196
this.jsonBuilder = new StringBuilder();
9297
this.classPathResourceProcessor = new ResourceProcessor(processingEnv, CLASS_OUTPUT);
98+
this.jsonElementVisitor = new ConfigurationPropertyJSONElementVisitor(processingEnv);
9399
}
94100

95101
@Override
@@ -105,15 +111,17 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
105111
private void resolveMetadata(RoundEnvironment roundEnv) {
106112
Set<? extends Element> elements = roundEnv.getRootElements();
107113
if (!elements.isEmpty()) {
108-
ConfigurationPropertyJSONElementVisitor visitor = new ConfigurationPropertyJSONElementVisitor(processingEnv);
109114

110115
Iterator<? extends Element> iterator = elements.iterator();
111116
jsonBuilder.append(LEFT_SQUARE_BRACKET_CHAR);
112117
while (iterator.hasNext()) {
113118
Element element = iterator.next();
114-
element.accept(visitor, jsonBuilder);
119+
element.accept(jsonElementVisitor, jsonBuilder);
115120
}
116121

122+
// append the JSON content generated by ConfigurationPropertyGenerator SPI
123+
appendGeneratedConfigurationPropertyJSON(jsonBuilder);
124+
117125
int lastIndex = jsonBuilder.length() - 1;
118126
if (COMMA_CHAR == jsonBuilder.charAt(lastIndex)) {
119127
jsonBuilder.setCharAt(lastIndex, RIGHT_SQUARE_BRACKET_CHAR);
@@ -123,6 +131,21 @@ private void resolveMetadata(RoundEnvironment roundEnv) {
123131
}
124132
}
125133

134+
private void appendGeneratedConfigurationPropertyJSON(StringBuilder jsonBuilder) {
135+
List<io.microsphere.beans.ConfigurationProperty> configurationProperties = loadAll();
136+
int size = configurationProperties.size();
137+
for (int i = 0; i < size; i++) {
138+
appendGeneratedConfigurationPropertyJSON(jsonBuilder, configurationProperties.get(i));
139+
}
140+
}
141+
142+
private void appendGeneratedConfigurationPropertyJSON(StringBuilder jsonBuilder, io.microsphere.beans.ConfigurationProperty configurationProperty) {
143+
ConfigurationPropertyJSONGenerator generator = this.jsonElementVisitor.getGenerator();
144+
String json = generator.generate(configurationProperty);
145+
jsonBuilder.append(json)
146+
.append(COMMA_CHAR);
147+
}
148+
126149
private void writeMetadata() {
127150
classPathResourceProcessor.processInResourceWriter(CONFIGURATION_PROPERTY_METADATA_RESOURCE_NAME, writer -> {
128151
JSONArray jsonArray = new JSONArray(jsonBuilder.toString());

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/FilerProcessor.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@
3636
* <p>It supports executing operations on the {@link Filer} using a callback model, allowing custom logic to be
3737
* applied while handling exceptions gracefully using provided handlers.</p>
3838
*
39+
* <h3>Example Usage</h3>
40+
* <pre>
41+
* // Creating an instance of FilerProcessor
42+
* FilerProcessor filerProcessor = new FilerProcessor(processingEnv);
43+
*
44+
* // Using processInFiler to create a source file
45+
* filerProcessor.processInFiler(filer -> {
46+
* JavaFileObject file = filer.createSourceFile("com.example.GeneratedClass");
47+
* try (Writer writer = file.openWriter()) {
48+
* writer.write("// Auto-generated class\npublic class GeneratedClass {}");
49+
* }
50+
* return null;
51+
* });
52+
*
53+
* // Using processInFiler with a custom exception handler
54+
* filerProcessor.processInFiler(
55+
* filer -> {
56+
* JavaFileObject file = filer.createSourceFile("com.example.AnotherGeneratedClass");
57+
* try (Writer writer = file.openWriter()) {
58+
* writer.write("// Auto-generated class\npublic class AnotherGeneratedClass {}");
59+
* }
60+
* return null;
61+
* },
62+
* (filer, e) -> {
63+
* System.err.println("Failed to generate file: " + e.getMessage());
64+
* return null;
65+
* }
66+
* );
67+
* </pre>
68+
*
3969
* @author <a href="mailto:[email protected]">Mercy</a>
4070
* @see #processInFiler(ThrowableFunction)
4171
* @see #processInFiler(ThrowableFunction, BiFunction)

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/ResourceProcessor.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@
5555
* - Manipulating resources via OutputStream or Writer
5656
* - Reading or modifying the content directly as CharSequence</p>
5757
*
58+
* <h3>Example Usage</h3>
59+
* <pre>
60+
* // Creating an instance of ResourceProcessor
61+
* ResourceProcessor resourceProcessor = new ResourceProcessor(processingEnv, StandardLocation.SOURCE_OUTPUT);
62+
*
63+
* // Reading content from a resource
64+
* Optional<String> content = resourceProcessor.processInResourceContent("example.txt", CharBuffer::toString);
65+
*
66+
* // Writing to a resource
67+
* resourceProcessor.processInResourceWriter("output.txt", writer -> {
68+
* writer.write("Hello, World!");
69+
* });
70+
*
71+
* // Processing an InputStream
72+
* resourceProcessor.processInResourceInputStream("data.bin", inputStream -> {
73+
* // Process binary data
74+
* return null;
75+
* });
76+
* </pre>
77+
*
5878
* @author <a href="mailto:[email protected]">Mercy</a>
5979
* @see FilerProcessor
6080
* @since 1.0.0

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/model/element/StringAnnotationValue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package io.microsphere.annotation.processor.model.element;
1919

20+
import io.microsphere.annotation.Immutable;
21+
2022
import javax.lang.model.element.AnnotationValue;
2123
import javax.lang.model.element.AnnotationValueVisitor;
2224

@@ -27,6 +29,7 @@
2729
* @see AnnotationValue
2830
* @since 1.0.0
2931
*/
32+
@Immutable
3033
public class StringAnnotationValue implements AnnotationValue {
3134

3235
private final String value;

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/model/util/AnnotatedElementJSONElementVisitor.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,48 @@
3535
import static io.microsphere.util.Assert.assertNotNull;
3636

3737
/**
38-
* {@link ElementVisitor} to assemble JSON on the annotated elements based on the specified {@link Annotation annotation}.
38+
* An abstract implementation of {@link ElementVisitor} that generates JSON content for elements
39+
* annotated with a specific annotation.
40+
*
41+
* <p>This class extends {@link JSONElementVisitor}, providing functionality to filter and process
42+
* only those elements that are annotated with the specified annotation. It leverages the annotation
43+
* processing environment to gather information about the annotated elements and constructs JSON
44+
* representations accordingly.</p>
45+
*
46+
* <h3>Example Usage</h3>
47+
*
48+
* <p>Suppose you have a custom annotation:</p>
49+
*
50+
* <pre>{@code
51+
* @Retention(RetentionPolicy.SOURCE)
52+
* @Target(ElementType.TYPE)
53+
* public @interface MyAnnotation {
54+
* }
55+
* }</pre>
56+
*
57+
* <p>You can create a concrete implementation of this class to process elements annotated with
58+
* {@code MyAnnotation}:</p>
59+
*
60+
* <pre>{@code
61+
* public class MyAnnotatedElementVisitor extends AnnotatedElementJSONElementVisitor {
62+
*
63+
* public MyAnnotatedElementVisitor(ProcessingEnvironment processingEnv, String annotationClassName) {
64+
* super(processingEnv, annotationClassName);
65+
* }
66+
*
67+
* @Override
68+
* protected boolean doVisitType(TypeElement e, StringBuilder jsonBuilder) {
69+
* // Custom logic to generate JSON for the type element
70+
* jsonBuilder.append("{");
71+
* jsonBuilder.append("\"name\":").append("\"").append(e.getSimpleName()).append("\"");
72+
* jsonBuilder.append("}");
73+
* return true;
74+
* }
75+
* }
76+
* }</pre>
77+
*
78+
* <p>The above example demonstrates how to extend this class to create a visitor that processes
79+
* type elements annotated with a custom annotation and generates JSON output for them.</p>
3980
*
4081
* @author <a href="mailto:[email protected]">Mercy</a>
4182
* @see JSONElementVisitor

0 commit comments

Comments
 (0)