本文整理汇总了Java中java.lang.instrument.UnmodifiableClassException类的典型用法代码示例。如果您正苦于以下问题:Java UnmodifiableClassException类的具体用法?Java UnmodifiableClassException怎么用?Java UnmodifiableClassException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnmodifiableClassException类属于java.lang.instrument包,在下文中一共展示了UnmodifiableClassException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTransformer
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
/**
* 初始化类字节码的转换器
*
* @param inst 用于管理字节码转换器
*/
private static void initTransformer(Instrumentation inst) throws UnmodifiableClassException {
LinkedList<Class> retransformClasses = new LinkedList<Class>();
CustomClassTransformer customClassTransformer = new CustomClassTransformer();
inst.addTransformer(customClassTransformer, true);
Class[] loadedClasses = inst.getAllLoadedClasses();
for (Class clazz : loadedClasses) {
for (final AbstractClassHook hook : customClassTransformer.getHooks()) {
if (hook.isClassMatched(clazz.getName().replace(".", "/"))) {
if (inst.isModifiableClass(clazz) && !clazz.getName().startsWith("java.lang.invoke.LambdaForm")) {
retransformClasses.add(clazz);
}
}
}
}
// hook已经加载的类
Class[] classes = new Class[retransformClasses.size()];
retransformClasses.toArray(classes);
if (classes.length > 0) {
inst.retransformClasses(classes);
}
}
示例2: premain
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
public static void premain(String args, Instrumentation instrumentation) {
if (!instrumentation.isRetransformClassesSupported()) {
System.out.println("Class retransformation is not supported.");
return;
}
System.out.println("Calling lambda to ensure that lambda forms were created");
Agent.lambda.run();
System.out.println("Registering class file transformer");
instrumentation.addTransformer(new Agent());
for (Class c : instrumentation.getAllLoadedClasses()) {
if (c.getName().contains("LambdaForm") &&
instrumentation.isModifiableClass(c)) {
System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
try {
instrumentation.retransformClasses(c);
} catch (UnmodifiableClassException e) {
throw new AssertionError("Modification of modifiable class " +
"caused UnmodifiableClassException", e);
}
}
}
}
示例3: testBadModification2
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Test
public void testBadModification2() throws ClassNotFoundException, AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException, UnmodifiableClassException, IllegalConnectorArgumentsException {
// Rewrite method
DynamicModification badModification = new DynamicModification() {
public Collection<String> affects() {
return Lists.newArrayList(TestJDWPAgentDebug.class.getName());
}
public void apply(ClassPool arg0) throws NotFoundException, CannotCompileException {
CtClass cls = arg0.getCtClass(TestJDWPAgentDebug.class.getName());
cls.getMethods()[0].insertBefore("definitely not code...");
}
};
JDWPAgent dynamic = JDWPAgent.get();
// Modification should just be ignored since it throws a notfoundexception
try {
dynamic.install(badModification);
fail();
} catch (CannotCompileException e) {
}
}
示例4: testBadModification2
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Test
public void testBadModification2() throws ClassNotFoundException, AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException, UnmodifiableClassException {
// Rewrite method
DynamicModification badModification = new DynamicModification() {
public Collection<String> affects() {
return Lists.newArrayList(TestJVMAgent.class.getName());
}
public void apply(ClassPool arg0) throws NotFoundException, CannotCompileException {
CtClass cls = arg0.getCtClass(TestJVMAgent.class.getName());
cls.getMethods()[0].insertBefore("definitely not code...");
}
};
JVMAgent dynamic = JVMAgent.get();
// Modification should just be ignored since it throws a notfoundexception
try {
dynamic.install(badModification);
fail();
} catch (CannotCompileException e) {
}
}
示例5: testBadTracepoint
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Test
public void testBadTracepoint() throws ClassNotFoundException, UnmodifiableClassException, CannotCompileException {
// Create and register dummy advice
PTAgentForTest test = new PTAgentForTest();
AdviceImplForTest advice = new AdviceImplForTest();
int lookupId = test.agent.adviceManager.register(advice);
// Method under test
MethodTracepointSpec t1 = TracepointsTestUtils.getMethodSpec(getClass(), "method");
MethodTracepointSpec tbad = MethodTracepointSpec.newBuilder(t1).setMethodName("badmethod").build();
// Invoke method - should do nothing before rewrite
method("hi");
advice.expectSize(0);
// Rewrite method
MethodRewriteModification mod = new MethodRewriteModification(tbad, lookupId);
test.agent.dynamic.clear().add(mod).install();
advice.expectSize(0);
method("hi");
advice.expectSize(0);
}
示例6: reset
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
public void reset() {
Class<?>[] classesToReset = classes.toArray(new Class[0]);
Class<?>[] classesToRelink = nativeClasses.toArray(new Class[0]);
classes.clear();
methods.clear();
nativeClasses.clear();
if (classesToReset.length > 0) {
try {
inst.retransformClasses(classesToReset);
} catch (UnmodifiableClassException e) {
System.err.println("unexpected class transforming restriction: " + e.getMessage());
e.printStackTrace(System.err);
}
}
for (Class<?> clazz : classesToRelink) {
relinkNativeMethods(clazz);
}
}
示例7: setupTransformer
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
private void setupTransformer() {
try {
System.out.println("setup");
Instrumented instrumented = getTestClass().getJavaClass().getAnnotation(Instrumented.class);
TestRecorderAgentConfig config = fetchConfig(instrumented);
Class<?>[] classes = fetchClasses(instrumented);
agent = new TestRecorderAgent(inst);
agent.prepareInstrumentations(config);
if (classes.length > 0) {
inst.retransformClasses(classes);
}
} catch (ReflectiveOperationException | UnmodifiableClassException | Error e) {
throw new RuntimeException(e);
}
}
示例8: instrument
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Before
public void instrument() throws ClassNotFoundException, UnmodifiableClassException, InstantiationException, IllegalAccessException{
Assert.assertTrue(methods.length == expectedResults.length);
Args args = new Args();
args.specify(Arg.DOUBLE2FLOAT);
args.specify(Arg.PRINT);
CojacReferencesBuilder builder = new CojacReferencesBuilder(args);
agent = new Agent(builder.build());
AgentTest.instrumentation.addTransformer(agent);
classz = ClassLoader.getSystemClassLoader().loadClass("com.github.cojac.unit.behaviours.Double2FloatTests");
AgentTest.instrumentation.retransformClasses(classz);
object = classz.newInstance();
}
示例9: setRounding
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
private void setRounding(Arg a) throws ClassNotFoundException, UnmodifiableClassException, InstantiationException, IllegalAccessException{
Args args = new Args();
args.specify(a);
args.specify(Arg.PRINT);
CojacReferencesBuilder builder = new CojacReferencesBuilder(args);
try{
agent = new Agent(builder.build());
isLibraryLoaded = true;
AgentTest.instrumentation.addTransformer(agent);
classz = ClassLoader.getSystemClassLoader().loadClass("com.github.cojac.unit.behaviours.NativeRoundingTests");
AgentTest.instrumentation.retransformClasses(classz);
object = classz.newInstance();
}catch(RuntimeException e){
System.err.println("Library couldn't be charged. Abording Native rounding tests.");
isLibraryLoaded = false;
}
}
示例10: testCallBackMethodCalled
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Test
public void testCallBackMethodCalled() throws ClassNotFoundException, IllegalAccessException, InstantiationException,
NoSuchMethodException, InvocationTargetException, NoSuchFieldException, UnmodifiableClassException {
Args args = new Args();
args.specify(Arg.ALL);
args.setValue(Arg.CALL_BACK, "com/github/cojac/unit/CallBacksAgent/log");
CojacReferencesBuilder builder = new CojacReferencesBuilder(args);
Agent agent = new Agent(builder.build());
AgentTest.instrumentation.addTransformer(agent);
Class<?> classz = ClassLoader.getSystemClassLoader().loadClass("com.github.cojac.unit.SimpleOverflows");
AgentTest.instrumentation.retransformClasses(classz);
Object object = classz.newInstance();
Method m = classz.getMethod("test");
m.invoke(object);
classz = ClassLoader.getSystemClassLoader().loadClass("com.github.cojac.unit.CallBacksAgent");
Field field = classz.getField("count");
Assert.assertEquals(1, field.get(null));
AgentTest.instrumentation.removeTransformer(agent);
}
示例11: instrumentClass
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
/**
* Ensures that the given sampler will be invoked every time a constructor
* for class c is invoked.
*
* @param c The class to be tracked
* @param sampler the code to be invoked when an instance of c is constructed
* @throws UnmodifiableClassException if c cannot be modified.
*/
public static void instrumentClass(Class<?> c, ConstructorCallback<?> sampler)
throws UnmodifiableClassException {
// IMPORTANT: Don't forget that other threads may be accessing this
// class while this code is running. Specifically, the class may be
// executed directly after the retransformClasses is called. Thus, we need
// to be careful about what happens after the retransformClasses call.
synchronized (samplerPutAtomicityLock) {
List<ConstructorCallback<?>> list = samplerMap.get(c);
if (list == null) {
CopyOnWriteArrayList<ConstructorCallback<?>> samplerList =
new CopyOnWriteArrayList<ConstructorCallback<?>>();
samplerList.add(sampler);
samplerMap.put(c, samplerList);
Instrumentation inst = AllocationRecorder.getInstrumentation();
Class<?>[] cs = new Class<?>[1];
cs[0] = c;
inst.retransformClasses(c);
} else {
list.add(sampler);
}
}
}
示例12: visitInsn
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
/**
* Inserts the appropriate INVOKESTATIC call
*/
@Override public void visitInsn(int opcode) {
if ((opcode == Opcodes.ARETURN) ||
(opcode == Opcodes.IRETURN) ||
(opcode == Opcodes.LRETURN) ||
(opcode == Opcodes.FRETURN) ||
(opcode == Opcodes.DRETURN)) {
throw new RuntimeException(new UnmodifiableClassException(
"Constructors are supposed to return void"));
}
if (opcode == Opcodes.RETURN) {
super.visitVarInsn(Opcodes.ALOAD, 0);
super.visitMethodInsn(
Opcodes.INVOKESTATIC,
"com/google/monitoring/runtime/instrumentation/ConstructorInstrumenter",
"invokeSamplers",
"(Ljava/lang/Object;)V",
false);
}
super.visitInsn(opcode);
}
示例13: reload
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
private void reload(Entry e)
throws IOException, ClassNotFoundException, UnmodifiableClassException {
System.err.println(e.file);
cdefs.clear();
if (e.loaderRef != null) {
ClassLoader cl = e.loaderRef.get();
if (cl != null) {
request(e, cl);
if (e.children != null) {
for (Entry ce : e.children) {
request(ce, cl);
}
}
// System.err.println(cdefs);
Agent.inst.redefineClasses(cdefs.toArray(new ClassDefinition[0]));
} else {
e.loaderRef = null;
}
}
}
示例14: autoReload
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
/**
* 自动热加载 Class
* @param intervals 自动重读的时间间隔, 单位: 秒
* @throws UnmodifiableClassException 不可修改的 Class 异常
* @throws ClassNotFoundException Class未找到异常
*/
public void autoReload(int intervals) throws UnmodifiableClassException, ClassNotFoundException {
this.reloadIntervals = intervals;
cancelAutoReload();
Logger.info("[HOTSWAP] Start auto reload and hotswap every " + intervals + " seconds");
reloadTask = new HashWheelTask() {
@Override
public void run() {
try {
List<ClassFileInfo> changedFiles = fileWatcher();
reloadClass(changedFiles);
} catch (UnmodifiableClassException |ClassNotFoundException e) {
e.printStackTrace();
}
}
};
Global.getHashWheelTimer().addTask(reloadTask, intervals, true);
}
示例15: visitInsn
import java.lang.instrument.UnmodifiableClassException; //导入依赖的package包/类
@Override
public void visitInsn(int opcode) {
if ((opcode == Opcodes.ARETURN) || (opcode == Opcodes.IRETURN)
|| (opcode == Opcodes.LRETURN)
|| (opcode == Opcodes.FRETURN)
|| (opcode == Opcodes.DRETURN)) {
throw new RuntimeException(new UnmodifiableClassException("Constructors are supposed to return void"));
}
if (opcode == Opcodes.RETURN) {
super.visitVarInsn(Opcodes.ALOAD, 0);
super.visitTypeInsn(Opcodes.NEW, Type.getInternalName(ScottReportingRule.class));
super.visitInsn(Opcodes.DUP);
super.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(ScottReportingRule.class), "<init>", "()V", false);
super.visitFieldInsn(Opcodes.PUTFIELD,
className, "scottReportingRule",
Type.getDescriptor(ScottReportingRule.class));
}
super.visitInsn(opcode);
}