Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested configuration interfaces (#129) #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions owner/src/main/java/org/aeonbits/owner/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.aeonbits.owner;

import org.aeonbits.owner.Config.ConverterClass;
import org.aeonbits.owner.util.Reflection;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
Expand All @@ -17,14 +18,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.*;

import static java.lang.reflect.Modifier.isStatic;
import static org.aeonbits.owner.Converters.SpecialValue.NULL;
Expand Down Expand Up @@ -144,8 +138,29 @@ Object tryConvert(Method targetMethod, Class<?> targetType, String text) {
}
},

PROPERTY_EDITOR {
CLASS_IMPLEMENTING_CONFIG_INTERFACE {
@Override
Object tryConvert(Method targetMethod, Class<?> targetType, String text) {
if (!Reflection.getAllInterfaces(targetType).contains(Config.class)) return SKIP;
try {
Map<String, String> imports = new HashMap<String, String>();
String namespace = ConfigFactory.getProperty("nested.config.prefix");
if (namespace == null) {
namespace = "ns";
}
imports.put(namespace, text);
Object result = ConfigFactory.create((Class<? extends Config>) targetType, imports);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here, is that every time this call is done, a new object (a dynamic proxy) is created; but that's also consistent with the rest of the code. Maybe we can add a class level annotation that allows owner to use a cached instance (see org.aeonbits.owner.ConfigCache.java, http://owner.aeonbits.org/docs/singleton/)

if (result == null) {
return null;
}
return result;
} catch (Exception e) {
return SKIP;
}
}
},

PROPERTY_EDITOR {
@Override
Object tryConvert(Method targetMethod, Class<?> targetType, String text) {
if (!canUsePropertyEditors())
Expand Down Expand Up @@ -180,7 +195,7 @@ private boolean isPropertyEditorDisabled() {
PRIMITIVE {
@Override
Object tryConvert(Method targetMethod, Class<?> targetType, String text) {
if (! targetType.isPrimitive()) return SKIP;
if (!targetType.isPrimitive()) return SKIP;
if (targetType == Byte.TYPE) return Byte.parseByte(text);
if (targetType == Short.TYPE) return Short.parseShort(text);
if (targetType == Integer.TYPE) return Integer.parseInt(text);
Expand Down
18 changes: 17 additions & 1 deletion owner/src/main/java/org/aeonbits/owner/PropertiesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.aeonbits.owner.event.RollbackOperationException;
import org.aeonbits.owner.event.TransactionalPropertyChangeListener;
import org.aeonbits.owner.event.TransactionalReloadListener;
import org.aeonbits.owner.util.Reflection;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand All @@ -25,6 +26,7 @@
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.URI;
Expand Down Expand Up @@ -102,7 +104,7 @@ public boolean remove(Object o) {
this.imports = imports;

ConfigURIFactory urlFactory = new ConfigURIFactory(clazz.getClassLoader(), expander);
uris = toURIs(clazz.getAnnotation(Sources.class), urlFactory);
uris = toURIs(getAnnotationFromSuperInterfaces(clazz, Sources.class), urlFactory);

LoadPolicy loadPolicy = clazz.getAnnotation(LoadPolicy.class);
loadType = (loadPolicy != null) ? loadPolicy.value() : FIRST;
Expand All @@ -122,6 +124,20 @@ public void run() {
}
}

private <T extends Annotation> T getAnnotationFromSuperInterfaces(Class<? extends Config> clazz, Class<T> annotationClass) {
T annotation = (T) clazz.getAnnotation(annotationClass);
if (annotation == null) {
List<Class> allInterfaces = Reflection.getAllInterfaces(clazz);
for (Class anInterface : allInterfaces) {
if (anInterface.getAnnotation(annotationClass) != null) {
annotation = (T) anInterface.getAnnotation(annotationClass);
break;
}
}
}
return annotation;
}

private List<URI> toURIs(Sources sources, ConfigURIFactory uriFactory) {
String[] specs = specs(sources, uriFactory);
List<URI> result = new ArrayList<URI>();
Expand Down
21 changes: 21 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/util/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package org.aeonbits.owner.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
* @author Luigi R. Viggiano
Expand Down Expand Up @@ -65,4 +67,23 @@ public static Object invokeDefaultMethod(Object proxy, Method method, Object[] a
return JAVA_8_SUPPORT.invokeDefaultMethod(proxy, method, args);
}

public static List<Class> getAllInterfaces(Class cls) {
List<Class> list = new ArrayList<Class>();
while (cls != null) {
for (Class anInterface : cls.getInterfaces()) {
if (!list.contains(anInterface)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here you should have used a java.util.Set instead of java.util.List, and you could have skipped this check.

list.add(anInterface);
}
List<Class> superInterfaces = getAllInterfaces(anInterface);
for (Class superInterface : superInterfaces) {
if (!list.contains(superInterface)) {
list.add(superInterface);
}
}
}
cls = cls.getSuperclass();
}
return list;
}

}