Skip to content

Commit bd5c4ab

Browse files
authored
Merge pull request #181 from mercyblitz/dev
Release 0.1.4
2 parents 3d046ee + 92cc57f commit bd5c4ab

File tree

59 files changed

+5707
-418
lines changed

Some content is hidden

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

59 files changed

+5707
-418
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +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;
23+
import io.microsphere.metadata.ConfigurationPropertyGenerator;
2424

2525
import javax.annotation.processing.AbstractProcessor;
2626
import javax.annotation.processing.Messager;
@@ -37,6 +37,7 @@
3737

3838
import static io.microsphere.annotation.processor.model.util.ConfigurationPropertyJSONElementVisitor.CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME;
3939
import static io.microsphere.annotation.processor.util.MessagerUtils.printNote;
40+
import static io.microsphere.constants.ResourceConstants.CONFIGURATION_PROPERTY_METADATA_RESOURCE;
4041
import static io.microsphere.constants.SymbolConstants.COMMA_CHAR;
4142
import static io.microsphere.constants.SymbolConstants.LEFT_SQUARE_BRACKET_CHAR;
4243
import static io.microsphere.constants.SymbolConstants.RIGHT_SQUARE_BRACKET_CHAR;
@@ -67,7 +68,7 @@
6768
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
6869
* @see ConfigurationProperty
6970
* @see ConfigurationPropertyJSONElementVisitor
70-
* @see ConfigurationPropertyJSONGenerator
71+
* @see ConfigurationPropertyGenerator
7172
* @see ResourceProcessor
7273
* @see Messager
7374
* @see ProcessingEnvironment
@@ -76,11 +77,6 @@
7677
@SupportedAnnotationTypes(value = CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME)
7778
public class ConfigurationPropertyAnnotationProcessor extends AbstractProcessor {
7879

79-
/**
80-
* The resource name of {@link ConfigurationProperty} metadata
81-
*/
82-
public static final String CONFIGURATION_PROPERTY_METADATA_RESOURCE_NAME = "META-INF/microsphere/configuration-properties.json";
83-
8480
private Messager messager;
8581

8682
private StringBuilder jsonBuilder;
@@ -140,14 +136,14 @@ private void appendGeneratedConfigurationPropertyJSON(StringBuilder jsonBuilder)
140136
}
141137

142138
private void appendGeneratedConfigurationPropertyJSON(StringBuilder jsonBuilder, io.microsphere.beans.ConfigurationProperty configurationProperty) {
143-
ConfigurationPropertyJSONGenerator generator = this.jsonElementVisitor.getGenerator();
139+
ConfigurationPropertyGenerator generator = this.jsonElementVisitor.getGenerator();
144140
String json = generator.generate(configurationProperty);
145141
jsonBuilder.append(json)
146142
.append(COMMA_CHAR);
147143
}
148144

149145
private void writeMetadata() {
150-
classPathResourceProcessor.processInResourceWriter(CONFIGURATION_PROPERTY_METADATA_RESOURCE_NAME, writer -> {
146+
classPathResourceProcessor.processInResourceWriter(CONFIGURATION_PROPERTY_METADATA_RESOURCE, writer -> {
151147
JSONArray jsonArray = new JSONArray(jsonBuilder.toString());
152148
String formatedJSON = jsonArray.toString(2);
153149
writer.write(formatedJSON);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import io.microsphere.annotation.ConfigurationProperty;
2121
import io.microsphere.beans.ConfigurationProperty.Metadata;
22-
import io.microsphere.metadata.ConfigurationPropertyJSONGenerator;
22+
import io.microsphere.metadata.ConfigurationPropertyGenerator;
2323

2424
import javax.annotation.processing.ProcessingEnvironment;
2525
import javax.lang.model.element.AnnotationMirror;
@@ -42,7 +42,7 @@
4242

4343
/**
4444
* {@link ConfigurationProperty @ConfigurationProperty}'s {@link AnnotatedElementJSONElementVisitor} based on
45-
* {@link ConfigurationPropertyJSONGenerator} generating the JSON representation of the configuration property metadata.
45+
* {@link ConfigurationPropertyGenerator} generating the JSON representation of the configuration property metadata.
4646
*
4747
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
4848
* @see AnnotatedElementJSONElementVisitor
@@ -54,11 +54,11 @@ public class ConfigurationPropertyJSONElementVisitor extends AnnotatedElementJSO
5454

5555
public static final String CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME = "io.microsphere.annotation.ConfigurationProperty";
5656

57-
private final ConfigurationPropertyJSONGenerator generator;
57+
private final ConfigurationPropertyGenerator generator;
5858

5959
public ConfigurationPropertyJSONElementVisitor(ProcessingEnvironment processingEnv) {
6060
super(processingEnv, CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME);
61-
this.generator = loadFirstService(ConfigurationPropertyJSONGenerator.class);
61+
this.generator = loadFirstService(ConfigurationPropertyGenerator.class);
6262
}
6363

6464
@Override
@@ -101,7 +101,7 @@ public Boolean visitVariableAsField(VariableElement field, StringBuilder jsonBui
101101
return false;
102102
}
103103

104-
public ConfigurationPropertyJSONGenerator getGenerator() {
104+
public ConfigurationPropertyGenerator getGenerator() {
105105
return generator;
106106
}
107107

microsphere-annotation-processor/src/main/resources/META-INF/services/io.microsphere.metadata.ConfigurationPropertyJSONGenerator

Lines changed: 0 additions & 1 deletion
This file was deleted.

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/ConfigurationPropertyAnnotationProcessorTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.util.Set;
2929

30-
import static io.microsphere.annotation.processor.ConfigurationPropertyAnnotationProcessor.CONFIGURATION_PROPERTY_METADATA_RESOURCE_NAME;
3130
import static io.microsphere.annotation.processor.model.util.ConfigurationPropertyJSONElementVisitor.CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME;
3231
import static org.junit.jupiter.api.Assertions.assertEquals;
3332

@@ -57,7 +56,6 @@ protected void addCompiledClasses(Set<Class<?>> compiledClasses) {
5756

5857
@Test
5958
void testConstants() {
60-
assertEquals("META-INF/microsphere/configuration-properties.json", CONFIGURATION_PROPERTY_METADATA_RESOURCE_NAME);
6159
assertEquals("io.microsphere.annotation.ConfigurationProperty", CONFIGURATION_PROPERTY_ANNOTATION_CLASS_NAME);
6260
}
6361
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.beans;
19+
20+
import io.microsphere.annotation.Immutable;
21+
import io.microsphere.annotation.Nonnull;
22+
23+
import java.beans.BeanInfo;
24+
import java.beans.Introspector;
25+
import java.beans.PropertyDescriptor;
26+
import java.util.Collection;
27+
import java.util.Map;
28+
import java.util.Objects;
29+
30+
import static io.microsphere.collection.MapUtils.newFixedHashMap;
31+
import static io.microsphere.lang.function.ThrowableSupplier.execute;
32+
import static io.microsphere.util.ClassUtils.getTypeName;
33+
import static io.microsphere.util.StringUtils.uncapitalize;
34+
import static java.util.Collections.unmodifiableMap;
35+
36+
/**
37+
* The metadata class of Bean, which is used to represent a Java Bean.
38+
*
39+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
40+
* @see BeanInfo
41+
* @see PropertyDescriptor
42+
* @since 1.0.0
43+
*/
44+
public class BeanMetadata {
45+
46+
private final Class<?> beanClass;
47+
48+
private final BeanInfo beanInfo;
49+
50+
private final Map<String, PropertyDescriptor> propertyDescriptorsMap;
51+
52+
protected BeanMetadata(@Nonnull Class<?> beanClass) {
53+
this(execute(() -> Introspector.getBeanInfo(beanClass, Object.class)));
54+
}
55+
56+
protected BeanMetadata(BeanInfo beanInfo) {
57+
this.beanInfo = beanInfo;
58+
this.propertyDescriptorsMap = buildPropertyDescriptorsMap(beanInfo);
59+
this.beanClass = beanInfo.getBeanDescriptor().getBeanClass();
60+
}
61+
62+
@Nonnull
63+
@Immutable
64+
static Map<String, PropertyDescriptor> buildPropertyDescriptorsMap(BeanInfo beanInfo) {
65+
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
66+
int length = propertyDescriptors.length;
67+
Map<String, PropertyDescriptor> propertyDescriptorsMap = newFixedHashMap(length);
68+
for (int i = 0; i < length; i++) {
69+
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
70+
String propertyName = uncapitalize(propertyDescriptor.getName());
71+
propertyDescriptorsMap.put(propertyName, propertyDescriptor);
72+
}
73+
return unmodifiableMap(propertyDescriptorsMap);
74+
}
75+
76+
public BeanInfo getBeanInfo() {
77+
return this.beanInfo;
78+
}
79+
80+
@Nonnull
81+
@Immutable
82+
public Collection<PropertyDescriptor> getPropertyDescriptors() {
83+
return this.propertyDescriptorsMap.values();
84+
}
85+
86+
/**
87+
* Get the {@link PropertyDescriptor} by property name
88+
*
89+
* @param propertyName the property name, which is usually the uncapitalized name of the property
90+
* (e.g., "propertyName" for a property named "PropertyName")
91+
* @return the {@link PropertyDescriptor} if found, otherwise {@code null}
92+
*/
93+
public PropertyDescriptor getPropertyDescriptor(String propertyName) {
94+
return this.propertyDescriptorsMap.get(propertyName);
95+
}
96+
97+
@Nonnull
98+
@Immutable
99+
public Map<String, PropertyDescriptor> getPropertyDescriptorsMap() {
100+
return this.propertyDescriptorsMap;
101+
}
102+
103+
public Class<?> getBeanClass() {
104+
return this.beanClass;
105+
}
106+
107+
@Override
108+
public final boolean equals(Object o) {
109+
if (!(o instanceof BeanMetadata)) return false;
110+
111+
BeanMetadata that = (BeanMetadata) o;
112+
return Objects.equals(getBeanClass(), that.getBeanClass());
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return Objects.hashCode(getBeanClass());
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return "BeanMetadata{" + "beanClass='" + getTypeName(this.beanClass) + "'}";
123+
}
124+
125+
126+
/**
127+
* Create a {@link BeanMetadata} instance from the specified bean class.
128+
*
129+
* <h3>Example Usage</h3>
130+
* <pre>{@code
131+
* // Example 1: Create BeanMetadata for a simple Java Bean
132+
* BeanMetadata metadata = BeanMetadata.of(Person.class);
133+
*
134+
* // Example 2: Create BeanMetadata for a complex Bean
135+
* BeanMetadata metadata = BeanMetadata.of(Company.class);
136+
* }</pre>
137+
*
138+
* @param beanClass the bean class must not be {@code null}
139+
* @return a {@link BeanMetadata} instance
140+
* @throws RuntimeException if the {@link BeanInfo} cannot be obtained from the specified bean class
141+
* @see Introspector#getBeanInfo(Class, Class)
142+
*/
143+
public static BeanMetadata of(@Nonnull Class<?> beanClass) throws RuntimeException {
144+
return new BeanMetadata(beanClass);
145+
}
146+
147+
}

0 commit comments

Comments
 (0)