You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Reflection.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.util;
  13. import java.io.File;
  14. import java.lang.reflect.InvocationTargetException;
  15. import java.lang.reflect.Method;
  16. import java.net.URL;
  17. import java.util.ArrayList;
  18. //import java.util.StringTokenizer;
  19. public class Reflection {
  20. public static final Class<?>[] MAIN_PARM_TYPES = new Class[] {String[].class};
  21. private Reflection() {
  22. }
  23. public static Object invokestaticN(Class<?> class_, String name, Object[] args) {
  24. return invokeN(class_, name, null, args);
  25. }
  26. public static Object invoke(Class<?> class_, Object target, String name, Object arg1, Object arg2) {
  27. return invokeN(class_, name, target, new Object[] { arg1, arg2 });
  28. }
  29. public static Object invoke(Class<?> class_, Object target, String name, Object arg1, Object arg2, Object arg3) {
  30. return invokeN(class_, name, target, new Object[] { arg1, arg2, arg3 });
  31. }
  32. public static Object invokeN(Class<?> class_, String name, Object target, Object[] args) {
  33. Method meth = getMatchingMethod(class_, name, args);
  34. try {
  35. return meth.invoke(target, args);
  36. } catch (IllegalAccessException e) {
  37. throw new RuntimeException(e.toString());
  38. } catch (InvocationTargetException e) {
  39. Throwable t = e.getTargetException();
  40. if (t instanceof Error) throw (Error)t;
  41. if (t instanceof RuntimeException) throw (RuntimeException)t;
  42. t.printStackTrace();
  43. throw new RuntimeException(t.toString());
  44. }
  45. }
  46. public static Method getMatchingMethod(Class<?> class_, String name, Object[] args) {
  47. Method[] meths = class_.getMethods();
  48. for (int i=0; i < meths.length; i++) {
  49. Method meth = meths[i];
  50. if (meth.getName().equals(name) && isCompatible(meth, args)) {
  51. return meth;
  52. }
  53. }
  54. return null;
  55. }
  56. private static boolean isCompatible(Method meth, Object[] args) {
  57. // ignore methods with overloading other than lengths
  58. return meth.getParameterTypes().length == args.length;
  59. }
  60. public static Object getStaticField(Class<?> class_, String name) {
  61. try {
  62. return class_.getField(name).get(null);
  63. } catch (IllegalAccessException e) {
  64. throw new RuntimeException("unimplemented");
  65. } catch (NoSuchFieldException e) {
  66. throw new RuntimeException("unimplemented");
  67. }
  68. }
  69. public static void runMainInSameVM(
  70. String classpath,
  71. String className,
  72. String[] args)
  73. throws SecurityException, NoSuchMethodException,
  74. IllegalArgumentException, IllegalAccessException,
  75. InvocationTargetException, ClassNotFoundException {
  76. LangUtil.throwIaxIfNull(className, "class name");
  77. if (LangUtil.isEmpty(classpath)) {
  78. Class<?> mainClass = Class.forName(className);
  79. runMainInSameVM(mainClass, args);
  80. return;
  81. }
  82. ArrayList<File> dirs = new ArrayList<File>();
  83. ArrayList<File> libs = new ArrayList<File>();
  84. ArrayList<URL> urls = new ArrayList<URL>();
  85. String[] entries = LangUtil.splitClasspath(classpath);
  86. for (int i = 0; i < entries.length; i++) {
  87. String entry = entries[i];
  88. URL url = makeURL(entry);
  89. if (null != url) {
  90. urls.add(url);
  91. }
  92. File file = new File(entries[i]);
  93. // tolerate bad entries b/c bootclasspath sometimes has them
  94. // if (!file.canRead()) {
  95. // throw new IllegalArgumentException("cannot read " + file);
  96. // }
  97. if (FileUtil.isZipFile(file)) {
  98. libs.add(file);
  99. } else if (file.isDirectory()) {
  100. dirs.add(file);
  101. } else {
  102. // not URL, zip, or dir - unsure what to do
  103. }
  104. }
  105. File[] dirRa = (File[]) dirs.toArray(new File[0]);
  106. File[] libRa = (File[]) libs.toArray(new File[0]);
  107. URL[] urlRa = (URL[]) urls.toArray(new URL[0]);
  108. runMainInSameVM(urlRa, libRa, dirRa, className, args);
  109. }
  110. public static void runMainInSameVM(
  111. URL[] urls,
  112. File[] libs,
  113. File[] dirs,
  114. String className,
  115. String[] args)
  116. throws SecurityException, NoSuchMethodException,
  117. IllegalArgumentException, IllegalAccessException,
  118. InvocationTargetException, ClassNotFoundException {
  119. LangUtil.throwIaxIfNull(className, "class name");
  120. LangUtil.throwIaxIfNotAssignable(libs, File.class, "jars");
  121. LangUtil.throwIaxIfNotAssignable(dirs, File.class, "dirs");
  122. URL[] libUrls = FileUtil.getFileURLs(libs);
  123. if (!LangUtil.isEmpty(libUrls)) {
  124. if (!LangUtil.isEmpty(urls)) {
  125. URL[] temp = new URL[libUrls.length + urls.length];
  126. System.arraycopy(urls, 0, temp, 0, urls.length);
  127. System.arraycopy(urls, 0, temp, libUrls.length, urls.length);
  128. urls = temp;
  129. } else {
  130. urls = libUrls;
  131. }
  132. }
  133. UtilClassLoader loader = new UtilClassLoader(urls, dirs);
  134. Class<?> targetClass = null;
  135. try {
  136. targetClass = loader.loadClass(className);
  137. } catch (ClassNotFoundException e) {
  138. String s = "unable to load class " + className
  139. + " using class loader " + loader;
  140. throw new ClassNotFoundException(s);
  141. }
  142. Method main = targetClass.getMethod("main", MAIN_PARM_TYPES);
  143. main.invoke(null, new Object[] { args });
  144. }
  145. public static void runMainInSameVM(Class<?> mainClass, String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  146. LangUtil.throwIaxIfNull(mainClass, "main class");
  147. Method main = mainClass.getMethod("main", MAIN_PARM_TYPES);
  148. main.invoke(null, new Object[] { args });
  149. }
  150. /** @return URL if the input is valid as such */
  151. private static URL makeURL(String s) {
  152. try {
  153. return new URL(s);
  154. } catch (Throwable t) {
  155. return null;
  156. }
  157. }
  158. }