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.

ReflectionFactory.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.lang.reflect.Constructor;
  15. import java.util.Arrays;
  16. /**
  17. *
  18. */
  19. public class ReflectionFactory { // XXX lease, pool
  20. public static final String OLD_AJC = "bridge.tools.impl.OldAjc";
  21. public static final String ECLIPSE = "org.aspectj.ajdt.ajc.AjdtCommand";
  22. private static final Object[] NONE = new Object[0];
  23. /**
  24. * Produce a compiler as an ICommand.
  25. *
  26. * @param cname the fully-qualified class name of the command to create by reflection (assuming a public no-argument
  27. * constructor).
  28. * @return ICommand compiler or null
  29. */
  30. public static ICommand makeCommand(String cname, IMessageHandler errorSink) {
  31. return (ICommand) make(ICommand.class, cname, NONE, errorSink);
  32. }
  33. /**
  34. * Make an object of type c by reflectively loading the class cname and creating an instance using args (if any), signalling
  35. * errors (if any) to any errorSink.
  36. */
  37. private static Object make(Class<?> c, String cname, Object[] args, IMessageHandler errorSink) {
  38. final boolean makeErrors = (null != errorSink);
  39. Object result = null;
  40. try {
  41. final Class<?> cfn = Class.forName(cname);
  42. String error = null;
  43. if (args == NONE) {
  44. result = cfn.newInstance();
  45. } else {
  46. Class<?>[] types = getTypes(args);
  47. Constructor<?> constructor = cfn.getConstructor(types);
  48. if (null != constructor) {
  49. result = constructor.newInstance(args);
  50. } else {
  51. if (makeErrors) {
  52. error = "no constructor for " + c + " using " + Arrays.asList(types);
  53. }
  54. }
  55. }
  56. if (null != result) {
  57. if (!c.isAssignableFrom(result.getClass())) {
  58. if (makeErrors) {
  59. error = "expecting type " + c + " got " + result.getClass();
  60. }
  61. result = null;
  62. }
  63. }
  64. if (null != error) {
  65. IMessage mssg = new Message(error, IMessage.FAIL, null, null);
  66. errorSink.handleMessage(mssg);
  67. }
  68. } catch (Throwable t) {
  69. if (makeErrors) {
  70. String mssg = "ReflectionFactory unable to load " + cname + " as " + c.getName();
  71. IMessage m = new Message(mssg, IMessage.FAIL, t, null);
  72. errorSink.handleMessage(m);
  73. }
  74. }
  75. return result;
  76. }
  77. /**
  78. * @return Class[] with types of args or matching null elements
  79. */
  80. private static Class<?>[] getTypes(Object[] args) {
  81. if ((null == args) || (0 < args.length)) {
  82. return new Class[0];
  83. } else {
  84. Class<?>[] result = new Class[args.length];
  85. for (int i = 0; i < result.length; i++) {
  86. if (null != args[i]) {
  87. result[i] = args[i].getClass();
  88. }
  89. }
  90. return result;
  91. }
  92. }
  93. private ReflectionFactory() {
  94. }
  95. }