Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gatanasov/reflection improvements #9

Merged
merged 4 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions src/src/com/tns/FileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,8 @@ public static String readText(File file) throws FileNotFoundException, IOExcepti

public static JSONObject readJSONFile(File file) throws IOException, JSONException
{
JSONObject object = null;

BufferedInputStream inputStream = null;
try
{
inputStream = new BufferedInputStream(new FileInputStream(file));
String content = readAll(inputStream);

if (content != null)
{
object = new JSONObject(content);
}
}
finally
{
if (inputStream != null)
inputStream.close();
}

return object;
String content = readText(file);
return new JSONObject(content);
}

public static String resolveRelativePath(String path, String currentDirectory){
Expand Down
163 changes: 88 additions & 75 deletions src/src/com/tns/MethodResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@

class MethodResolver
{
private static Map<String, String> primitiveTypesSignature = new HashMap<String, String>();

static
{
// Boolean
primitiveTypesSignature.put("boolean", "Z");
primitiveTypesSignature.put("Boolean", "Z");
// Integer
primitiveTypesSignature.put("int", "I");
primitiveTypesSignature.put("Integer", "I");
// Double
primitiveTypesSignature.put("double", "D");
primitiveTypesSignature.put("Double", "D");
// Float
primitiveTypesSignature.put("float", "F");
primitiveTypesSignature.put("Float", "F");
// Short
primitiveTypesSignature.put("short", "S");
primitiveTypesSignature.put("Short", "S");
// Long
primitiveTypesSignature.put("long", "J");
primitiveTypesSignature.put("Long", "J");
// Char
primitiveTypesSignature.put("char", "C");
primitiveTypesSignature.put("Character", "C");
// Byte
primitiveTypesSignature.put("byte", "B");
primitiveTypesSignature.put("Byte", "B");
}

private static class Tuple<X, Y>
{
public final X x;
Expand Down Expand Up @@ -51,86 +81,65 @@ public static String getMethodSignature(Class<?> retType, Class<?>[] params)
public static String getTypeSignature(Class<?> type)
{
if (type == null)
{
return "V";
}

Class<?> t = type;

String typeName = t.getName().toLowerCase();
String array = "";
while (t.isArray())
{
array += "[";
t = type.getComponentType();
typeName = t.getName().toLowerCase();
}

if ("boolean".equals(typeName))
{
return array + "Z";
}
else if ("int".equals(typeName))
{
return array + "I";
}
else if ("double".equals(typeName))
{
return array + "D";
}
else if ("float".equals(typeName))
{
return array + "F";
}
else if ("short".equals(typeName))
{
return array + "S";
}
else if ("long".equals(typeName))
{
return array + "J";
}
else if ("char".equals(typeName))
{
return array + "C";
}
else if ("byte".equals(typeName))

String signature;
if(t.isPrimitive())
{
return array + "B";
signature = primitiveTypesSignature.get(t.getName());
}
else if ("void".equals(typeName))
else if(t.getName().equals("void"))
{
return array + "V";
signature = "V";
}
else
{
return array + "L" + t.getName().replace('.', '/') + ";";
signature = "L" + t.getName().replace('.', '/') + ";";
}

return array + signature;
}

static String resolveMethodOverload(String className, String methodName, Object[] args) throws ClassNotFoundException
{
String methodSig = null;

Class<?> clazz = Class.forName(className);

ArrayList<Tuple<Method, Integer>> candidates = new ArrayList<Tuple<Method, Integer>>();

int argLength = (args != null) ? args.length : 0;

tryFindMatches(methodName, candidates, args, argLength, clazz.getMethods());
ArrayList<Tuple<Method, Integer>> candidates = new ArrayList<Tuple<Method, Integer>>();

Class<?> c = clazz;
while (candidates.isEmpty() && (c != null))
int iterationIndex = 0;
while (c != null)
{
tryFindMatches(methodName, candidates, args, argLength, c.getDeclaredMethods());
if(candidates.size() > iterationIndex && candidates.get(iterationIndex).y == 0)
{
// direct matching (distance 0) found
break;
}

c = c.getSuperclass();
iterationIndex++;
}

if (!candidates.isEmpty())
{
if (candidates.size() > 1)
Collections.sort(candidates, distanceComparator);
Method selectedMethod = candidates.get(0).x;
methodSig = getMethodSignature(selectedMethod.getReturnType(), selectedMethod.getParameterTypes());
Method method = candidates.get(0).x;
methodSig = getMethodSignature(method.getReturnType(), method.getParameterTypes());
}

return methodSig;
Expand All @@ -140,50 +149,54 @@ static void tryFindMatches(String methodName, ArrayList<Tuple<Method, Integer>>
{
for (Method method : methods)
{
if (!method.getName().equals(methodName))
{
continue;
}

int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers))
{
continue;
}

if (method.getName().equals(methodName))
{
Class<?>[] params = method.getParameterTypes();
Class<?>[] params = method.getParameterTypes();

boolean success = false;

if (params.length == argLength)
boolean success = false;

if (params.length == argLength)
{
int dist = 0;
if (argLength == 0)
{
int dist = 0;
if (argLength == 0)
{
success = true;
}
else
success = true;
}
else
{
for (int i = 0; i < params.length; i++)
{
for (int i = 0; i < params.length; i++)
if (args[i] != null)
{
if (args[i] != null)
{
Tuple<Boolean, Integer> res = isAssignableFrom(params[i], args[i].getClass());
success = res.x.booleanValue();
dist += res.y;
}
else
{
success = !params[i].isPrimitive();
}

if (!success)
break;
Tuple<Boolean, Integer> res = isAssignableFrom(params[i], args[i].getClass());
success = res.x.booleanValue();
dist += res.y;
}
else
{
success = !params[i].isPrimitive();
}
}

if (success)
{
candidates.add(new Tuple<Method, Integer>(method, Integer.valueOf(dist)));
if (dist == 0)
if (!success)
break;
}
}

if (success)
{
candidates.add(new Tuple<Method, Integer>(method, Integer.valueOf(dist)));
if (dist == 0)
break;
}
}
}
}
Expand Down