コード例 #1
0
 public Object m(String method, Object[] args) {
   try {
     Class<?> c = obj.getClass();
     Method m = null;
     if (args != null) {
       Class[] types = new Class[args.length];
       for (int i = 0; i < args.length; i++) {
         if (args[i] != null) {
           types[i] = args[i].getClass();
         } else types[i] = String.class; // FIXME, use the string type as default
       }
       m = c.getMethod(method, types);
     } else {
       m = c.getMethod(method, (Class[]) null);
     }
     // Log.e(TAG,"now call obj method:"+m.toString());
     return m.invoke(obj, args);
   } catch (NoSuchMethodException e) {
     // e.printStackTrace();
     Log.e(TAG, "Can't find method:" + method);
   } catch (SecurityException e) {
     Log.e(TAG, "Can't get method:" + method + " for security issue");
   } catch (Exception e) {
     Log.e(TAG, "call method:" + method + " failed:" + e.getMessage());
   }
   return null;
 }
コード例 #2
0
  /**
   * Uses 'loader' to load class 'clzz', and calls the static method 'method'. If the return value
   * does not equal 'value' (or if an exception is thrown), then a test failure is indicated.
   *
   * <p>If 'value' is null, then no equality check is performed -- the assertion fails only if an
   * exception is thrown.
   */
  protected void assertStaticCallEquals(
      ClassLoader loader, Class clzz, String method, Object value) {
    java.lang.Class<?> cls = null;
    try {
      cls = java.lang.Class.forName(clzz.getName(), true, loader);
    } catch (ClassNotFoundException e) {
    }
    assertNotNull(cls);

    java.lang.reflect.Method m = null;
    try {
      m = cls.getMethod(method);
    } catch (NoSuchMethodException e) {
    }
    assertNotNull(m);

    try {
      Object res = m.invoke(null);
      assertNotNull(res);
      if (value != null) {
        assertEquals(res, value);
      }
    } catch (InvocationTargetException | IllegalAccessException e) {
      fail("Unexpected exception thrown: " + e.getCause(), e.getCause());
    }
  }
コード例 #3
0
  public static void go()
      throws MalformedURLException, ClassNotFoundException, NoSuchMethodException,
          InvocationTargetException, IllegalAccessException {

    // 1. make List of URLs to pass to CustomClassLoader class
    URL url = new URL(PATH_TO_JAR);
    List<URL> urls = new ArrayList<URL>();
    urls.add(url);

    // 2. Use CustomLoaderClass, to make sure, that loaded classes/methods are not from current
    // project,
    // but from the jar specified in URL, since Java class loaders (including URLClassLoader)
    // first ask to load classes from their parent class loader.
    CustomClassLoader clsLoader = new CustomClassLoader(urls);
    java.lang.Class cls = clsLoader.loadClass("GetNumbers");

    // String.class in methods second parametr means, that we should pass String to that method
    Method method = cls.getMethod("migrate", String.class);

    method.invoke(null, PATH_TO_JOURNALS);

    // 3. invoke method
    List numbers = (List) method.invoke(null, PATH_TO_JOURNALS);

    System.out.println(numbers.size());
    System.out.println(numbers);

    java.lang.Class clsNumbers = clsLoader.loadClass("Numbers");
    Method methodInt = clsNumbers.getMethod("getIntVariable");

    Integer test = (Integer) methodInt.invoke(null);

    System.out.println("Variable: " + test);

    ////////////////////////////////////

    java.lang.Class numbersClass = clsLoader.loadClass("Numbers"); // Class "Numbers"
    Method getCast = numbersClass.getMethod("getCast"); // Method "getCast"
    java.lang.Class castClass = clsLoader.loadClass("Cast"); // Class "Cast"
    Object cast = getCast.invoke(null); // Object of type "Cast"
    System.out.println(castClass.getMethod("getCastString").invoke(cast));
  }
 /** Override toString */
 public String toString() {
   String strId = "ClassifierContextDecl";
   String name = null;
   try {
     java.lang.Class cls = this.getClass();
     java.lang.reflect.Method method = cls.getMethod("getName", new java.lang.Class[] {});
     name = (String) method.invoke(this, new Object[] {});
     if (name != null && name.length() == 0) name = null;
   } catch (Exception e) {
   }
   if (name == null) return strId;
   else return strId + " '" + name + "'";
 }
コード例 #5
0
ファイル: Class.java プロジェクト: AlexWengh/HMM
 @Override
 public ExecuteContext execute(ExecuteContext context) throws Exception {
   int pos = classFilePath.lastIndexOf(File.separatorChar);
   String folder = classFilePath.substring(0, pos);
   String file = classFilePath.substring(pos + 1);
   file = file.replace(".class", "");
   FileSystemClassLoader loader = new FileSystemClassLoader(folder);
   java.lang.Class<?> classs = loader.findClass(file);
   Object obj = classs.newInstance();
   Method method = classs.getMethod("run");
   method.invoke(obj);
   return context;
 }
コード例 #6
0
 public Object m(String method, Class[] types, Object[] args) {
   try {
     Class<?> c = obj.getClass();
     Method m = c.getMethod(method, types);
     // Log.e(TAG,"now call obj method:"+m.toString());
     return m.invoke(obj, args);
   } catch (NoSuchMethodException e) {
     Log.e(TAG, "Can't find method:" + method);
   } catch (SecurityException e) {
     Log.e(TAG, "Can't get method:" + method + " for security issue");
   } catch (Exception e) {
     e.printStackTrace();
     Log.e(TAG, "call method:" + method + " failed:" + e.getMessage());
   }
   return null;
 }
コード例 #7
0
  /**
   * Creates a class which calls target::method(args) via invokevirtual, compiles and loads both the
   * new class and 'target', and then invokes the method. If an exception of type 'exceptionType' is
   * not thrown, then a test failure is indicated.
   */
  public void assertThrows(
      java.lang.Class<?> exceptionType,
      Class target,
      ConcreteMethod method,
      String returns,
      String... args) {

    Compiler compiler = compilerLocal.get();
    compiler.setFlags(compilerFlags());

    Class iv = invokeVirtualHarness(target, method, returns, args);
    ClassLoader loader = compiler.compile(iv, target);

    java.lang.Class<?> cls = null;
    try {
      cls = java.lang.Class.forName(iv.getName(), true, loader);
    } catch (ClassNotFoundException e) {
    }
    assertNotNull(cls);

    java.lang.reflect.Method m = null;
    try {
      m = cls.getMethod(method.getName());
    } catch (NoSuchMethodException e) {
    }
    assertNotNull(m);

    try {
      m.invoke(null);
      fail("Exception should have been thrown");
    } catch (InvocationTargetException | IllegalAccessException e) {
      if (verboseLocal.get() == Boolean.TRUE) {
        System.out.println(e.getCause());
      }
      assertTrue(exceptionType.isAssignableFrom(e.getCause().getClass()));
    }
    compiler.cleanup();
  }
コード例 #8
0
  /**
   * Auto generated method signature
   *
   * @see com.pa.SecondFileService#getEmp
   * @param getEmp4
   */
  public com.pa.GetEmpResponse getEmp(com.pa.GetEmp getEmp4) throws java.rmi.RemoteException {

    org.apache.axis2.context.MessageContext _messageContext = null;
    try {
      org.apache.axis2.client.OperationClient _operationClient =
          _serviceClient.createClient(_operations[1].getName());
      _operationClient.getOptions().setAction("urn:getEmp");
      _operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);

      addPropertyToOperationClient(
          _operationClient,
          org.apache.axis2.description.WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR,
          "&");

      // create a message context
      _messageContext = new org.apache.axis2.context.MessageContext();

      // create SOAP envelope with that payload
      org.apache.axiom.soap.SOAPEnvelope env = null;

      env =
          toEnvelope(
              getFactory(_operationClient.getOptions().getSoapVersionURI()),
              getEmp4,
              optimizeContent(new javax.xml.namespace.QName("http://pa.com", "getEmp")),
              new javax.xml.namespace.QName("http://pa.com", "getEmp"));

      // adding SOAP soap_headers
      _serviceClient.addHeadersToEnvelope(env);
      // set the message context with that soap envelope
      _messageContext.setEnvelope(env);

      // add the message contxt to the operation client
      _operationClient.addMessageContext(_messageContext);

      // execute the operation client
      _operationClient.execute(true);

      org.apache.axis2.context.MessageContext _returnMessageContext =
          _operationClient.getMessageContext(
              org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
      org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();

      java.lang.Object object =
          fromOM(
              _returnEnv.getBody().getFirstElement(),
              com.pa.GetEmpResponse.class,
              getEnvelopeNamespaces(_returnEnv));

      return (com.pa.GetEmpResponse) object;

    } catch (org.apache.axis2.AxisFault f) {

      org.apache.axiom.om.OMElement faultElt = f.getDetail();
      if (faultElt != null) {
        if (faultExceptionNameMap.containsKey(
            new org.apache.axis2.client.FaultMapKey(faultElt.getQName(), "getEmp"))) {
          // make the fault by reflection
          try {
            java.lang.String exceptionClassName =
                (java.lang.String)
                    faultExceptionClassNameMap.get(
                        new org.apache.axis2.client.FaultMapKey(faultElt.getQName(), "getEmp"));
            java.lang.Class exceptionClass = java.lang.Class.forName(exceptionClassName);
            java.lang.reflect.Constructor constructor = exceptionClass.getConstructor(String.class);
            java.lang.Exception ex = (java.lang.Exception) constructor.newInstance(f.getMessage());
            // message class
            java.lang.String messageClassName =
                (java.lang.String)
                    faultMessageMap.get(
                        new org.apache.axis2.client.FaultMapKey(faultElt.getQName(), "getEmp"));
            java.lang.Class messageClass = java.lang.Class.forName(messageClassName);
            java.lang.Object messageObject = fromOM(faultElt, messageClass, null);
            java.lang.reflect.Method m =
                exceptionClass.getMethod("setFaultMessage", new java.lang.Class[] {messageClass});
            m.invoke(ex, new java.lang.Object[] {messageObject});

            throw new java.rmi.RemoteException(ex.getMessage(), ex);
          } catch (java.lang.ClassCastException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          } catch (java.lang.ClassNotFoundException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          } catch (java.lang.NoSuchMethodException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          } catch (java.lang.reflect.InvocationTargetException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          } catch (java.lang.IllegalAccessException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          } catch (java.lang.InstantiationException e) {
            // we cannot intantiate the class - throw the original Axis fault
            throw f;
          }
        } else {
          throw f;
        }
      } else {
        throw f;
      }
    } finally {
      if (_messageContext.getTransportOut() != null) {
        _messageContext.getTransportOut().getSender().cleanup(_messageContext);
      }
    }
  }
コード例 #9
0
 private java.lang.reflect.Method findPut(java.lang.Class<?> cls)
     throws java.lang.SecurityException, java.lang.NoSuchMethodException {
   return cls.getMethod("put", java.lang.Object.class, java.lang.Object.class);
 }