Example usage for java.lang.reflect Constructor getModifiers

List of usage examples for java.lang.reflect Constructor getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Constructor getModifiers.

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:com.mayabot.thriftpool.utils.ReflectUtils.java

/**
 * ???/*w  w  w  .j  ava  2 s  .c  o m*/
 * 
 * @param clazz 
 * @param methodName ??method1(int, String)?????????method2
 * @return 
 * @throws NoSuchMethodException
 * @throws ClassNotFoundException  
 * @throws IllegalStateException ???????
 */
//   public static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes)
//           throws NoSuchMethodException, ClassNotFoundException {
//       String signature = clazz.getName() + "." + methodName;
//        if(parameterTypes != null && parameterTypes.length > 0){
//            signature += StringUtils.join(parameterTypes);
//        }
//        Method method = Signature_METHODS_CACHE.get(signature);
//        if(method != null){
//            return method;
//        }
//       if (parameterTypes == null) {
//            List<Method> finded = new ArrayList<Method>();
//            for (Method m : clazz.getMethods()) {
//                if (m.getName().equals(methodName)) {
//                    finded.add(m);
//                }
//            }
//            if (finded.isEmpty()) {
//                throw new NoSuchMethodException("No such method " + methodName + " in class " + clazz);
//            }
//            if(finded.size() > 1) {
//                String msg = String.format("Not unique method for method name(%s) in class(%s), find %d methods.",
//                        methodName, clazz.getName(), finded.size());
//                throw new IllegalStateException(msg);
//            }
//            method = finded.get(0);
//        } else {
//            Class<?>[] types = new Class<?>[parameterTypes.length];
//            for (int i = 0; i < parameterTypes.length; i ++) {
//                types[i] = ReflectUtils.name2class(parameterTypes[i]);
//            }
//            method = clazz.getMethod(methodName, types);
//            
//        }
//       Signature_METHODS_CACHE.put(signature, method);
//        return method;
//   }

//    public static Method findMethodByMethodName(Class<?> clazz, String methodName)
//          throws NoSuchMethodException, ClassNotFoundException {
//       return findMethodByMethodSignature(clazz, methodName, null);
//    }

public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException {
    Constructor<?> targetConstructor;
    try {
        targetConstructor = clazz.getConstructor(new Class<?>[] { paramType });
    } catch (NoSuchMethodException e) {
        targetConstructor = null;
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1
                    && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) {
                targetConstructor = constructor;
                break;
            }
        }
        if (targetConstructor == null) {
            throw e;
        }
    }
    return targetConstructor;
}

From source file:gobblin.runtime.cli.ConstructorAndPublicMethodsCliObjectFactory.java

private boolean canUseConstructor(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())) {
        return false;
    }/*w  w w . j  a  v a2  s  . c om*/
    if (!constructor.isAnnotationPresent(CliObjectSupport.class)) {
        return false;
    }
    for (Class<?> param : constructor.getParameterTypes()) {
        if (param != String.class) {
            return false;
        }
    }
    return constructor.getParameterTypes().length == constructor.getAnnotation(CliObjectSupport.class)
            .argumentNames().length;
}

From source file:gobblin.runtime.cli.ConstructorAndPublicMethodsGobblinCliFactory.java

private boolean canUseConstructor(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())) {
        return false;
    }/*  w w  w  . j av  a 2s  . c  om*/
    if (!constructor.isAnnotationPresent(EmbeddedGobblinCliSupport.class)) {
        return false;
    }
    for (Class<?> param : constructor.getParameterTypes()) {
        if (param != String.class) {
            return false;
        }
    }
    return constructor.getParameterTypes().length == constructor.getAnnotation(EmbeddedGobblinCliSupport.class)
            .argumentNames().length;
}

From source file:hu.bme.mit.sette.common.validator.reflection.ConstructorValidator.java

/**
 * Sets the required modifiers for the constructor.
 *
 * @param modifiers//from   ww w.  j  a  v a2 s. c o  m
 *            the required modifiers for the constructor
 * @return this object
 */
public ConstructorValidator withModifiers(final int modifiers) {
    if (getSubject() != null) {
        Constructor<?> constructor = getSubject();

        if ((constructor.getModifiers() & modifiers) != modifiers) {
            this.addException(String.format(
                    "The constructor must have all the " + "specified modifiers\n(modifiers: [%s])",
                    Modifier.toString(modifiers)));
        }
    }

    return this;
}

From source file:hu.bme.mit.sette.common.validator.reflection.ConstructorValidator.java

/**
 * Sets the prohibited modifiers for the constructor.
 *
 * @param modifiers//from   ww  w.j  a v  a  2s.  c  o  m
 *            the prohibited modifiers for the constructor.
 * @return this object
 */
public ConstructorValidator withoutModifiers(final int modifiers) {
    if (getSubject() != null) {
        Constructor<?> constructor = getSubject();

        if ((constructor.getModifiers() & modifiers) != 0) {
            this.addException(String.format(
                    "The constructor must not have any of " + "the specified modifiers\n(modifiers: [%s])",
                    Modifier.toString(modifiers)));
        }
    }

    return this;
}

From source file:com.glaf.core.util.ReflectUtils.java

public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException {
    Constructor<?> targetConstructor;
    try {// w ww  .  j a v a2s . co m
        targetConstructor = clazz.getConstructor(new Class<?>[] { paramType });
    } catch (NoSuchMethodException e) {
        targetConstructor = null;
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1
                    && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) {
                targetConstructor = constructor;
                break;
            }
        }
        if (targetConstructor == null) {
            throw e;
        }
    }
    return targetConstructor;
}

From source file:com.github.wnameless.jsonapi.JapisonTest.java

@Test
public void testPrivateConstruct() throws Exception {
    Constructor<JsonApi> c = JsonApi.class.getDeclaredConstructor();
    assertTrue(Modifier.isPrivate(c.getModifiers()));
    c.setAccessible(true);//from ww  w . jav  a 2s .co m
    c.newInstance();
}

From source file:org.jgentleframework.utils.ReflectUtils.java

/**
 * Make the given constructor accessible, explicitly setting it accessible
 * if necessary. The <code>setAccessible(true)</code> method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).//from ww w.  java  2s  .  c  om
 * 
 * @param ctor
 *            the constructor to make accessible
 * @see java.lang.reflect.Constructor#setAccessible(boolean)
 */
public static void makeAccessible(Constructor<?> ctor) {

    if (!Modifier.isPublic(ctor.getModifiers())
            || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) {
        ctor.setAccessible(true);
    }
}

From source file:org.sonar.java.se.NullableAnnotationUtilsTest.java

@Test
public void private_constructor() throws Exception {
    assertThat(Modifier.isFinal(NullableAnnotationUtils.class.getModifiers())).isTrue();
    Constructor<NullableAnnotationUtils> constructor = NullableAnnotationUtils.class.getDeclaredConstructor();
    assertThat(Modifier.isPrivate(constructor.getModifiers())).isTrue();
    assertThat(constructor.isAccessible()).isFalse();
    constructor.setAccessible(true);//from   w ww.j  a v a2s .c o  m
    constructor.newInstance();
}

From source file:net.krotscheck.util.ResourceUtilTest.java

/**
 * Ensure the constructor is private./*from  ww  w.  j  ava 2 s .c om*/
 *
 * @throws Exception Tests throw exceptions.
 */
@Test
public void testConstructorIsPrivate() throws Exception {
    Constructor<ResourceUtil> constructor = ResourceUtil.class.getDeclaredConstructor();
    Assert.assertTrue(Modifier.isPrivate(constructor.getModifiers()));

    // Override the private constructor and create an instance
    constructor.setAccessible(true);
    ResourceUtil util = constructor.newInstance();
    Assert.assertNotNull(util);
}