Package java.lang.instrument

Examples of java.lang.instrument.Instrumentation


        final Map<Class,byte[]> classes) {
        Log log = conf.getLog(OpenJPAConfiguration.LOG_ENHANCE);
        if (classes == null || classes.size() == 0)
            return;

        Instrumentation inst = null;
        ClassFileTransformer t = null;
        try {
            inst = InstrumentationFactory.getInstrumentation();

            Class[] array = classes.keySet().toArray(new Class[classes.size()]);
            if (JavaVersions.VERSION >= 6) {
                log.trace(_loc.get("retransform-types", classes.keySet()));

                t = new ClassFileTransformer() {
                    public byte[] transform(ClassLoader loader, String clsName,
                        Class<?> classBeingRedefined, ProtectionDomain pd,
                        byte[] classfileBuffer) {
                        return classes.get(classBeingRedefined);
                    }
                };
               
                // these are Java 6 methods, and we don't have a Java 6 build
                // module yet. The cost of reflection here is negligible
                // compared to the redefinition / enhancement costs in total,
                // so this should not be a big problem.
                Method meth = inst.getClass().getMethod("addTransformer",
                    new Class[] { ClassFileTransformer.class, boolean.class });
                meth.invoke(inst, new Object[] { t, true });
                meth = inst.getClass().getMethod("retransformClasses",
                    new Class[] { array.getClass() });
                meth.invoke(inst, new Object[] { array });
            } else {
                log.trace(_loc.get("redefine-types", classes.keySet()));
                // in a Java 5 context, we can use class redefinition instead
                ClassDefinition[] defs = new ClassDefinition[array.length];
                for (int i = 0; i < defs.length; i++)
                    defs[i] = new ClassDefinition(array[i],
                        classes.get(array[i]));
                inst.redefineClasses(defs);
            }
        } catch (NoSuchMethodException e) {
            throw new InternalException(e);
        } catch (IllegalAccessException e) {
            throw new InternalException(e);
        } catch (InvocationTargetException e) {
            throw new UserException(e.getCause());
        } catch (IOException e) {
            throw new InternalException(e);
        } catch (ClassNotFoundException e) {
            throw new InternalException(e);
        } catch (UnmodifiableClassException e) {
            throw new InternalException(e);
        } finally {
            if (inst != null && t != null)
                inst.removeTransformer(t);
        }
    }
View Full Code Here


     * if retransformation is possible.
     */
    public static boolean canRedefineClasses() {
        if (_canRedefine == null) {
            try {
                Instrumentation inst = InstrumentationFactory
                    .getInstrumentation();
                if (inst == null) {
                    _canRedefine = Boolean.FALSE;
                } else if (JavaVersions.VERSION == 5) {
                    // if inst is non-null and we're using Java 5,
View Full Code Here

    private EntityManagerFactory createEntityManagerFactory() throws Exception {
        PersistenceClassLoaderHandler persistenceClassLoaderHandler = new PersistenceClassLoaderHandler() {

            public void addTransformer(String unitId, ClassLoader classLoader, ClassFileTransformer classFileTransformer) {
                Instrumentation instrumentation = Agent.getInstrumentation();
                instrumentation.addTransformer(classFileTransformer);
            }

            public void destroy(String unitId) {
            }
View Full Code Here

        }
    }

    private static class Handler implements PersistenceClassLoaderHandler {
        public void addTransformer(String unitId, ClassLoader classLoader, ClassFileTransformer classFileTransformer) {
            Instrumentation instrumentation = Agent.getInstrumentation();
            if (instrumentation != null) {
                instrumentation.addTransformer(new Transformer(classFileTransformer));
            }
        }
View Full Code Here

    private static class PersistenceClassLoaderHandlerImpl implements PersistenceClassLoaderHandler {
        private final Map<String,List<ClassFileTransformer>> transformers = new TreeMap<String,List<ClassFileTransformer>>();

        public void addTransformer(String unitId, ClassLoader classLoader, ClassFileTransformer classFileTransformer) {
            Instrumentation instrumentation = Agent.getInstrumentation();
            if (instrumentation != null) {
                instrumentation.addTransformer(classFileTransformer);

                if (unitId != null) {
                    List<ClassFileTransformer> transformers = this.transformers.get(unitId);
                    if (transformers == null) {
                        transformers = new ArrayList<ClassFileTransformer>(1);
View Full Code Here

        }

        public void destroy(String unitId) {
            List<ClassFileTransformer> transformers = this.transformers.remove(unitId);
            if (transformers != null) {
                Instrumentation instrumentation = Agent.getInstrumentation();
                if (instrumentation != null) {
                    for (ClassFileTransformer transformer : transformers) {
                        instrumentation.removeTransformer(transformer);
                    }
                } else {
                    logger.error("assembler.noAgent");
                }
            }
View Full Code Here

    private static class PersistenceClassLoaderHandlerImpl implements PersistenceClassLoaderHandler {
        private final Map<String,List<ClassFileTransformer>> transformers = new TreeMap<String, List<ClassFileTransformer>> ();

        public void addTransformer(String unitId, ClassLoader classLoader, ClassFileTransformer classFileTransformer) {
            Instrumentation instrumentation = Agent.getInstrumentation();
            if (instrumentation != null) {
                instrumentation.addTransformer(classFileTransformer);

                if (unitId != null) {
                    List<ClassFileTransformer> transformers = this.transformers.get(unitId);
                    if (transformers == null) {
                        transformers = new ArrayList<ClassFileTransformer>(1);
View Full Code Here

        }

        public void destroy(String unitId) {
            List<ClassFileTransformer> transformers = this.transformers.remove(unitId);
            if (transformers != null) {
                Instrumentation instrumentation = Agent.getInstrumentation();
                if (instrumentation != null) {
                    for (ClassFileTransformer transformer : transformers) {
                        instrumentation.removeTransformer(transformer);
                    }
                } else {
                    logger.error("assembler.noAgent");
                }
            }
View Full Code Here

        }
    }

    private static class Handler implements PersistenceClassLoaderHandler {
        public void addTransformer(String unitId, ClassLoader classLoader, ClassFileTransformer classFileTransformer) {
            Instrumentation instrumentation = Agent.getInstrumentation();
            if (instrumentation != null) {
                instrumentation.addTransformer(new Transformer(classFileTransformer));
            }
        }
View Full Code Here

     */
//    @Test
    public void testPremain() {
        System.out.println("premain");
        String agentArgs = "";
        Instrumentation inst = null;
        UnitTestTransformer.premain(agentArgs, inst);
        // TODO 检查生成的测试代码并删除失败的默认调用。
        fail("\u6d4b\u8bd5\u7528\u4f8b\u4e3a\u539f\u578b\u3002");
    }
View Full Code Here

TOP

Related Classes of java.lang.instrument.Instrumentation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.