All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.classGeneration.ImplementationClass Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for automated developer testing. It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external APIs; JUnit (4 & 5) and TestNG test runners are supported. It also contains an advanced code coverage tool.

The newest version!
/*
 * Copyright (c) 2006 JMockit developers
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.classGeneration;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

import java.lang.reflect.Type;

import mockit.asm.classes.ClassReader;
import mockit.asm.classes.ClassVisitor;
import mockit.internal.ClassFile;
import mockit.internal.util.ClassLoad;
import mockit.internal.util.GeneratedClasses;
import mockit.internal.util.Utilities;

/**
 * Allows the creation of new implementation classes for interfaces and abstract classes.
 */
public abstract class ImplementationClass {
    @NonNull
    protected final Class sourceClass;
    @NonNull
    protected String generatedClassName;
    @Nullable
    private byte[] generatedBytecode;

    protected ImplementationClass(@NonNull Type mockedType) {
        this(Utilities.getClassType(mockedType));
    }

    protected ImplementationClass(@NonNull Class mockedClass) {
        this(mockedClass, GeneratedClasses.getNameForGeneratedClass(mockedClass, null));
    }

    protected ImplementationClass(@NonNull Class sourceClass, @NonNull String desiredClassName) {
        this.sourceClass = sourceClass;
        generatedClassName = desiredClassName;
    }

    @NonNull
    public final Class generateClass() {
        ClassReader classReader = ClassFile.createReaderOrGetFromCache(sourceClass);

        ClassVisitor modifier = createMethodBodyGenerator(classReader);
        classReader.accept(modifier);

        return defineNewClass(modifier);
    }

    @NonNull
    protected abstract ClassVisitor createMethodBodyGenerator(@NonNull ClassReader cr);

    @NonNull
    private Class defineNewClass(@NonNull ClassVisitor modifier) {
        final ClassLoader parentLoader = ClassLoad.getClassLoaderWithAccess(sourceClass);
        final byte[] modifiedClassfile = modifier.toByteArray();

        try {
            @SuppressWarnings("unchecked")
            Class generatedClass = (Class) new ClassLoader(parentLoader) {
                @Override
                protected Class findClass(String name) throws ClassNotFoundException {
                    if (!name.equals(generatedClassName)) {
                        return parentLoader.loadClass(name);
                    }

                    return defineClass(name, modifiedClassfile, 0, modifiedClassfile.length);
                }
            }.findClass(generatedClassName);
            generatedBytecode = modifiedClassfile;

            return generatedClass;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Unable to define class: " + generatedClassName, e);
        }
    }

    @Nullable
    public final byte[] getGeneratedBytecode() {
        return generatedBytecode;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy