@Test public void testGetConstructor() { ReflectionEngine engine = new ReflectionEngine(); MethodInvoker mInvoker = engine.getConstructor("p1.Cat", new Object[] {}); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] {}); mInvoker = engine.getConstructor("p1.Cat", new Object[] {}); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] {}); mInvoker = engine.getConstructor("p1.Cat", new Object[] { new Integer(2) }); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] { int.class }); mInvoker = engine.getConstructor("p1.Cat", new Object[] { new ArrayList<String>(), new String() }); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] { Object.class, String.class }); mInvoker = engine.getConstructor("p1.Cat", new Object[] { "", new String() }); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] { String.class, String.class }); mInvoker = engine.getConstructor("p1.Cat", new Object[] { "a", 2 }); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] { char.class, int.class }); mInvoker = engine.getConstructor("p1.Cat", new Object[] { true, 2 }); assertArrayEquals(mInvoker.getConstructor().getParameterTypes(), new Class[] { boolean.class, short.class }); mInvoker = engine.getConstructor("p1.Cat", new Object[] { "a", 2 }); Object obj = mInvoker.invoke(null, new Object[] { "a", 2 }); assertTrue(obj instanceof Cat); }
public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; }
ReflectionEngine { public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; } }
ReflectionEngine { public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; }  }
ReflectionEngine { public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; }  Object createArray(String fqn, int[] dimensions); Class<?> getClass(Class<?> clazz, String name); Class<?>[] getClassParameters(Object[] parameters); MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters); MethodInvoker getConstructor(String classFQN, Object[] parameters); Field getField(Class<?> clazz, String name); Field getField(Object obj, String name); Field getField(String classFQN, String name); Object getFieldValue(Object obj, Field field); Method getMethod(Class<?> clazz, String name); MethodInvoker getMethod(Class<?> clazz, String name, Class<?>[] parameters); MethodInvoker getMethod(Object object, String name, Object[] parameters); MethodInvoker getMethod(String classFQN, String name, Object[] parameters); Object invoke(Object object, MethodInvoker invoker, Object[] parameters); void setFieldValue(Object obj, Field field, Object value); String[] getPublicMethodNames(Object obj); String[] getPublicFieldNames(Object obj); String[] getPublicStaticFieldNames(Class<?> clazz); String[] getPublicStaticMethodNames(Class<?> clazz); String[] getPublicStaticClassNames(Class<?> clazz); String[] getPublicStaticNames(Class<?> clazz); }
ReflectionEngine { public MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters) { MethodDescriptor mDescriptor = new MethodDescriptor(clazz.getName(), clazz, parameters); MethodInvoker mInvoker = null; List<Constructor<?>> acceptableConstructors = null; LRUCache<MethodDescriptor, MethodInvoker> cache = cacheHolder.get(); mInvoker = cache.get(mDescriptor); if (mInvoker == null) { acceptableConstructors = getConstructorsByLength(clazz, parameters.length); if (acceptableConstructors.size() == 1) { mInvoker = MethodInvoker.buildInvoker(acceptableConstructors.get(0), parameters); } else { mInvoker = getBestConstructor(acceptableConstructors, parameters); } if (mInvoker != null && mInvoker.getCost() != -1) { cache.put(mDescriptor, mInvoker); } else { String errorMessage = "Constructor " + clazz.getName() + "(" + Arrays.toString(parameters) + ") does not exist"; logger.log(Level.WARNING, errorMessage); throw new Py4JException(errorMessage); } } return mInvoker; }  Object createArray(String fqn, int[] dimensions); Class<?> getClass(Class<?> clazz, String name); Class<?>[] getClassParameters(Object[] parameters); MethodInvoker getConstructor(Class<?> clazz, Class<?>[] parameters); MethodInvoker getConstructor(String classFQN, Object[] parameters); Field getField(Class<?> clazz, String name); Field getField(Object obj, String name); Field getField(String classFQN, String name); Object getFieldValue(Object obj, Field field); Method getMethod(Class<?> clazz, String name); MethodInvoker getMethod(Class<?> clazz, String name, Class<?>[] parameters); MethodInvoker getMethod(Object object, String name, Object[] parameters); MethodInvoker getMethod(String classFQN, String name, Object[] parameters); Object invoke(Object object, MethodInvoker invoker, Object[] parameters); void setFieldValue(Object obj, Field field, Object value); String[] getPublicMethodNames(Object obj); String[] getPublicFieldNames(Object obj); String[] getPublicStaticFieldNames(Class<?> clazz); String[] getPublicStaticMethodNames(Class<?> clazz); String[] getPublicStaticClassNames(Class<?> clazz); String[] getPublicStaticNames(Class<?> clazz); final static int cacheSize; final static Object RETURN_VOID; }