Skip to content

Commit

Permalink
refact code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Jan 19, 2018
1 parent 1c87c46 commit 9287e24
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 74 deletions.
48 changes: 23 additions & 25 deletions src/main/java/com/baidu/bjf/remoting/protobuf/IDLProxyObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public class IDLProxyObject {

/** The cached fields. */
private final Map<String, ReflectInfo> cachedFields = new HashMap<String, ReflectInfo>();

/** The cached. */
private boolean cached = true;

/**
* Checks if is cached.
*
* @return true, if is cached
* @return the cached
*/
public boolean isCached() {
return cached;
Expand Down Expand Up @@ -102,7 +102,7 @@ public IDLProxyObject newInstnace() {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* Do set field value.
*
Expand All @@ -114,8 +114,8 @@ public IDLProxyObject newInstnace() {
* @param cachedFields the cached fields
* @return the IDL proxy object
*/
private IDLProxyObject doSetFieldValue(String fullField, String field,
Object value, Object object, boolean useCache, Map<String, ReflectInfo> cachedFields) {
private IDLProxyObject doSetFieldValue(String fullField, String field, Object value, Object object,
boolean useCache, Map<String, ReflectInfo> cachedFields) {
Field f;
// check cache
if (useCache) {
Expand All @@ -134,8 +134,8 @@ private IDLProxyObject doSetFieldValue(String fullField, String field,
try {
f = FieldUtils.findField(object.getClass(), parent);
if (f == null) {
throw new RuntimeException("No field '" + parent + "' found at class "
+ object.getClass().getName());
throw new RuntimeException(
"No field '" + parent + "' found at class " + object.getClass().getName());
}
Class<?> type = f.getType();
f.setAccessible(true);
Expand All @@ -147,9 +147,9 @@ private IDLProxyObject doSetFieldValue(String fullField, String field,
constructor.setAccessible(true);
o = constructor.newInstance(new Object[0]);
} else if (memberClass) {
Constructor<?> constructor = type.getConstructor(new Class[]{object.getClass()});
Constructor<?> constructor = type.getConstructor(new Class[] { object.getClass() });
constructor.setAccessible(true);
o = constructor.newInstance(new Object[]{object});
o = constructor.newInstance(new Object[] { object });
} else {
o = type.newInstance();
}
Expand Down Expand Up @@ -185,7 +185,7 @@ private IDLProxyObject doSetFieldValue(String fullField, String field,
private IDLProxyObject put(String fullField, String field, Object value, Object object) {
return doSetFieldValue(fullField, field, value, object, this.cached, this.cachedFields);
}

/**
* Put.
*
Expand All @@ -208,14 +208,13 @@ public IDLProxyObject put(String field, Object value) {
*/
private void setField(Object value, Object object, Field f) {
f.setAccessible(true);

Object valueToSet = value;
try {
// check if field type is enum
if (Enum.class.isAssignableFrom(f.getType())) {
Enum v = Enum.valueOf((Class<Enum>) f.getType(), String.valueOf(value)); {
valueToSet = v;
}
Enum v = Enum.valueOf((Class<Enum>) f.getType(), String.valueOf(value));
valueToSet = v;
}
f.set(object, valueToSet);
} catch (Exception e) {
Expand All @@ -236,7 +235,7 @@ public Object get(String field) {

return get(field, field, target);
}

/**
* Do get field value.
*
Expand All @@ -247,9 +246,9 @@ public Object get(String field) {
* @param cachedFields the cached fields
* @return the object
*/
private Object doGetFieldValue(String fullField, String field,
Object object, boolean useCache, Map<String, ReflectInfo> cachedFields) {
// check cache
private Object doGetFieldValue(String fullField, String field, Object object, boolean useCache,
Map<String, ReflectInfo> cachedFields) {
// check cache
Field f;
if (useCache) {
ReflectInfo info = cachedFields.get(fullField);
Expand All @@ -266,8 +265,8 @@ private Object doGetFieldValue(String fullField, String field,
try {
f = FieldUtils.findField(object.getClass(), parent);
if (f == null) {
throw new RuntimeException("No field '" + parent + "' found at class "
+ object.getClass().getName());
throw new RuntimeException(
"No field '" + parent + "' found at class " + object.getClass().getName());
}
f.setAccessible(true);
Object o = f.get(object);
Expand Down Expand Up @@ -344,15 +343,14 @@ public IDLProxyObject decode(byte[] bb) throws IOException {
return new IDLProxyObject(codec, object, cls);

}

/**
* Clear field cache.
*/
public void clearFieldCache() {
cachedFields.clear();
}



/**
* Gets the target.
*
Expand All @@ -366,10 +364,10 @@ public Object getTarget() {
* The Class ReflectInfo.
*/
private static class ReflectInfo {

/** The field. */
private Field field;

/** The target. */
private Object target;

Expand Down
Loading

0 comments on commit 9287e24

Please sign in to comment.