Example usage for java.lang.reflect Constructor getClass

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.grouplens.grapht.reflect.internal.ClassInstantiator.java

@Override
public Object instantiate() throws ConstructionException {
    // find constructor and build up necessary constructor arguments

    Constructor<?> ctor = getConstructor();
    LogContext globalLogContext = LogContext.create();
    Object instance = null;//from ww w .  j  ava 2 s.c o m
    Method[] methods;

    try {
        // create the instance that we are injecting
        try {
            globalLogContext.put("org.grouplens.grapht.class", ctor.getClass().toString());
            Object[] ctorArgs = new Object[ctor.getParameterTypes().length];
            for (Desire d : desires) {
                LogContext ipContext = LogContext.create();
                if (d.getInjectionPoint() instanceof ConstructorParameterInjectionPoint) {
                    // this desire is a constructor argument so create it now
                    Instantiator provider = providers.get(d);
                    ConstructorParameterInjectionPoint cd = (ConstructorParameterInjectionPoint) d
                            .getInjectionPoint();
                    logger.trace("Injection point satisfactions in progress {}", cd);
                    try {
                        ipContext.put("org.grouplens.grapht.injectionPoint", cd.toString());
                    } finally {
                        ipContext.finish();
                    }
                    ctorArgs[cd.getParameterIndex()] = checkNull(cd, provider.instantiate());
                }
            }
            logger.trace("Invoking constructor {} with arguments {}", ctor, ctorArgs);
            ctor.setAccessible(true);
            instance = ctor.newInstance(ctorArgs);
        } catch (InvocationTargetException e) {
            throw new ConstructionException(ctor, "Constructor " + ctor + " failed", e);
        } catch (InstantiationException e) {
            throw new ConstructionException(ctor, "Could not instantiate " + type, e);
        } catch (IllegalAccessException e) {
            throw new ConstructionException(ctor, "Access violation on " + ctor, e);
        }

        // satisfy dependencies in the order of the list, which was
        // prepared to comply with JSR 330
        Map<Method, InjectionArgs> settersAndArguments = new HashMap<Method, InjectionArgs>();
        for (Desire d : desires) {
            LogContext ipContext = LogContext.create();
            try {
                final InjectionStrategy injectionStrategy = InjectionStrategy
                        .forInjectionPoint(d.getInjectionPoint());
                ipContext.put("org.grouplens.grapht.injectionPoint", d.getInjectionPoint().toString());
                injectionStrategy.inject(d.getInjectionPoint(), instance, providers.get(d),
                        settersAndArguments);
            } finally {
                ipContext.finish();
            }
        }
    } finally {
        globalLogContext.finish();
    }
    if (manager != null) {
        manager.registerComponent(instance);
    }

    methods = MethodUtils.getMethodsWithAnnotation(type, PostConstruct.class);
    for (Method method : methods) {
        method.setAccessible(true);
        try {
            method.invoke(instance);
        } catch (InvocationTargetException e) {
            throw new ConstructionException("Exception throw by " + method, e);
        } catch (IllegalAccessException e) {
            throw new ConstructionException("Access violation invoking " + method, e);
        }
    }

    // the instance has been fully configured
    return instance;
}

From source file:org.rapidcontext.core.storage.RootStorage.java

/**
 * Initializes an object with the corresponding object type (if
 * found)./*from   ww w  .  j a  v a  2 s .c om*/
 *
 * @param id             the object id
 * @param dict           the dictionary data
 *
 * @return the StorableObject instance created, or
 *         the input dictionary if no type matched
 */
private Object initObject(String id, Dict dict) {
    Constructor constr = Type.constructor(this, dict);
    if (constr != null) {
        String typeId = dict.getString(KEY_TYPE, null);
        Object[] args = new Object[] { id, typeId, dict };
        try {
            StorableObject obj = (StorableObject) constr.newInstance(args);
            obj.init();
            obj.activate();
            return obj;
        } catch (Exception e) {
            String msg = "failed to create instance of " + constr.getClass().getName() + " for object " + id
                    + " of type " + typeId;
            LOG.log(Level.WARNING, msg, e);
            dict.add("_error", msg);
            return dict;
        }
    }
    return dict;
}