protected PsmMethodAction(Class c, String m, Class[] argArray) {
    // This prevents IllegalAccessExceptions when attempting to
    // invoke methods on a class that is in another package and not
    // defined 'public'.
    if (!Modifier.isPublic(c.getModifiers())) {
      throw new IllegalPsmMethodActionException("Action class must be public.");
    }

    try {
      method = c.getMethod(m, argArray);
    } catch (NoSuchMethodException ex) {
      throw new IllegalPsmMethodActionException(ex.toString() + ": method " + m);
    }

    // Check each exception this method declares thrown.  If it declares
    // exceptions, and any of them are not runtime exceptions, abort.
    Class[] exceptionTypes = method.getExceptionTypes();
    for (int i = 0; i < exceptionTypes.length; i++) {
      Class exceptionClass = exceptionTypes[i];
      if (!RuntimeException.class.isAssignableFrom(exceptionClass)) {
        throw new IllegalPsmMethodActionException(
            "Method must not declare non-Runtime " + "exceptions.");
      }
    }

    // Ensure that the method returns PsmEvent
    if (PsmEvent.class != method.getReturnType()) {
      throw new IllegalPsmMethodActionException("Method return type must be PsmEvent");
    }

    // Ensure that both the method is both public and static.
    if (!Modifier.isStatic(method.getModifiers()) || !Modifier.isPublic(method.getModifiers())) {
      throw new IllegalPsmMethodActionException("Method " + m + " must be static and public.");
    }
  }
Beispiel #2
0
  /**
   * Invokes the method specified by a method description.
   *
   * @param methodDescription the method description of the method that is to be invoked.
   */
  protected void invoke(MethodDescription methodDescription) throws InvocationTargetException {

    VParamUtil.throwIfNull(methodDescription);

    Object o = getObject(methodDescription.getObjectID());

    ObjectDescription oDesc = getObjectDescription(o);

    if (o == null) {
      throw new IllegalStateException("Object must not be null!");
    }

    if (methodDescription.getMethodType() == MethodType.REFERENCE) {

      Object[] parameters = methodDescription.getParameters();

      // error handling
      if (parameters.length > 1) {
        throw new IllegalArgumentException(
            "More than one parameter " + "for reference methods is not allowed!");
      }

      if (parameters.length > 0 && parameters[0] != null) {
        methodDescription.setReturnValue(parameters[0]);
      } else {
        methodDescription.setReturnValue(o);
      }

    } else if (o instanceof ProxyObject) {
      ProxyObject proxy = (ProxyObject) o;
      methodDescription.setReturnValue(proxy.invoke(methodDescription));
      if (!methodDescription
          .getReturnType()
          .equals(methodDescription.getReturnValue().getClass())) {
        System.err.println(">> ProxyObject: wrong type of return value!");
        generateErrorMessage(">> ProxyObject:" + " wrong type of return value!", methodDescription);
      }
    } else if (methodDescription.getMethodType() == MethodType.DEFAULT
        || methodDescription.getMethodType() == MethodType.CUSTOM_REFERENCE) {
      Method m = null;

      try {
        m =
            o.getClass()
                .getMethod(
                    methodDescription.getMethodName(), methodDescription.getParameterTypes());

        // access this method even if language visibility forbids
        // invocation of this method
        m.setAccessible(true);

        methodDescription.setReturnValue(m.invoke(o, methodDescription.getParameters()));

      } catch (NoSuchMethodException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      } catch (IllegalAccessException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      } catch (IllegalArgumentException ex) {
        System.err.println(">> ObjectInspector:" + " invoked method with wrong arguments!");

        String yourParamStr = "";
        String expectedParamStr = "";

        if (methodDescription.getParameters() != null) {

          for (int i = 0; i < methodDescription.getParameters().length; i++) {
            Object parameter = methodDescription.getParameters()[i];
            if (parameter != null) {
              yourParamStr += parameter.getClass().getName() + " ";
            } else {
              yourParamStr += "null ";
            }
          }
        } else {
          yourParamStr = "no parameters given!";
        }

        for (int i = 0; i < m.getParameterTypes().length; i++) {
          expectedParamStr += m.getParameterTypes()[i].getName() + " ";
        }

        System.err.println("   your parameters:     " + yourParamStr);
        System.err.println("   expected parameters: " + expectedParamStr);

        generateErrorMessage(
            methodDescription.getMethodName() + "(): method invoked with wrong arguments!",
            methodDescription);

      } catch (InvocationTargetException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(methodDescription, ex.getCause());
        throw ex;

      } catch (SecurityException ex) {
        Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex);
        generateErrorMessage(
            methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription);
      }
    } // end else if (o instanceof ProxyObject)
  }
  /**
   * Creates a new aspect container.
   *
   * @param aspectClass the aspect class
   */
  private static AspectContainer createAspectContainer(final Class aspectClass) {
    AspectDefinition aspectDefinition = null;

    Set definitions =
        SystemDefinitionContainer.getRegularAndVirtualDefinitionsFor(aspectClass.getClassLoader());
    for (Iterator iterator = definitions.iterator();
        iterator.hasNext() && aspectDefinition == null; ) {
      SystemDefinition systemDefinition = (SystemDefinition) iterator.next();
      for (Iterator iterator1 = systemDefinition.getAspectDefinitions().iterator();
          iterator1.hasNext(); ) {
        AspectDefinition aspectDef = (AspectDefinition) iterator1.next();
        if (aspectClass.getName().replace('/', '.').equals(aspectDef.getClassName())) {
          aspectDefinition = aspectDef;
          break;
        }
      }
    }
    if (aspectDefinition == null) {
      throw new Error("Could not find AspectDefinition for " + aspectClass.getName());
    }

    String containerClassName = aspectDefinition.getContainerClassName();
    try {
      Class containerClass;
      if (containerClassName == null
          || aspectClass.getName().equals(CFlowSystemAspect.CLASS_NAME)) {
        containerClass =
            ContextClassLoader.loadClass(aspectClass.getClassLoader(), DEFAULT_ASPECT_CONTAINER);
      } else {
        containerClass =
            ContextClassLoader.loadClass(aspectClass.getClassLoader(), containerClassName);
      }
      Constructor constructor = containerClass.getConstructor(new Class[] {AspectContext.class});
      final AspectContext aspectContext =
          new AspectContext(
              aspectDefinition.getSystemDefinition().getUuid(),
              aspectClass,
              aspectDefinition.getName(),
              DeploymentModel.getDeploymentModelAsInt(aspectDefinition.getDeploymentModel()),
              aspectDefinition,
              aspectDefinition.getParameters());
      final AspectContainer container =
          (AspectContainer) constructor.newInstance(new Object[] {aspectContext});
      aspectContext.setContainer(container);
      return container;
    } catch (InvocationTargetException e) {
      throw new DefinitionException(e.getTargetException().toString());
    } catch (NoSuchMethodException e) {
      throw new DefinitionException(
          "aspect container does not have a valid constructor ["
              + containerClassName
              + "] need to take an AspectContext instance as its only parameter: "
              + e.toString());
    } catch (Throwable e) {
      StringBuffer cause = new StringBuffer();
      cause.append("could not create aspect container using the implementation specified [");
      cause.append(containerClassName);
      cause.append("] due to: ");
      cause.append(e.toString());
      e.printStackTrace();
      throw new DefinitionException(cause.toString());
    }
  }