選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Reflection.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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 (Method meth : meths) {
  49. if (meth.getName().equals(name) && isCompatible(meth, args)) {
  50. return meth;
  51. }
  52. }
  53. return null;
  54. }
  55. private static boolean isCompatible(Method meth, Object[] args) {
  56. // ignore methods with overloading other than lengths
  57. return meth.getParameterTypes().length == args.length;
  58. }
  59. public static Object getStaticField(Class<?> class_, String name) {
  60. try {
  61. return class_.getField(name).get(null);
  62. } catch (IllegalAccessException e) {
  63. throw new RuntimeException("unimplemented");
  64. } catch (NoSuchFieldException e) {
  65. throw new RuntimeException("unimplemented");
  66. }
  67. }
  68. public static void runMainInSameVM(
  69. String classpath,
  70. String className,
  71. String[] args)
  72. throws SecurityException, NoSuchMethodException,
  73. IllegalArgumentException, IllegalAccessException,
  74. InvocationTargetException, ClassNotFoundException {
  75. LangUtil.throwIaxIfNull(className, "class name");
  76. if (LangUtil.isEmpty(classpath)) {
  77. Class<?> mainClass = Class.forName(className);
  78. runMainInSameVM(mainClass, args);
  79. return;
  80. }
  81. ArrayList<File> dirs = new ArrayList<>();
  82. ArrayList<File> libs = new ArrayList<>();
  83. ArrayList<URL> urls = new ArrayList<>();
  84. String[] entries = LangUtil.splitClasspath(classpath);
  85. for (String entry : entries) {
  86. URL url = makeURL(entry);
  87. if (null != url) {
  88. urls.add(url);
  89. }
  90. File file = new File(entry);
  91. // tolerate bad entries b/c bootclasspath sometimes has them
  92. // if (!file.canRead()) {
  93. // throw new IllegalArgumentException("cannot read " + file);
  94. // }
  95. if (FileUtil.isZipFile(file)) {
  96. libs.add(file);
  97. } else if (file.isDirectory()) {
  98. dirs.add(file);
  99. } else {
  100. // not URL, zip, or dir - unsure what to do
  101. }
  102. }
  103. File[] dirRa = dirs.toArray(new File[0]);
  104. File[] libRa = libs.toArray(new File[0]);
  105. URL[] urlRa = urls.toArray(new URL[0]);
  106. runMainInSameVM(urlRa, libRa, dirRa, className, args);
  107. }
  108. public static void runMainInSameVM(
  109. URL[] urls,
  110. File[] libs,
  111. File[] dirs,
  112. String className,
  113. String[] args)
  114. throws SecurityException, NoSuchMethodException,
  115. IllegalArgumentException, IllegalAccessException,
  116. InvocationTargetException, ClassNotFoundException {
  117. LangUtil.throwIaxIfNull(className, "class name");
  118. LangUtil.throwIaxIfNotAssignable(libs, File.class, "jars");
  119. LangUtil.throwIaxIfNotAssignable(dirs, File.class, "dirs");
  120. URL[] libUrls = FileUtil.getFileURLs(libs);
  121. if (!LangUtil.isEmpty(libUrls)) {
  122. if (!LangUtil.isEmpty(urls)) {
  123. URL[] temp = new URL[libUrls.length + urls.length];
  124. System.arraycopy(urls, 0, temp, 0, urls.length);
  125. System.arraycopy(urls, 0, temp, libUrls.length, urls.length);
  126. urls = temp;
  127. } else {
  128. urls = libUrls;
  129. }
  130. }
  131. UtilClassLoader loader = new UtilClassLoader(urls, dirs);
  132. Class<?> targetClass = null;
  133. try {
  134. targetClass = loader.loadClass(className);
  135. } catch (ClassNotFoundException e) {
  136. String s = "unable to load class " + className
  137. + " using class loader " + loader;
  138. throw new ClassNotFoundException(s);
  139. }
  140. Method main = targetClass.getMethod("main", MAIN_PARM_TYPES);
  141. main.invoke(null, new Object[] { args });
  142. }
  143. public static void runMainInSameVM(Class<?> mainClass, String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
  144. LangUtil.throwIaxIfNull(mainClass, "main class");
  145. Method main = mainClass.getMethod("main", MAIN_PARM_TYPES);
  146. main.invoke(null, new Object[] { args });
  147. }
  148. /** @return URL if the input is valid as such */
  149. private static URL makeURL(String s) {
  150. try {
  151. return new URL(s);
  152. } catch (Throwable t) {
  153. return null;
  154. }
  155. }
  156. }