[#1365] Allow to configure all types of configuration settings from command line

This commit is contained in:
2017-04-08 10:19:02 +02:00
parent 27a192570b
commit 64293e3813
5 changed files with 174 additions and 64 deletions
@@ -33,8 +33,6 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
@@ -865,13 +863,7 @@ public final class Configuration {
if (config.containsKey(name)) {
value = config.get(name);
Class<?> type;
Type type2 = ((ParameterizedType) (field.getGenericType())).getActualTypeArguments()[0];
if (type2 instanceof Class<?>) {
type = (Class<?>) type2;
} else {
type = (Class<?>) ((ParameterizedType) type2).getRawType();
}
Class<?> type = ConfigurationItem.getConfigurationFieldType(field);
if (value != null && !type.isAssignableFrom(value.getClass())) {
System.out.println("Configuration item has a wrong type: " + name + " expected: " + type.getSimpleName() + " actual: " + value.getClass().getSimpleName());
value = null;
@@ -926,12 +918,19 @@ public final class Configuration {
}
public static Map<String, Field> getConfigurationFields() {
return getConfigurationFields(false);
}
public static Map<String, Field> getConfigurationFields(boolean lowerCaseNames) {
Field[] fields = Configuration.class.getDeclaredFields();
Map<String, Field> result = new HashMap<>();
for (Field field : fields) {
if (ConfigurationItem.class.isAssignableFrom(field.getType())) {
ConfigurationName annotation = field.getAnnotation(ConfigurationName.class);
String name = annotation == null ? field.getName() : annotation.value();
String name = ConfigurationItem.getName(field);
if (lowerCaseNames) {
name = name.toLowerCase();
}
result.put(name, field);
}
}
@@ -16,8 +16,13 @@
*/
package com.jpexs.decompiler.flash.configuration;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
@@ -94,6 +99,37 @@ public class ConfigurationItem<T> {
return modified;
}
public static ConfigurationItem<?> getItem(Field field) {
try {
field.setAccessible(true);
ConfigurationItem<?> item = (ConfigurationItem<?>) field.get(null);
return item;
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(ConfigurationItem.class.getName()).log(Level.SEVERE, null, ex);
throw new Error(ex);
}
}
public static Class<?> getConfigurationFieldType(Field field) {
Type type = ((ParameterizedType) (field.getGenericType())).getActualTypeArguments()[0];
if (type instanceof Class<?>) {
return (Class<?>) type;
} else {
return (Class<?>) ((ParameterizedType) type).getRawType();
}
}
public static String getName(Field field) {
ConfigurationName annotation = field.getAnnotation(ConfigurationName.class);
String name = annotation == null ? field.getName() : annotation.value();
return name;
}
public static boolean isInternal(Field field) {
ConfigurationInternal cint = field.getAnnotation(ConfigurationInternal.class);
return cint != null;
}
@Override
public String toString() {
return name;