Example usage for java.lang.reflect Constructor getDeclaringClass

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

Introduction

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

Prototype

@Override
public Class<T> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class that declares the constructor represented by this object.

Usage

From source file:org.firesoa.common.util.LocalVariableTableParameterNameDiscoverer.java

/**
 * Visit the given constructor and discover its parameter names.
 *//*  ww  w  .  ja  v a2 s.c om*/
private ParameterNameDiscoveringVisitor visitConstructor(Constructor ctor) throws IOException {
    ClassReader classReader = createClassReader(ctor.getDeclaringClass());
    FindConstructorParameterNamesClassVisitor classVisitor = new FindConstructorParameterNamesClassVisitor(
            ctor);
    classReader.accept(classVisitor, false);
    return classVisitor;
}

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 w  w w  .j  a  va2 s. c o m*/
 * 
 * @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:net.abhinavsarkar.spelhelper.SpelHelper.java

/**
 * Registers the public constructors of the class `clazz` so that they
 * can be called by their simple name from SpEL expressions.
 * @param clazz The class to register the constructors from.
 * @return      The current instance of SpelHelper. This is for chaining
 * the methods calls./*from   ww w  .j  a v  a  2  s  .c  o  m*/
 */
public SpelHelper registerConstructorsFromClass(final Class<?> clazz) {
    for (Constructor<?> constructor : asList(clazz.getConstructors())) {
        registeredConstructors.put(constructor.getDeclaringClass().getSimpleName()
                + Arrays.toString(constructor.getParameterTypes()), constructor);
    }
    return this;
}

From source file:it.cnr.icar.eric.client.xml.registry.InfomodelFactory.java

/**
 * Calls newInstance() on the constructor w/ the given initArgs. Returns the
 * new instance, or, in case of exception, log.warn and return null.
 *
 * @param objectType String, used for log only.
 * @param initArgs Objects to be used when calling newInstance().
 * @return new instance; or null in case of exception.
 *///from ww w .ja v  a  2s. c o m
private Object instantiate(String objectType, Constructor<?> c, Object initArgs[]) {
    try {
        return c.newInstance(initArgs);
    } catch (Exception e) {
        Object[] objs = { c.getDeclaringClass().getName(), objectType };
        String msg = i18nUtil.getString("extension.instantiation.failure", objs);
        log.warn(msg, e);
    }
    return null;
}

From source file:com.gh.bmd.jrt.android.v4.core.DefaultContextRoutine.java

@Nonnull
@Override//  w w w . ja  v a2 s  .c  om
protected Invocation<INPUT, OUTPUT> newInvocation(final boolean async) {

    final Logger logger = getLogger();

    if (async) {

        return new LoaderInvocation<INPUT, OUTPUT>(mContext, mInvocationId, mClashResolutionType,
                mCacheStrategyType, mConstructor, mArgs, mOrderType, logger);
    }

    final Object context = mContext.get();

    if (context == null) {

        throw new IllegalStateException("the routine context has been destroyed");
    }

    final Context appContext;

    if (context instanceof FragmentActivity) {

        final FragmentActivity activity = (FragmentActivity) context;
        appContext = activity.getApplicationContext();

    } else if (context instanceof Fragment) {

        final Fragment fragment = (Fragment) context;
        appContext = fragment.getActivity().getApplicationContext();

    } else {

        throw new IllegalArgumentException("invalid context type: " + context.getClass().getCanonicalName());
    }

    try {

        final Constructor<? extends ContextInvocation<INPUT, OUTPUT>> constructor = mConstructor;
        logger.dbg("creating a new instance of class: %s", constructor.getDeclaringClass());
        final ContextInvocation<INPUT, OUTPUT> invocation = constructor.newInstance(mArgs);
        invocation.onContext(appContext);
        return invocation;

    } catch (final RoutineException e) {

        logger.err(e, "error creating the invocation instance");
        throw e;

    } catch (final Throwable t) {

        logger.err(t, "error creating the invocation instance");
        throw new InvocationException(t);
    }
}

From source file:org.grails.datastore.gorm.finders.DynamicFinder.java

protected MethodExpression findMethodExpression(Class clazz, String expression) {
    MethodExpression me = null;// www .j  av a2 s  . c o  m
    final Matcher matcher = methodExpressinPattern.matcher(expression);

    if (matcher.find()) {
        Constructor constructor = methodExpressions.get(matcher.group(1));
        try {
            me = (MethodExpression) constructor.newInstance(clazz,
                    calcPropertyName(expression, constructor.getDeclaringClass().getSimpleName()));
        } catch (Exception e) {
            // ignore
        }
    }
    if (me == null) {
        me = new Equal(clazz, calcPropertyName(expression, Equal.class.getSimpleName()));
    }

    return me;
}

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

/**
 * Calls a constructor with a set of arguments. After execution the stack should have an extra item pushed on it: the object that was
 * created by this constructor./*www .  ja  v  a  2s . c om*/
 * @param constructor constructor to call
 * @param args constructor argument instruction lists -- each instruction list must leave one item on the stack of the type expected
 * by the constructor
 * @return instructions to invoke a constructor
 * @throws NullPointerException if any argument is {@code null} or array contains {@code null}
 * @throws IllegalArgumentException if the length of {@code args} doesn't match the number of parameters in {@code constructor}
 */
public static InsnList construct(Constructor<?> constructor, InsnList... args) {
    Validate.notNull(constructor);
    Validate.notNull(args);
    Validate.noNullElements(args);
    Validate.isTrue(constructor.getParameterCount() == args.length);

    InsnList ret = new InsnList();

    Type clsType = Type.getType(constructor.getDeclaringClass());
    Type methodType = Type.getType(constructor);

    ret.add(new TypeInsnNode(Opcodes.NEW, clsType.getInternalName()));
    ret.add(new InsnNode(Opcodes.DUP));
    for (InsnList arg : args) {
        ret.add(arg);
    }
    ret.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, clsType.getInternalName(), "<init>",
            methodType.getDescriptor(), false));

    return ret;
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @return <code>true</code> if two given {@link Constructor}-s represent same constructor.
 *//*from w  w  w . jav a 2s  .  co m*/
public static boolean equals(Constructor<?> constructor_1, Constructor<?> constructor_2) {
    if (constructor_1 == constructor_2) {
        return true;
    }
    return constructor_1.getDeclaringClass() == constructor_2.getDeclaringClass() && ObjectUtils
            .equals(getConstructorSignature(constructor_1), getConstructorSignature(constructor_2));
}

From source file:it.cnr.icar.eric.client.xml.registry.InfomodelFactoryTest.java

/**
 * Test of getInstance method, of class it.cnr.icar.eric.client.xml.registry.InfomodelFactory.
 *//*from  ww  w. ja  va  2  s  .  com*/
public void testGetInstance() {
    System.out.println("\ntestGetInstance");
    InfomodelFactory extUtil = InfomodelFactory.getInstance();
    assertNotNull(extUtil);

    Constructor<?> c1, c2, c3, c4;
    c1 = extUtil.getConstructor1Arg(EO_EXT_UUID);
    c2 = extUtil.getConstructor2Args(EO_EXT_UUID);
    c3 = extUtil.getConstructor1Arg(EO_NICKNAME);
    c4 = extUtil.getConstructor2Args(EO_NICKNAME);
    assertNotNull(c1);
    assertNotNull(c2);
    assertNotNull(c3);
    assertNotNull(c4);
    assertEquals("Configured class differs.", EO_EXT_CLASSNAME, c1.getDeclaringClass().getName());
    assertEquals("Configured class differs.", EO_EXT_CLASSNAME, c2.getDeclaringClass().getName());
    assertEquals("Configured class differs.", EO_EXT_CLASSNAME, c3.getDeclaringClass().getName());
    assertEquals("Configured class differs.", EO_EXT_CLASSNAME, c4.getDeclaringClass().getName());

    c1 = extUtil.getConstructor1Arg(ASS_EXT_UUID);
    c2 = extUtil.getConstructor2Args(ASS_EXT_UUID);
    c3 = extUtil.getConstructor1Arg(ASS_NICKNAME);
    c4 = extUtil.getConstructor2Args(ASS_NICKNAME);
    assertNotNull(c1);
    assertNotNull(c2);
    assertNotNull(c3);
    assertNotNull(c4);
    assertEquals("Configured class differs.", ASS_EXT_CLASSNAME, c1.getDeclaringClass().getName());
    assertEquals("Configured class differs.", ASS_EXT_CLASSNAME, c2.getDeclaringClass().getName());
    assertEquals("Configured class differs.", ASS_EXT_CLASSNAME, c3.getDeclaringClass().getName());
    assertEquals("Configured class differs.", ASS_EXT_CLASSNAME, c4.getDeclaringClass().getName());
}

From source file:org.evosuite.setup.TestClusterGenerator.java

protected static void makeAccessible(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }/*from ww w.  ja v a 2  s.co m*/
}