Search in sources :

Example 1 with Extension

use of org.fakereplace.Extension in project fakereplace by fakereplace.

the class IntegrationActivationTransformer method transform.

@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
    boolean modified = false;
    for (FakereplaceTransformer i : integrationTransformers) {
        if (i.transform(loader, className, classBeingRedefined, protectionDomain, file, classesToRetransform, changedClass, modifiedMethods)) {
            modified = true;
        }
    }
    if (trackedInstances.contains(file.getName())) {
        makeTrackedInstance(file);
        modified = true;
    }
    if (integrationClassTriggers.containsKey(className)) {
        modified = true;
        integrationClassloader.add(loader);
        // we need to load the class in another thread
        // otherwise it will not go through the javaagent
        final Extension extension = integrationClassTriggers.get(className);
        if (!loadedClassChangeAwares.contains(extension.getClassChangeAwareName())) {
            loadedClassChangeAwares.add(extension.getClassChangeAwareName());
            try {
                Class<?> clazz = Class.forName(extension.getClassChangeAwareName(), true, loader);
                final Object intance = clazz.newInstance();
                if (intance instanceof ClassChangeAware) {
                    ClassChangeNotifier.instance().add((ClassChangeAware) intance);
                }
                final String replaceableClassSelectorName = extension.getReplaceableClassSelectorName();
                if (replaceableClassSelectorName != null) {
                    final Class<?> envClass = Class.forName(replaceableClassSelectorName, true, loader);
                    final ReplaceableClassSelector selector = (ReplaceableClassSelector) envClass.newInstance();
                    Fakereplace.addReplaceableClassSelector(selector);
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    return modified;
}
Also used : Extension(org.fakereplace.Extension) ClassChangeAware(org.fakereplace.api.ClassChangeAware) ReplaceableClassSelector(org.fakereplace.ReplaceableClassSelector)

Example 2 with Extension

use of org.fakereplace.Extension in project fakereplace by fakereplace.

the class IntegrationActivationTransformer method transform.

@Override
public boolean transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file, Set<Class<?>> classesToRetransform, ChangedClassImpl changedClass, Set<MethodInfo> modifiedMethods, boolean replaceable) throws IllegalClassFormatException, BadBytecode, DuplicateMemberException {
    boolean modified = false;
    for (FakereplaceTransformer i : integrationTransformers) {
        if (i.transform(loader, className, classBeingRedefined, protectionDomain, file, classesToRetransform, changedClass, modifiedMethods, replaceable)) {
            modified = true;
        }
    }
    if (replaceable) {
        for (Extension i : extensions) {
            i.replaceableClassFileLoaded(className, loader);
        }
    }
    if (trackedInstances.contains(file.getName())) {
        makeTrackedInstance(file);
        modified = true;
    }
    if (integrationClassTriggers.containsKey(className)) {
        modified = true;
        integrationClassloader.add(loader);
        // we need to load the class in another thread
        // otherwise it will not go through the javaagent
        final Extension extension = integrationClassTriggers.get(className);
        if (!loadedClassChangeAwares.contains(extension.getClassChangeAwareName())) {
            loadedClassChangeAwares.add(extension.getClassChangeAwareName());
            try {
                Class<?> clazz = Class.forName(extension.getClassChangeAwareName(), true, loader);
                final Object intance = clazz.newInstance();
                if (intance instanceof ClassChangeAware) {
                    ClassChangeNotifier.instance().add((ClassChangeAware) intance);
                }
                final String replaceableClassSelectorName = extension.getReplaceableClassSelectorName();
                if (replaceableClassSelectorName != null) {
                    final Class<?> envClass = Class.forName(replaceableClassSelectorName, true, loader);
                    final ReplaceableClassSelector selector = (ReplaceableClassSelector) envClass.newInstance();
                    Fakereplace.addReplaceableClassSelector(selector);
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    return modified;
}
Also used : Extension(org.fakereplace.Extension) ClassChangeAware(org.fakereplace.api.ClassChangeAware) ReplaceableClassSelector(org.fakereplace.ReplaceableClassSelector)

Example 3 with Extension

use of org.fakereplace.Extension in project fakereplace by fakereplace.

the class Fakereplace method getIntegrationInfo.

private static Set<Extension> getIntegrationInfo(ClassLoader clr) {
    final ServiceLoader<Extension> loader = ServiceLoader.load(Extension.class, clr);
    final Set<Extension> integrations = new HashSet<>();
    for (Extension aLoader : loader) {
        integrations.add(aLoader);
    }
    return integrations;
}
Also used : Extension(org.fakereplace.Extension) HashSet(java.util.HashSet)

Example 4 with Extension

use of org.fakereplace.Extension in project fakereplace by fakereplace.

the class Fakereplace method premain.

public static void premain(java.lang.String s, java.lang.instrument.Instrumentation i) {
    AgentOptions.setup(s);
    inst = i;
    final Set<Extension> extension = getIntegrationInfo(ClassLoader.getSystemClassLoader());
    replaceableClassSelectors.add(DefaultReplaceableClassSelector.INSTANCE);
    // first we need to instrument the class loaders
    final Set<Class> cls = new HashSet<>();
    for (Class c : inst.getAllLoadedClasses()) {
        if (ClassLoader.class.isAssignableFrom(c)) {
            cls.add(c);
        }
    }
    final ClassLoaderTransformer classLoaderTransformer = new ClassLoaderTransformer();
    final MainTransformer mainTransformer = new MainTransformer();
    Fakereplace.mainTransformer = mainTransformer;
    inst.addTransformer(mainTransformer, true);
    mainTransformer.addTransformer(classLoaderTransformer);
    try {
        inst.retransformClasses(cls.toArray(EMPTY_CL_ARRAY));
    } catch (UnmodifiableClassException e) {
        e.printStackTrace();
    }
    mainTransformer.addTransformer(new IntegrationActivationTransformer(extension));
    mainTransformer.addTransformer(new AnnotationTransformer());
    mainTransformer.addTransformer(new FieldReplacementTransformer());
    mainTransformer.addTransformer(new MethodReplacementTransformer());
    mainTransformer.addTransformer(new Transformer());
    mainTransformer.addTransformer(new ClassInfoTransformer());
    mainTransformer.setRetransformationStarted(false);
    mainTransformer.setLogClassRetransformation(true);
}
Also used : FieldReplacementTransformer(org.fakereplace.replacement.FieldReplacementTransformer) MethodReplacementTransformer(org.fakereplace.replacement.MethodReplacementTransformer) AnnotationTransformer(org.fakereplace.replacement.AnnotationTransformer) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) Extension(org.fakereplace.Extension) AnnotationTransformer(org.fakereplace.replacement.AnnotationTransformer) FieldReplacementTransformer(org.fakereplace.replacement.FieldReplacementTransformer) AddedClass(org.fakereplace.replacement.AddedClass) MethodReplacementTransformer(org.fakereplace.replacement.MethodReplacementTransformer) HashSet(java.util.HashSet)

Aggregations

Extension (org.fakereplace.Extension)4 HashSet (java.util.HashSet)2 ReplaceableClassSelector (org.fakereplace.ReplaceableClassSelector)2 ClassChangeAware (org.fakereplace.api.ClassChangeAware)2 UnmodifiableClassException (java.lang.instrument.UnmodifiableClassException)1 AddedClass (org.fakereplace.replacement.AddedClass)1 AnnotationTransformer (org.fakereplace.replacement.AnnotationTransformer)1 FieldReplacementTransformer (org.fakereplace.replacement.FieldReplacementTransformer)1 MethodReplacementTransformer (org.fakereplace.replacement.MethodReplacementTransformer)1