Example usage for java.lang ClassNotFoundException ClassNotFoundException

List of usage examples for java.lang ClassNotFoundException ClassNotFoundException

Introduction

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

Prototype

public ClassNotFoundException(String s) 

Source Link

Document

Constructs a ClassNotFoundException with the specified detail message.

Usage

From source file:org.kuali.rice.core.api.util.ShadowingInstrumentableClassLoader.java

private Class<?> loadAndInstrumentClass(String name) throws ClassNotFoundException {
    String internalName = StringUtils.replace(name, ".", "/") + ".class";
    attempted.add(name);/*w w  w  .j a  v a  2 s  . co m*/
    attempted.add(internalName);
    InputStream is = this.enclosingClassLoader.getResourceAsStream(internalName);
    if (is == null) {
        throw new ClassNotFoundException(name);
    }
    byte[] bytes;
    try {
        bytes = ByteStreams.toByteArray(is);
    } catch (IOException e) {
        throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", e);
    } finally {
        Closeables.closeQuietly(is);
    }
    bytes = weavingTransformer.transformIfNecessary(name, bytes);
    Class<?> cls = defineClass(name, bytes, 0, bytes.length);
    // Additional check for defining the package, if not defined yet.
    if (cls.getPackage() == null) {
        int packageSeparator = name.lastIndexOf('.');
        if (packageSeparator != -1) {
            String packageName = name.substring(0, packageSeparator);
            definePackage(packageName, null, null, null, null, null, null, null);
        }
    }
    this.classCache.put(name, cls);
    return cls;
}

From source file:roboguice.inject.ViewListener.java

private <I> void prepareViewMembersInjector(TypeEncounter<I> typeEncounter, Field field) {
    if (field.isAnnotationPresent(InjectView.class)) {
        if (Modifier.isStatic(field.getModifiers()))
            throw new UnsupportedOperationException("Views may not be statically injected");
        else if (!View.class.isAssignableFrom(field.getType()))
            throw new UnsupportedOperationException("You may only use @InjectView on fields that extend View");
        else if (Context.class.isAssignableFrom(field.getDeclaringClass())
                && !Activity.class.isAssignableFrom(field.getDeclaringClass()))
            throw new UnsupportedOperationException("You may only use @InjectView in Activity contexts");
        else {/*from   w  w  w.  j  a  v a 2s .co m*/
            final f<?, ?> utils = FragmentUtil.hasSupport && (FragmentUtil.supportActivity
                    .isAssignableFrom(field.getDeclaringClass())
                    || FragmentUtil.supportFrag.fragmentType().isAssignableFrom(field.getDeclaringClass()))
                            ? FragmentUtil.supportFrag
                            : FragmentUtil.nativeFrag;

            typeEncounter.register(new ViewMembersInjector<I>(field, field.getAnnotation(InjectView.class),
                    typeEncounter, utils));
        }
    } else if (field.isAnnotationPresent(InjectFragment.class)) {
        if (!FragmentUtil.hasNative && !FragmentUtil.hasSupport) {
            throw new RuntimeException(new ClassNotFoundException("No fragment classes were available"));
        } else if (Modifier.isStatic(field.getModifiers())) {
            throw new UnsupportedOperationException("Fragments may not be statically injected");

        } else {
            final boolean assignableFromNative = FragmentUtil.hasNative
                    && FragmentUtil.nativeFrag.fragmentType().isAssignableFrom(field.getType());
            final boolean assignableFromSupport = FragmentUtil.hasSupport
                    && FragmentUtil.supportFrag.fragmentType().isAssignableFrom(field.getType());
            final boolean isSupportActivity = FragmentUtil.hasSupport
                    && FragmentUtil.supportActivity.isAssignableFrom(field.getDeclaringClass());
            final boolean isNativeActivity = !isSupportActivity
                    && Activity.class.isAssignableFrom(field.getDeclaringClass());

            if (isNativeActivity && assignableFromNative || isSupportActivity && assignableFromSupport) {
                typeEncounter.register(new ViewMembersInjector<I>(field,
                        field.getAnnotation(InjectFragment.class), typeEncounter,
                        isNativeActivity ? FragmentUtil.nativeFrag : FragmentUtil.supportFrag));
            } else if (isNativeActivity && !assignableFromNative) {
                // Error messages - these filters are comprehensive. The
                // final else block will never execute.
                throw new UnsupportedOperationException(
                        "You may only use @InjectFragment in native activities if fields are descended from type android.app.Fragment");
            } else if (!isSupportActivity && !isNativeActivity) {
                throw new UnsupportedOperationException(
                        "You may only use @InjectFragment in Activity contexts");
            } else if (isSupportActivity && !assignableFromSupport) {
                throw new UnsupportedOperationException(
                        "You may only use @InjectFragment in support activities if fields are descended from type android.support.v4.app.Fragment");
            } else {
                throw new RuntimeException("This should never happen.");
            }
        }
    }
}

From source file:com.ottogroup.bi.asap.repository.ComponentClassloader.java

/**
 * Loads referenced class:/*  ww w  . ja  v  a  2 s.c  o m*/
 * <ul>
 *   <li>find in already loaded classes</li>
 *   <li>look it up in previously loaded and thus cached classes</li>
 *   <li>find it in jar files</li>
 *   <li>ask parent class loader</li>
 * </ul>
 * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
 */
public Class<?> loadClass(String name) throws ClassNotFoundException {

    synchronized (getClassLoadingLock(name)) {

        // check if class has already been loaded
        Class<?> clazz = findLoadedClass(name);
        if (clazz != null)
            return clazz;

        // check internal cache for already loaded classes
        clazz = cachedClasses.get(name);
        if (clazz != null) {
            return clazz;
        }

        // check if the managed jars contain the class
        if (classesJarMapping.containsKey(name))
            clazz = findClass(name);
        if (clazz != null) {
            return clazz;
        }

        // otherwise hand over the request to the parent class loader
        clazz = super.loadClass(name);
        if (clazz == null)
            throw new ClassNotFoundException("Class '" + name + "' not found");

        return clazz;
    }
}

From source file:com.galeoconsulting.leonardinius.api.impl.ChainingClassLoader.java

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
    for (ClassLoader classloader : classLoaders) {
        try {//from  w ww . j a  v  a 2 s . c  o  m
            return classloader.loadClass(name);
        } catch (ClassNotFoundException e) {
            // ignoring until we reach the end of the list since we are chaining
        }
    }
    throw new ClassNotFoundException(name);
}

From source file:org.beangle.model.persist.hibernate.internal.ChainedClassLoader.java

private Class<?> doLoadClass(String name) throws ClassNotFoundException {
    Class<?> clazz = doLoadClass(name, loaders);

    if (clazz != null) {
        return clazz;
    } else {/*from ww  w .  j  av  a2 s .  co  m*/
        clazz = doLoadClass(name, nonOsgiLoaders);
    }

    if (clazz != null) {
        return clazz;
    }

    if (parent != null) {
        return parent.loadClass(name);
    }

    else {
        throw new ClassNotFoundException(name);
    }
}

From source file:org.arsenal.framework.core.loader.ShadowingClassLoader.java

private Class doLoadClass(String name) throws ClassNotFoundException {
    String internalName = StringUtils.replace(name, ".", "/") + ".class";
    InputStream is = this.parentClassLoader.getResourceAsStream(internalName);
    if (is == null) {
        throw new ClassNotFoundException(name);
    }//from   ww  w  . java2 s.com
    try {

        byte[] bytes = XIOUtils.toByteArray(is);
        bytes = applyTransformers(name, bytes);
        Class cls = defineClass(name, bytes, 0, bytes.length);
        // Additional check for defining the package, if not defined yet.
        if (cls.getPackage() == null) {
            int packageSeparator = name.lastIndexOf('.');
            if (packageSeparator != -1) {
                String packageName = name.substring(0, packageSeparator);
                definePackage(packageName, null, null, null, null, null, null, null);
            }
        }
        this.classCache.put(name, cls);
        return cls;
    } catch (IOException ex) {
        throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", ex);
    }
}

From source file:de.micromata.genome.gwiki.plugin.CombinedClassLoader.java

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
    for (ClassLoader cl : parents) {
        try {/*from  w w w  . j  a v  a  2 s. c o m*/
            Class<?> cls = cl.loadClass(name);
            return cls;
        } catch (ClassNotFoundException ign) {
        } catch (NoClassDefFoundError ign2) {

        }

    }
    throw new ClassNotFoundException(name);
}

From source file:org.evosuite.junit.DetermineSUT.java

public Set<String> determineCalledClasses(String fullyQualifiedTargetClass, Set<String> targetClasses)
        throws ClassNotFoundException, NoJUnitClassException {
    Set<String> calledClasses = new HashSet<String>();

    String className = fullyQualifiedTargetClass.replace('.', '/');
    try {//w  w w  .  j a va2  s  . c  o m

        InputStream is = ClassLoader.getSystemResourceAsStream(className + ".class");
        if (is == null) {
            throw new ClassNotFoundException("Class '" + className + ".class"
                    + "' should be in target project, but could not be found!");
        }
        ClassReader reader = new ClassReader(is);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, ClassReader.SKIP_FRAMES);
        superClasses = getSuperClasses(classNode);

        if (isJUnitTest(classNode)) {
            handleClassNode(calledClasses, classNode, targetClasses);
        } else {
            throw new NoJUnitClassException();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    calledClasses.remove("java.lang.Object");
    calledClasses.remove(fullyQualifiedTargetClass);

    return calledClasses;
}

From source file:com.haulmont.cuba.core.sys.javacl.JavaClassLoader.java

@Override
public Class loadClass(final String fullClassName, boolean resolve) throws ClassNotFoundException {
    String containerClassName = StringUtils.substringBefore(fullClassName, "$");

    StopWatch loadingWatch = new Slf4JStopWatch("LoadClass");
    try {/*from  w  w w .  j a  v a 2  s.  c  o  m*/
        lock(containerClassName);
        Class clazz;

        if (!sourceProvider.getSourceFile(containerClassName).exists()) {
            clazz = super.loadClass(fullClassName, resolve);
            return clazz;
        }

        CompilationScope compilationScope = new CompilationScope(this, containerClassName);
        if (!compilationScope.compilationNeeded()) {
            TimestampClass timestampClass = getTimestampClass(fullClassName);
            if (timestampClass == null) {
                throw new ClassNotFoundException(fullClassName);
            }
            return timestampClass.clazz;
        }

        String src;
        try {
            src = sourceProvider.getSourceString(containerClassName);
        } catch (IOException e) {
            throw new ClassNotFoundException("Could not load java sources for class " + containerClassName);
        }

        try {
            log.debug("Compiling " + containerClassName);
            final DiagnosticCollector<JavaFileObject> errs = new DiagnosticCollector<>();

            SourcesAndDependencies sourcesAndDependencies = new SourcesAndDependencies(rootDir, this);
            sourcesAndDependencies.putSource(containerClassName, src);
            sourcesAndDependencies.collectDependencies(containerClassName);
            Map<String, CharSequence> sourcesForCompilation = sourcesAndDependencies
                    .collectSourcesForCompilation(containerClassName);

            @SuppressWarnings("unchecked")
            Map<String, Class> compiledClasses = createCompiler().compile(sourcesForCompilation, errs);

            Map<String, TimestampClass> compiledTimestampClasses = wrapCompiledClasses(compiledClasses);
            compiled.putAll(compiledTimestampClasses);
            linkDependencies(compiledTimestampClasses, sourcesAndDependencies.dependencies);

            clazz = compiledClasses.get(fullClassName);

            springBeanLoader.updateContext(compiledClasses.values());

            return clazz;
        } catch (Exception e) {
            proxyClassLoader.restoreRemoved();
            throw new RuntimeException(e);
        } finally {
            proxyClassLoader.cleanupRemoved();
        }
    } finally {
        unlock(containerClassName);
        loadingWatch.stop();
    }
}

From source file:mzmatch.util.Tool.java

/**
 * /* w  w w  .  j a v a 2s.c om*/
 * 
 * @param pckgname
 * @return
 * @throws ClassNotFoundException
 */
public static Vector<Class<? extends Object>> getAllClasses(String pckgname) throws ClassNotFoundException {
    Vector<Class<? extends Object>> classes = new Vector<Class<? extends Object>>();

    // load the current package
    ClassLoader cld = Thread.currentThread().getContextClassLoader();
    if (cld == null)
        throw new ClassNotFoundException("Can't get class loader.");

    String path = pckgname.replace('.', '/');
    URL resource = cld.getResource(path);
    if (resource == null)
        throw new ClassNotFoundException("No resource for " + path);

    // parse the directory
    File directory = new File(resource.getFile());
    if (directory.isDirectory() && directory.exists()) {
        for (File f : directory.listFiles()) {
            if (f.getName().endsWith(".class"))
                classes.add(Class.forName(pckgname + '.' + f.getName().substring(0, f.getName().length() - 6)));
        }
        for (File f : directory.listFiles()) {
            if (f.isDirectory())
                classes.addAll(getAllClasses(pckgname + "." + f.getName()));
        }
    } else
        throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");

    return classes;
}