Skip to content

Commit

Permalink
Enable inherited config class
Browse files Browse the repository at this point in the history
  • Loading branch information
hogelog committed Sep 20, 2014
1 parent 7e72857 commit 67b8844
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/hogel/config/annotation/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.hogel.config.loader.BasicAttributeLoader;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Attribute {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hogel.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BooleanDefaultValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hogel.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DoubleDefaultValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hogel.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IntDefaultValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hogel.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LongDefaultValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.hogel.config.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Inherited
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StringDefaultValue {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/hogel/config/loader/BasicConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BasicConfigLoader implements ConfigLoader<Config> {
Expand All @@ -19,7 +21,7 @@ public void load(Config config, Object source) throws InvalidConfigException {
}

Class<? extends Config> klass = config.getClass();
Field[] fields = klass.getDeclaredFields();
List<Field> fields = getConfigFields(klass);
try {
for (Field field : fields) {
loadFieldValue(config, field, configMap);
Expand All @@ -29,6 +31,19 @@ public void load(Config config, Object source) throws InvalidConfigException {
}
}

private List<Field> getConfigFields(Class<? extends Config> configClass) {
List<Field> configFields = new ArrayList<>();
Class klass = configClass;
do {
Field[] fields = klass.getDeclaredFields();
for (Field field : fields) {
configFields.add(field);
}
klass = klass.getSuperclass();
} while (Config.class != klass);
return configFields;
}

private void loadFieldValue(Config config, Field field, Map<String, Object> configMap) throws IllegalAccessException, InvalidConfigException {
Annotation[] annotations = field.getAnnotations();
Attribute attributeAnnotation = field.getAnnotation(Attribute.class);
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/hogel/config/InheritedSampleConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.hogel.config;

public class InheritedSampleConfig extends SampleConfig {
}
22 changes: 22 additions & 0 deletions src/test/java/org/hogel/config/InheritedSampleConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.hogel.config;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class InheritedSampleConfigTest extends ConfigTestCase {
@Test
public void loadDefaultInheritedValue() throws Exception {
InheritedSampleConfig sampleConfig = new InheritedSampleConfig();
sampleConfig.load(testConfigPath("empty.yaml"));

assertThat(sampleConfig.getTimeout(), is(1000));
assertThat(sampleConfig.getMark(), is(200_000L));
assertThat(sampleConfig.getSalt(), is("salt"));
assertThat(sampleConfig.isAbortOnFail(), is(true));
assertThat(sampleConfig.getNanika(), is(1800.0));
assertThat(sampleConfig.getIgnore(), is("ignore"));
}

}
5 changes: 0 additions & 5 deletions src/test/java/org/hogel/config/SampleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.hogel.config.annotation.LongDefaultValue;
import org.hogel.config.annotation.StringDefaultValue;

import java.io.IOException;

public class SampleConfig extends Config {
@Attribute
@IntDefaultValue(1000)
Expand All @@ -32,9 +30,6 @@ public class SampleConfig extends Config {

private String ignore = "ignore";

public SampleConfig() throws IOException, InvalidConfigException {
}

public int getTimeout() {
return timeout;
}
Expand Down

0 comments on commit 67b8844

Please sign in to comment.