Skip to content

Commit

Permalink
[#4127] Upgrade embedded jOOR dependency to 0.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Mar 5, 2015
1 parent 9d787a2 commit 7f7c256
Showing 1 changed file with 141 additions and 40 deletions.
181 changes: 141 additions & 40 deletions jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java
Expand Up @@ -43,6 +43,7 @@
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;


Expand All @@ -63,6 +64,7 @@
* // Retrieve the wrapped object * // Retrieve the wrapped object
* *
* @author Lukas Eder * @author Lukas Eder
* @author Irek Matysiewicz
*/ */
public class Reflect { public class Reflect {


Expand Down Expand Up @@ -202,22 +204,12 @@ public <T> T get() {
*/ */
public Reflect set(String name, Object value) throws ReflectException { public Reflect set(String name, Object value) throws ReflectException {
try { try {

Field field = field0(name);
// Try setting a public field
Field field = type().getField(name);
field.set(object, unwrap(value)); field.set(object, unwrap(value));
return this; return this;
} }
catch (Exception e1) { catch (Exception e) {

throw new ReflectException(e);
// Try again, setting a non-public field
try {
accessible(type().getDeclaredField(name)).set(object, unwrap(value));
return this;
}
catch (Exception e2) {
throw new ReflectException(e2);
}
} }
} }


Expand Down Expand Up @@ -255,20 +247,35 @@ public <T> T get(String name) throws ReflectException {
*/ */
public Reflect field(String name) throws ReflectException { public Reflect field(String name) throws ReflectException {
try { try {

Field field = field0(name);
// Try getting a public field
Field field = type().getField(name);
return on(field.get(object)); return on(field.get(object));
} }
catch (Exception e1) { catch (Exception e) {
throw new ReflectException(e);
}
}


// Try again, getting a non-public field private Field field0(String name) throws ReflectException {
try { Class<?> type = type();
return on(accessible(type().getDeclaredField(name)).get(object));
} // Try getting a public field
catch (Exception e2) { try {
throw new ReflectException(e2); return type.getField(name);
}

// Try again, getting a non-public field
catch (NoSuchFieldException e) {
do {
try {
return accessible(type.getDeclaredField(name));
}
catch (NoSuchFieldException ignore) {}

type = type.getSuperclass();
} }
while (type != null);

throw new ReflectException(e);
} }
} }


Expand All @@ -289,13 +296,21 @@ public Reflect field(String name) throws ReflectException {
*/ */
public Map<String, Reflect> fields() { public Map<String, Reflect> fields() {
Map<String, Reflect> result = new LinkedHashMap<String, Reflect>(); Map<String, Reflect> result = new LinkedHashMap<String, Reflect>();
Class<?> type = type();


for (Field field : type().getFields()) { do {
if (!isClass ^ Modifier.isStatic(field.getModifiers())) { for (Field field : type.getDeclaredFields()) {
String name = field.getName(); if (!isClass ^ Modifier.isStatic(field.getModifiers())) {
result.put(name, field(name)); String name = field.getName();

if (!result.containsKey(name))
result.put(name, field(name));
}
} }

type = type.getSuperclass();
} }
while (type != null);


return result; return result;
} }
Expand Down Expand Up @@ -338,6 +353,14 @@ public Reflect call(String name) throws ReflectException {
* public void method(Number param1, Object param2); * public void method(Number param1, Object param2);
* public void method(int param1, Object param2); * public void method(int param1, Object param2);
* </pre></code> * </pre></code>
* <p>
* The best matching method is searched for with the following strategy:
* <ol>
* <li>public method with exact signature match in class hierarchy</li>
* <li>non-public method with exact signature match on declaring class</li>
* <li>public method with similar signature in class hierarchy</li>
* <li>non-public method with similar signature on declaring class</li>
* </ol>
* *
* @param name The method name * @param name The method name
* @param args The method arguments * @param args The method arguments
Expand All @@ -352,21 +375,93 @@ public Reflect call(String name, Object... args) throws ReflectException {
// Try invoking the "canonical" method, i.e. the one with exact // Try invoking the "canonical" method, i.e. the one with exact
// matching argument types // matching argument types
try { try {
Method method = type().getMethod(name, types); Method method = exactMethod(name, types);
return on(method, object, args); return on(method, object, args);
} }


// If there is no exact match, try to find one that has a "similar" // If there is no exact match, try to find a method that has a "similar"
// signature if primitive argument types are converted to their wrappers // signature if primitive argument types are converted to their wrappers
catch (NoSuchMethodException e) { catch (NoSuchMethodException e) {
for (Method method : type().getMethods()) { try {
if (method.getName().equals(name) && match(method.getParameterTypes(), types)) { Method method = similarMethod(name, types);
return on(method, object, args); return on(method, object, args);
} catch (NoSuchMethodException e1) {
throw new ReflectException(e1);
}
}
}

/**
* Searches a method with the exact same signature as desired.
* <p>
* If a public method is found in the class hierarchy, this method is returned.
* Otherwise a private method with the exact same signature is returned.
* If no exact match could be found, we let the {@code NoSuchMethodException} pass through.
*/
private Method exactMethod(String name, Class<?>[] types) throws NoSuchMethodException {
Class<?> type = type();

// first priority: find a public method with exact signature match in class hierarchy
try {
return type.getMethod(name, types);
}

// second priority: find a private method with exact signature match on declaring class
catch (NoSuchMethodException e) {
do {
try {
return type.getDeclaredMethod(name, types);
} }
catch (NoSuchMethodException ignore) {}

type = type.getSuperclass();
} }
while (type != null);


throw new ReflectException(e); throw new NoSuchMethodException();
}
}

/**
* Searches a method with a similar signature as desired using
* {@link #isSimilarSignature(java.lang.reflect.Method, String, Class[])}.
* <p>
* First public methods are searched in the class hierarchy, then private
* methods on the declaring class. If a method could be found, it is
* returned, otherwise a {@code NoSuchMethodException} is thrown.
*/
private Method similarMethod(String name, Class<?>[] types) throws NoSuchMethodException {
Class<?> type = type();

// first priority: find a public method with a "similar" signature in class hierarchy
// similar interpreted in when primitive argument types are converted to their wrappers
for (Method method : type.getMethods()) {
if (isSimilarSignature(method, name, types)) {
return method;
}
}

// second priority: find a non-public method with a "similar" signature on declaring class
do {
for (Method method : type.getDeclaredMethods()) {
if (isSimilarSignature(method, name, types)) {
return method;
}
}

type = type.getSuperclass();
} }
while (type != null);

throw new NoSuchMethodException("No similar method " + name + " with params " + Arrays.toString(types) + " could be found on type " + type() + ".");
}

/**
* Determines if a method has a "similar" signature, especially if wrapping
* primitive argument types would result in an exactly matching signature.
*/
private boolean isSimilarSignature(Method possiblyMatchingMethod, String desiredMethodName, Class<?>[] desiredParamTypes) {
return possiblyMatchingMethod.getName().equals(desiredMethodName) && match(possiblyMatchingMethod.getParameterTypes(), desiredParamTypes);
} }


/** /**
Expand Down Expand Up @@ -415,14 +510,14 @@ public Reflect create(Object... args) throws ReflectException {
// Try invoking the "canonical" constructor, i.e. the one with exact // Try invoking the "canonical" constructor, i.e. the one with exact
// matching argument types // matching argument types
try { try {
Constructor<?> constructor = type().getConstructor(types); Constructor<?> constructor = type().getDeclaredConstructor(types);
return on(constructor, args); return on(constructor, args);
} }


// If there is no exact match, try to find one that has a "similar" // If there is no exact match, try to find one that has a "similar"
// signature if primitive argument types are converted to their wrappers // signature if primitive argument types are converted to their wrappers
catch (NoSuchMethodException e) { catch (NoSuchMethodException e) {
for (Constructor<?> constructor : type().getConstructors()) { for (Constructor<?> constructor : type().getDeclaredConstructors()) {
if (match(constructor.getParameterTypes(), types)) { if (match(constructor.getParameterTypes(), types)) {
return on(constructor, args); return on(constructor, args);
} }
Expand Down Expand Up @@ -507,9 +602,13 @@ else if (length == 1) {
private boolean match(Class<?>[] declaredTypes, Class<?>[] actualTypes) { private boolean match(Class<?>[] declaredTypes, Class<?>[] actualTypes) {
if (declaredTypes.length == actualTypes.length) { if (declaredTypes.length == actualTypes.length) {
for (int i = 0; i < actualTypes.length; i++) { for (int i = 0; i < actualTypes.length; i++) {
if (!wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i]))) { if (actualTypes[i] == NULL.class)
return false; continue;
}
if (wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i])))
continue;

return false;
} }


return true; return true;
Expand Down Expand Up @@ -556,7 +655,7 @@ public String toString() {
*/ */
private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException { private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException {
try { try {
return on(constructor.newInstance(args)); return on(accessible(constructor).newInstance(args));
} }
catch (Exception e) { catch (Exception e) {
throw new ReflectException(e); throw new ReflectException(e);
Expand Down Expand Up @@ -608,7 +707,7 @@ private static Class<?>[] types(Object... values) {


for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
Object value = values[i]; Object value = values[i];
result[i] = value == null ? Object.class : value.getClass(); result[i] = value == null ? NULL.class : value.getClass();
} }


return result; return result;
Expand Down Expand Up @@ -682,4 +781,6 @@ else if (void.class == type) {


return type; return type;
} }

private static class NULL {}
} }

0 comments on commit 7f7c256

Please sign in to comment.