diff --git a/src/src/com/tns/FileSystem.java b/src/src/com/tns/FileSystem.java index fa7debb70..306b2b896 100644 --- a/src/src/com/tns/FileSystem.java +++ b/src/src/com/tns/FileSystem.java @@ -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){ diff --git a/src/src/com/tns/MethodResolver.java b/src/src/com/tns/MethodResolver.java index 30da2250f..d98b19b12 100644 --- a/src/src/com/tns/MethodResolver.java +++ b/src/src/com/tns/MethodResolver.java @@ -11,6 +11,36 @@ class MethodResolver { + private static Map primitiveTypesSignature = new HashMap(); + + 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 { public final X x; @@ -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> candidates = new ArrayList>(); - int argLength = (args != null) ? args.length : 0; - tryFindMatches(methodName, candidates, args, argLength, clazz.getMethods()); + ArrayList> candidates = new ArrayList>(); 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; @@ -140,50 +149,54 @@ static void tryFindMatches(String methodName, ArrayList> { 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 res = isAssignableFrom(params[i], args[i].getClass()); - success = res.x.booleanValue(); - dist += res.y; - } - else - { - success = !params[i].isPrimitive(); - } - - if (!success) - break; + Tuple 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.valueOf(dist))); - if (dist == 0) + if (!success) break; } } + + if (success) + { + candidates.add(new Tuple(method, Integer.valueOf(dist))); + if (dist == 0) + break; + } } } }