Permalink
Browse files

Moved getField()/setField() from Configurator to Util

  • Loading branch information...
1 parent 7c5b452 commit c44ed8f744311e84f5bc0e897316ec2c40ee8d24 Bela Ban committed Dec 8, 2011
@@ -69,7 +69,7 @@ public String toString(Object value) {
private static int getPortRange(Protocol protocol) throws Exception {
Field f = protocol.getClass().getDeclaredField("port_range") ;
- return ((Integer) Configurator.getField(f,protocol)).intValue();
+ return ((Integer) Util.getField(f,protocol)).intValue();
}
}
@@ -109,7 +109,7 @@ public Object convert(Object obj, Class<?> propertyFieldType, String propertyNam
private static void setBindAddress(Protocol protocol, InetAddress bind_addr) throws Exception {
Field f=Util.getField(protocol.getClass(), "bind_addr");
- Configurator.setField(f, protocol, bind_addr) ;
+ Util.setField(f, protocol, bind_addr) ;
}
@@ -819,7 +819,7 @@ public static void setDefaultValues(List<ProtocolConfiguration> protocol_configs
else
converted=PropertyHelper.getConvertedValue(protocol, fields[j], properties, defaultValue, true);
if(converted != null)
- setField(fields[j], protocol, converted);
+ Util.setField(fields[j], protocol, converted);
}
catch(Exception e) {
throw new Exception("default could not be assigned for field " + propertyName + " in "
@@ -867,7 +867,7 @@ public static void setDefaultValues(List<Protocol> protocols, StackType ip_versi
else
converted=PropertyHelper.getConvertedValue(protocol, fields[j], defaultValue, true);
if(converted != null)
- setField(fields[j], protocol, converted);
+ Util.setField(fields[j], protocol, converted);
}
catch(Exception e) {
throw new Exception("default could not be assigned for field " + fields[j].getName() + " in "
@@ -1147,7 +1147,7 @@ public static void resolveAndAssignField(Object obj, Field field, Map<String,Str
try {
converted=PropertyHelper.getConvertedValue(obj, field, props, propertyValue, true);
if(converted != null)
- setField(field, obj, converted);
+ Util.setField(field, obj, converted);
}
catch(Exception e) {
String name=obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName();
@@ -1189,30 +1189,7 @@ public static boolean isSetPropertyMethod(Method method) {
}
- public static void setField(Field field, Object target, Object value) {
- if(!Modifier.isPublic(field.getModifiers())) {
- field.setAccessible(true);
- }
- try {
- field.set(target, value);
- }
- catch(IllegalAccessException iae) {
- throw new IllegalArgumentException("Could not set field " + field, iae);
- }
- }
-
- public static Object getField(Field field, Object target) {
- if(!Modifier.isPublic(field.getModifiers())) {
- field.setAccessible(true);
- }
- try {
- return field.get(target);
- }
- catch(IllegalAccessException iae) {
- throw new IllegalArgumentException("Could not get field " + field, iae);
- }
- }
-
+
private static String grabSystemProp(Property annotation) {
String[] system_property_names=annotation.systemProperty();
@@ -91,7 +91,7 @@ public Object getValue(String name) {
Field field=Util.getField(getClass(), name);
if(field == null)
throw new IllegalArgumentException("field \"" + name + "\n not found");
- return Configurator.getField(field, this);
+ return Util.getField(field, this);
}
public Protocol setValues(Map<String,Object> values) {
@@ -102,7 +102,7 @@ public Protocol setValues(Map<String,Object> values) {
Object value=entry.getValue();
Field field=Util.getField(getClass(), attrname);
if(field != null) {
- Configurator.setField(field, this, value);
+ Util.setField(field, this, value);
}
}
return this;
@@ -115,7 +115,7 @@ public Protocol setValue(String name, Object value) {
Field field=Util.getField(getClass(), name);
if(field == null)
throw new IllegalArgumentException("field \"" + name + "\n not found");
- Configurator.setField(field, this, value);
+ Util.setField(field, this, value);
return this;
}
@@ -186,8 +186,8 @@ public void setChannel(JChannel ch) {
Field[] fields=clazz.getDeclaredFields();
for(Field field: fields) {
if(field.isAnnotationPresent(Property.class)) {
- Object value=Configurator.getField(field, prot);
- Configurator.setField(field, new_prot, value);
+ Object value=Util.getField(field, prot);
+ Util.setField(field, new_prot, value);
}
}
@@ -204,8 +204,8 @@ public void setChannel(JChannel ch) {
possible_names.add(Util.methodNameToAttributeName(methodName));
Field field=findField(prot, possible_names);
if(field != null) {
- Object value=Configurator.getField(field, prot);
- Configurator.setField(field, new_prot, value);
+ Object value=Util.getField(field, prot);
+ Util.setField(field, new_prot, value);
}
}
}
@@ -411,7 +411,7 @@ private String printProtocolSpecAsPlainString(boolean print_props) {
Property annotation;
for(Field field: fields) {
if(field.isAnnotationPresent(Property.class)) {
- Object value=Configurator.getField(field, prot);
+ Object value=Util.getField(field, prot);
if(value != null) {
annotation=field.getAnnotation(Property.class);
Class<?> conv_class=annotation.converter();
@@ -440,7 +440,7 @@ private String printProtocolSpecAsPlainString(boolean print_props) {
possible_names.add(Util.methodNameToAttributeName(methodName));
Field field=findField(prot, possible_names);
if(field != null) {
- Object value=Configurator.getField(field, prot);
+ Object value=Util.getField(field, prot);
if(value != null) {
Class<?> conv_class=annotation.converter();
PropertyConverter conv=null;
@@ -24,6 +24,7 @@
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
@@ -842,6 +843,20 @@ public static int size(ViewId vid) {
return retval;
}
+ public static int size(String s) {
+ int retval=Global.BYTE_SIZE;
+ if(s != null)
+ retval+=s.length() +2;
+ return retval;
+ }
+
+ public static int size(byte[] buf) {
+ int retval=Global.BYTE_SIZE + Global.INT_SIZE;
+ if(buf != null)
+ retval+=buf.length;
+ return retval;
+ }
+
private static Address readOtherAddress(DataInput in) throws Exception {
short magic_number=in.readShort();
Class<Address> cl=ClassConfigurator.get(magic_number);
@@ -2667,6 +2682,31 @@ public static Field getField(final Class clazz, String field_name) {
return field;
}
+ public static void setField(Field field, Object target, Object value) {
+ if(!Modifier.isPublic(field.getModifiers())) {
+ field.setAccessible(true);
+ }
+ try {
+ field.set(target, value);
+ }
+ catch(IllegalAccessException iae) {
+ throw new IllegalArgumentException("Could not set field " + field, iae);
+ }
+ }
+
+ public static Object getField(Field field, Object target) {
+ if(!Modifier.isPublic(field.getModifiers())) {
+ field.setAccessible(true);
+ }
+ try {
+ return field.get(target);
+ }
+ catch(IllegalAccessException iae) {
+ throw new IllegalArgumentException("Could not get field " + field, iae);
+ }
+ }
+
+
public static <T> Set<Class<T>> findClassesAssignableFrom(String packageName, Class<T> assignableFrom)
throws IOException, ClassNotFoundException {
ClassLoader loader = Thread.currentThread().getContextClassLoader();

0 comments on commit c44ed8f

Please sign in to comment.