Example usage for org.apache.commons.lang3.reflect MethodUtils getAccessibleMethod

List of usage examples for org.apache.commons.lang3.reflect MethodUtils getAccessibleMethod

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect MethodUtils getAccessibleMethod.

Prototype

public static Method getAccessibleMethod(final Class<?> cls, final String methodName,
        final Class<?>... parameterTypes) 

Source Link

Document

Returns an accessible method (that is, one that can be invoked via reflection) with given name and parameters.

Usage

From source file:com.robertsmieja.test.utils.junit.Internal.java

static void doNotUseDefaultMethod(Class<?> aClass, String methodName, Class<?>... parameterTypes) {
    Method methodToCheck = MethodUtils.getAccessibleMethod(aClass, methodName, parameterTypes);
    Assertions.assertNotEquals(Object.class, methodToCheck.getDeclaringClass(),
            methodName + "() method not implemented");
}

From source file:com.offbynull.coroutines.instrumenter.asm.MethodInvokeUtilsTest.java

@Test
public void mustProperlyDetermineStackSizeForNormalMethod() {
    Type type = Type.getType(MethodUtils.getAccessibleMethod(Integer.class, "compareTo", Integer.class));
    MethodInsnNode methodInsnNode = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/util/Integer", "compareTo",
            type.getDescriptor(), false);
    int reqStackCount = MethodInvokeUtils.getArgumentCountRequiredForInvocation(methodInsnNode);

    assertEquals(2, reqStackCount);/*  w ww. j  av a  2s  . co m*/
}

From source file:com.offbynull.coroutines.instrumenter.asm.MethodInvokeUtilsTest.java

@Test
public void mustProperlyDetermineStackSizeForStaticMethod() {
    Type type = Type.getType(MethodUtils.getAccessibleMethod(Integer.class, "decode", String.class));
    MethodInsnNode methodInsnNode = new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Integer", "decode",
            type.getDescriptor(), false);
    int reqStackCount = MethodInvokeUtils.getArgumentCountRequiredForInvocation(methodInsnNode);

    assertEquals(1, reqStackCount);//  ww w.  j av  a 2  s . c om
}

From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java

@Before
public void setup() {
    argumentResolver = new InvocationArgumentsResolver() {

        public Object[] getArguments(Object invokedObject, Method invokedMethod) {
            return new Object[] { 1 };
        }/*  ww  w .  j ava 2s.  c o  m*/
    };
    charAtMethod = MethodUtils.getAccessibleMethod(String.class, "charAt", Integer.TYPE);
}

From source file:com.msc.facturierws.dao.specif.DAOSpecifGeneric.java

@Override
public T fillObject(ResultSet rs) throws SQLException {
    T obj = super.fillObject(rs);

    if (obj != null && sc != null) {
        Method m = MethodUtils.getAccessibleMethod(clazz, "setToken", Token.class);
        try {//  w ww .  j  a  v a  2s  .c om
            m.invoke(obj, TokenHelper.newToken(sc));
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GenericDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException | InvocationTargetException ex) {
            Logger.getLogger(DAOSpecifGeneric.class.getName()).log(Level.SEVERE, null, ex);
        }
        return obj;
    }
    return null;
}

From source file:me.bird.util.Reflections.java

/**
 * , private/protected.//  w  w  w  .  j a  va 2s .  c  o  m
 * .
 */
public static Object invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes,
        final Object[] args) {
    Method method = MethodUtils.getAccessibleMethod(obj.getClass(), methodName, parameterTypes);
    if (method == null) {
        throw new IllegalArgumentException(
                "Could not find method [" + methodName + "] on target [" + obj + "]");
    }

    try {
        return method.invoke(obj, args);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java

@Test
public void invokeMethodWithNoArguments() {
    String testString = "HelloWorld";
    Method toStringMethod = MethodUtils.getAccessibleMethod(String.class, "toString", new Class<?>[0]);
    Transformer transformer = new MethodInvokingTransformer(toStringMethod);
    Object transform = transformer.transform(testString);
    assertNotNull(transform);// w  w  w .  ja v a  2s. c  o m
    assertTrue(transform instanceof String);
    String string = (String) transform;
    assertEquals("HelloWorld", string);
}

From source file:com.link_intersystems.lang.reflect.ReflectFacade.java

/**
 * A {@link Predicate} that evaluates to true if it is evaluated against
 * {@link Member} objects and the {@link Member#getName()} is equal to the
 * memberName this Predicate was constructed with.
 *
 * @return {@link Predicate} that evaluates to true if the {@link Member}'s
 *         name it is evaluated against is equal to the name that this
 *         {@link MemberNamePredicate} was constructed with.
 * @since 1.0.0.0/*from ww w. j  av a2  s .c o m*/
 */
public static Predicate getMemberNamePredicate(String memberName) {
    Assert.notNull("memberName", memberName);

    class SerializableMemberNamePredicate implements SerializableReference<Predicate> {

        private static final long serialVersionUID = -625148437829360498L;

        private transient Predicate predicate;

        private final String memberName;

        public SerializableMemberNamePredicate(String memberName) {
            this.memberName = memberName;
        }

        public Predicate get() {
            if (predicate == null) {
                Method memberGetNameMethod = MethodUtils.getAccessibleMethod(Member.class, "getName",
                        new Class<?>[0]);
                MethodInvokingTransformer member2NameTransformer = new MethodInvokingTransformer(
                        memberGetNameMethod);
                Predicate propertyValuePredicate = AndPredicate
                        .andPredicate(NotNullPredicate.notNullPredicate(), new EqualPredicate(memberName));
                Predicate instance = TransformedPredicate.transformedPredicate(member2NameTransformer,
                        propertyValuePredicate);
                Predicate memberNamePredicate = AndPredicate.andPredicate(NotNullPredicate.notNullPredicate(),
                        instance);
                this.predicate = memberNamePredicate;
            }
            return predicate;
        }
    }

    return new SerializablePredicate(new SerializableMemberNamePredicate(memberName));
}

From source file:com.offbynull.coroutines.instrumenter.asm.SearchUtilsTest.java

@Test
public void mustFindCallToPrintlnThroughMethodMatching() {
    MethodNode methodNode = findMethodsWithName(classNode.methods, "syncTest").get(0);
    List<AbstractInsnNode> insns = findInvocationsOf(methodNode.instructions,
            MethodUtils.getAccessibleMethod(PrintStream.class, "println", String.class));

    assertEquals(1, insns.size());//w  ww.  jav  a2s . c  om
    assertEquals("println", ((MethodInsnNode) insns.get(0)).name);
}

From source file:com.link_intersystems.lang.reflect.MethodInvokingTransformerTest.java

@Test(expected = IllegalStateException.class)
public void invokeInstanceMethodThrowsUnknownException() {
    String testString = "HelloWorld";
    Method toStringMethod = MethodUtils.getAccessibleMethod(String.class, "toString", new Class<?>[0]);
    Transformer transformer = new MethodInvokingTransformer(toStringMethod) {

        @Override/* ww  w . jav  a2  s  .c  o  m*/
        protected Object invokeInstanceMethod(Object targetObject) throws Exception {
            throw new IOException();
        }

    };
    transformer.transform(testString);
}