Package java.lang.reflect

Examples of java.lang.reflect.Method


    }

    @Test public void testFindBestMethodWithSignature_longAndMethodName() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeInterface.class);
        Class[] signatureSought = new Class[] {Long.TYPE};
        Method theMethod = helper.findBestMethodWithSignature("method_long", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = signatureSought;
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here


    //   Test 2-arg methods
    //  ===============================================================================================
    @Test public void testFindBestMethodWithSignature_2ArgIntegerObjectAndMethodName() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeInterface.class);
        Class[] signatureSought = new Class[] {Integer.class, Integer.class};
        Method theMethod = helper.findBestMethodWithSignature("twoArgMethod_Object_Object", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = new Class[] {Object.class, Object.class};
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here

    }

    @Test public void testFindBestMethodWithSignature_2ArgLongObjectAndMethodName() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeInterface.class);
        Class[] signatureSought = new Class[] {Long.class, Long.class};
        Method theMethod = helper.findBestMethodWithSignature("twoArgMethod_Object_Object", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = new Class[] {Object.class, Object.class};
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here

    }

    @Test public void testFindBestMethodWithSignature_2ArgSerializableNumberAndMethodName() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeInterface.class);
        Class[] signatureSought = new Class[] {Long.class, Long.class};
        Method theMethod = helper.findBestMethodWithSignature("twoArgMethod_Serializable_Number", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = new Class[] {Serializable.class, Number.class};
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here

    //   Test overridden methods
    //  ===============================================================================================
    @Test public void testFindBestMethodWithSignature_SubInterface_2ArgSerializableAndNumber() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeSubInterface.class);
        Class[] signatureSought = new Class[] {Serializable.class, Number.class};
        Method theMethod = helper.findBestMethodWithSignature("method", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = signatureSought;
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeSubInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here

    }

    @Test public void testFindBestMethodWithSignature_SubInterface_2ArgSerializableAndLong() throws Exception {
        ReflectionHelper helper = new ReflectionHelper(FakeSubInterface.class);
        Class[] signatureSought = new Class[] {Serializable.class, Long.class};
        Method theMethod = helper.findBestMethodWithSignature("method", signatureSought ); //$NON-NLS-1$
        assertNotNull("Failed to find method for args: " + signatureSought, theMethod); //$NON-NLS-1$

        // make sure we got the method we expected
        Class[] signatureFound = theMethod.getParameterTypes();
        Class[] signatureExpected = new Class[] {Serializable.class, Long.class};
        assertEquals("Wrong class", theMethod.getDeclaringClass().getName(), FakeSubInterface.class.getName()); //$NON-NLS-1$
        helpAssertSameMethodSignature("Found wrong method signature", signatureExpected, signatureFound); //$NON-NLS-1$
    }
View Full Code Here

      return ACTIONS;
   }

   public Object getFieldValue(Object bean, String field) throws Exception
   {
      Method method = ReflectionUtil.getGetBindingMethod(bean, field);
      return method.invoke(bean, ReflectionUtil.EMPTY_ARGS);
   }
View Full Code Here

    // Move all prop names to lower case so we can use reflection to get
      // method names and look them up in the connection props.
      final Properties connProps = lowerCaseAllPropNames(props);
      final Method[] methods = bean.getClass().getMethods();
      for (int i = 0; i < methods.length; i++) {
          final Method method = methods[i];
          final String methodName = method.getName();
          // If setter ...
          if ( methodName.startsWith("set") && method.getParameterTypes().length == 1 ) { //$NON-NLS-1$
              // Get the property name
              final String propertyName = methodName.substring(3);    // remove the "set"
              String shortName = propertyName.toLowerCase();
              String propertyValue = null;
              if (prefix != null) {
                propertyValue = connProps.getProperty(prefix + "." + shortName); //$NON-NLS-1$
              } else {
                propertyValue = connProps.getProperty(shortName);
              }
              if (propertyValue == null) {
                continue;
              }
                final Class<?> argType = method.getParameterTypes()[0];
                try {
                    final Object[] params = new Object[] {StringUtil.valueOf(propertyValue, argType)};
                    method.invoke(bean, params);
                } catch (Throwable e) {
                  throw new InvalidPropertyException(propertyName, propertyValue, argType, e);
                }
          }
      }
View Full Code Here

        return;
      }
      name = name.toLowerCase();
      final Method[] methods = bean.getClass().getMethods();
      for (int i = 0; i < methods.length; i++) {
          final Method method = methods[i];
          final String methodName = method.getName();
          // If setter ...
          if ( methodName.startsWith("set") && method.getParameterTypes().length == 1 ) { //$NON-NLS-1$
              // Get the property name
              final String propertyName = methodName.substring(3);    // remove the "set"
              String shortName = propertyName.toLowerCase();
              if (!shortName.equals(name)) {
                continue;
              }
                final Class<?> argType = method.getParameterTypes()[0];
                try {
                  Object[] params = new Object[] {value};
                  if (!argType.isAssignableFrom(value.getClass())) {
                    params = new Object[] {StringUtil.valueOf(value.toString(), argType)};
                  }
                    method.invoke(bean, params);
                } catch (Throwable e) {
                  throw new InvalidPropertyException(propertyName, value.toString(), argType, e);
                }
          }
      }
View Full Code Here

   
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    {
        for (int i = 0; i < delegates.length; i++) {
            Object object = delegates[i];
            Method m = null;
            try {
        m = object.getClass().getDeclaredMethod(method.getName(), method.getParameterTypes());
      } catch (NoSuchMethodException e) {
      }
      if (m != null) {
        try {
          return m.invoke(object, args);
        } catch (InvocationTargetException e) {
          throw e.getTargetException();
        }
      }
        }
View Full Code Here

TOP

Related Classes of java.lang.reflect.Method

Copyright © 2015 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.