Example usage for java.lang Class getClassLoader

List of usage examples for java.lang Class getClassLoader

Introduction

In this page you can find the example usage for java.lang Class getClassLoader.

Prototype

@CallerSensitive
@ForceInline 
public ClassLoader getClassLoader() 

Source Link

Document

Returns the class loader for the class.

Usage

From source file:com.amazonaws.hal.client.ConversionUtil.java

private static Object convertFromMap(Type type, Map value) {
    if (type instanceof Class && !Map.class.isAssignableFrom((Class) type)) {
        Class typeClass = (Class) type;

        return Proxy.newProxyInstance(typeClass.getClassLoader(), new Class<?>[] { typeClass },
                new MapBackedInvocationHandler(type, value));
    } else {//from  ww  w .java  2s. c  om
        return new ConvertingMap(getCollectionType(type, 1, Object.class), value);
    }
}

From source file:com.shelfmap.stepsfinder.CandidateStepsFactory.java

public static URL codeLocationFromParentPackage(Class<?> codeLocationClass) {
    String simpleName = codeLocationClass.getSimpleName() + ".class";
    String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class";
    URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass);
    String codeLocationPath = removeEnd(classResource.getFile(), simpleName);
    return codeLocationFromPath(codeLocationPath);
}

From source file:net.solarnetwork.node.util.ClassUtils.java

/**
 * Load a class of a particular type./*from  w ww  .j a  v  a 2s  .  c om*/
 * 
 * <p>
 * This uses the {@code type}'s ClassLoader to load the class. If that is
 * not available, it will use the current thread's context class loader.
 * </p>
 * 
 * @param <T>
 *        the desired interface type
 * @param className
 *        the class name that implements the interface
 * @param type
 *        the desired interface
 * @return the class
 */
public static <T> Class<? extends T> loadClass(String className, Class<T> type) {
    try {
        ClassLoader loader = type.getClassLoader();
        if (loader == null) {
            loader = Thread.currentThread().getContextClassLoader();
        }
        Class<?> clazz = loader.loadClass(className);
        if (!type.isAssignableFrom(clazz)) {
            throw new RuntimeException("Class [" + clazz + "] is not a [" + type + ']');
        }
        return clazz.asSubclass(type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to load class [" + className + ']', e);
    }
}

From source file:com.stratuscom.harvester.Utils.java

public static void logGrantsToClass(final Logger log, final Level level, final Class c) {
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        public Object run() {
            ClassLoader cl = c.getClassLoader();
            DynamicPolicyProvider dpp = (DynamicPolicyProvider) Policy.getPolicy();
            Permission[] perms = dpp.getGrants(c, null);
            log.log(level, MessageNames.GRANTS_TO_CLASS_ARE, new Object[] { c.getName(), Utils.format(perms) });
            return null;
        }// w  ww .  jav a2  s  .c o m
    });
}

From source file:org.fingerprintsoft.vitunit.test.VitUnitTestCase.java

protected static DataSource loadDatasource(final Class<? extends VitUnitTestCase> clazz) {

    DataSourceConfiguration annotation = clazz.getAnnotation(DataSourceConfiguration.class);

    String jdbcPropertiesFile = annotation.jdbcPropertiesFile();
    InputStream resourceAsStream = clazz.getClassLoader().getResourceAsStream(jdbcPropertiesFile);
    Properties properties = new Properties();
    try {//from   w  ww .j  av a 2  s .com
        properties.load(resourceAsStream);
    } catch (IOException e) {
        throw new SetupException("Could not load properties.", e);
    }

    try {
        return BasicDataSourceFactory.createDataSource(properties);
    } catch (Exception e) {
        throw new SetupException("Could not create datasource.", e);
    }
}

From source file:ch.citux.td.ui.fragments.TDFragment.java

public static <T extends Fragment> T instantiate(Class<T> clazz, Bundle args) {
    try {/*  www  . j ava 2 s  .  c  om*/
        T fragment = clazz.newInstance();
        if (args != null) {
            args.setClassLoader(clazz.getClassLoader());
            fragment.setArguments(args);
        }
        return fragment;
    } catch (Exception e) {
        throw new InstantiationException("Unable to instantiate fragment " + clazz
                + ": make sure class name exists, is public, and has an" + " empty constructor that is public",
                e);
    }
}

From source file:Main.java

public static InputStream getResourceAsStream(String name, Class clazz) {
    ClassLoader loader;//w w  w  .  j  a va 2 s.c  om
    InputStream retval = null;

    // https://issues.jboss.org/browse/JGRP-1762: load the classloader from the defining class first
    if (clazz != null) {
        try {
            loader = clazz.getClassLoader();
            if (loader != null) {
                retval = loader.getResourceAsStream(name);
                if (retval != null)
                    return retval;
            }
        } catch (Throwable t) {
        }
    }

    try {
        loader = Thread.currentThread().getContextClassLoader();
        if (loader != null) {
            retval = loader.getResourceAsStream(name);
            if (retval != null)
                return retval;
        }
    } catch (Throwable t) {
    }

    try {
        loader = ClassLoader.getSystemClassLoader();
        if (loader != null) {
            return loader.getResourceAsStream(name);
        }
    } catch (Throwable t) {
    }

    return retval;
}

From source file:Main.java

/**
 * Loads a class from the classloader; //  w w  w. j  a v a2s .  co m
 * If not found, the classloader of the {@code context} class specified will be used.
 * If the flag {@code checkParent} is true, the classloader's parent is included in 
 * the lookup.
 */
static Class<?> loadClass(String className, Class<?> context, boolean checkParent) {
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
        if (context != null) {
            ClassLoader loader = context.getClassLoader();
            while (loader != null) {
                try {
                    clazz = loader.loadClass(className);
                    return clazz;
                } catch (ClassNotFoundException e1) {
                    loader = checkParent ? loader.getParent() : null;
                }
            }
        }
    }
    return clazz;
}

From source file:org.apache.axiom.util.stax.dialect.StAXDialectDetector.java

private static URL getRootUrlForClass(Class cls) {
    return getRootUrlForResource(cls.getClassLoader(), cls.getName().replace('.', '/') + ".class");
}

From source file:io.syndesis.runtime.Recordings.java

static public <T> T recorder(Object object, Class<T> as) {
    if (as.isInterface()) {
        // If it's just an interface, use standard java reflect proxying
        return as.cast(Proxy.newProxyInstance(as.getClassLoader(), new Class[] { as },
                new RecordingInvocationHandler(object)));
    }//w  ww .  j  av  a2s  . c o m

    // If it's a class then use gclib to implement a subclass to implement proxying
    RecordingInvocationHandler ih = new RecordingInvocationHandler(object);
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(as);
    enhancer.setInterfaces(new Class[] { RecordingProxy.class });
    enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            return ih.invoke(o, method, objects);
        }
    });
    return as.cast(enhancer.create());
}