Skip to content

Commit

Permalink
Removed circular dependencies between ConfigExtension and ConfigProdu…
Browse files Browse the repository at this point in the history
…cerUtil.
  • Loading branch information
radcortez committed Jun 23, 2020
1 parent 2f8fb38 commit 757e234
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;
Expand All @@ -34,7 +33,6 @@
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
Expand Down Expand Up @@ -120,7 +118,7 @@ void validate(@Observes AfterDeploymentValidation adv) {
}

ConfigProperty configProperty = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class);
String name = getConfigKey(injectionPoint, configProperty);
String name = ConfigProducerUtil.getConfigKey(injectionPoint, configProperty);

// Check if the name is part of the properties first. Since properties can be a subset, then search for the actual property for a value.
if (!configNames.contains(name) && ConfigProducerUtil.getRawValue(name, (SmallRyeConfig) config) == null) {
Expand All @@ -142,27 +140,6 @@ void validate(@Observes AfterDeploymentValidation adv) {
}
}

static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) {
String key = configProperty.name();
if (!key.trim().isEmpty()) {
return key;
}
if (ip.getAnnotated() instanceof AnnotatedMember) {
AnnotatedMember member = (AnnotatedMember) ip.getAnnotated();
AnnotatedType declaringType = member.getDeclaringType();
if (declaringType != null) {
String[] parts = declaringType.getJavaClass().getCanonicalName().split("\\.");
StringBuilder sb = new StringBuilder(parts[0]);
for (int i = 1; i < parts.length; i++) {
sb.append(".").append(parts[i]);
}
sb.append(".").append(member.getJavaMember().getName());
return sb.toString();
}
}
throw InjectionMessages.msg.noConfigPropertyDefaultName(ip);
}

private static boolean isClassHandledByConfigProducer(Type requiredType) {
return requiredType == String.class
|| requiredType == Boolean.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -86,7 +85,7 @@ public T create(CreationalContext<T> context) {
InjectionPoint ip = (InjectionPoint) bm.getInjectableReference(new InjectionPointMetadataInjectionPoint(), context);
Annotated annotated = ip.getAnnotated();
ConfigProperty configProperty = annotated.getAnnotation(ConfigProperty.class);
String key = ConfigExtension.getConfigKey(ip, configProperty);
String key = ConfigProducerUtil.getConfigKey(ip, configProperty);
String defaultValue = configProperty.defaultValue();

if (annotated.getBaseType() instanceof ParameterizedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Set;
import java.util.function.Supplier;

import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.InjectionPoint;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -120,13 +122,13 @@ public static String getName(InjectionPoint injectionPoint) {
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier.annotationType().equals(ConfigProperty.class)) {
ConfigProperty configProperty = ((ConfigProperty) qualifier);
return ConfigExtension.getConfigKey(injectionPoint, configProperty);
return getConfigKey(injectionPoint, configProperty);
}
}
return null;
}

private static String getDefaultValue(InjectionPoint injectionPoint) {
public static String getDefaultValue(InjectionPoint injectionPoint) {
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier.annotationType().equals(ConfigProperty.class)) {
String str = ((ConfigProperty) qualifier).defaultValue();
Expand All @@ -148,4 +150,25 @@ private static String getDefaultValue(InjectionPoint injectionPoint) {
}
return null;
}

static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) {
String key = configProperty.name();
if (!key.trim().isEmpty()) {
return key;
}
if (ip.getAnnotated() instanceof AnnotatedMember) {
AnnotatedMember<?> member = (AnnotatedMember<?>) ip.getAnnotated();
AnnotatedType<?> declaringType = member.getDeclaringType();
if (declaringType != null) {
String[] parts = declaringType.getJavaClass().getCanonicalName().split("\\.");
StringBuilder sb = new StringBuilder(parts[0]);
for (int i = 1; i < parts.length; i++) {
sb.append(".").append(parts[i]);
}
sb.append(".").append(member.getJavaMember().getName());
return sb.toString();
}
}
throw InjectionMessages.msg.noConfigPropertyDefaultName(ip);
}
}

0 comments on commit 757e234

Please sign in to comment.