當前位置: 首頁>>代碼示例>>Java>>正文


Java UnmodifiableClassException類代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:baidu,項目名稱:openrasp,代碼行數:27,代碼來源:Agent.java

示例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);
            }
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:TestLambdaFormRetransformation.java

示例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) {
    }
}
 
開發者ID:brownsys,項目名稱:tracing-framework,代碼行數:21,代碼來源:TestJDWPAgentDebug.java

示例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) {
    }
}
 
開發者ID:brownsys,項目名稱:tracing-framework,代碼行數:21,代碼來源:TestJVMAgent.java

示例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);
}
 
開發者ID:brownsys,項目名稱:tracing-framework,代碼行數:25,代碼來源:TestMethodRewriteModification.java

示例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);
	}
}
 
開發者ID:almondtools,項目名稱:testrecorder,代碼行數:19,代碼來源:FakeIOTransformer.java

示例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);
	}
}
 
開發者ID:almondtools,項目名稱:testrecorder,代碼行數:19,代碼來源:TestrecorderAgentRunner.java

示例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();
}
 
開發者ID:Cojac,項目名稱:Cojac,代碼行數:19,代碼來源:Double2FloatTest.java

示例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;
    }
}
 
開發者ID:Cojac,項目名稱:Cojac,代碼行數:20,代碼來源:NativeRoundingTest.java

示例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);
  }
 
開發者ID:Cojac,項目名稱:Cojac,代碼行數:27,代碼來源:CallBackAgentTest.java

示例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);
    }
  }
}
 
開發者ID:google,項目名稱:allocation-instrumenter,代碼行數:31,代碼來源:ConstructorInstrumenter.java

示例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);
}
 
開發者ID:google,項目名稱:allocation-instrumenter,代碼行數:24,代碼來源:ConstructorInstrumenter.java

示例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;
        }
    }
}
 
開發者ID:jervenclark,項目名稱:jreloader,代碼行數:21,代碼來源:Transformer.java

示例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);
}
 
開發者ID:helyho,項目名稱:Voovan,代碼行數:28,代碼來源:Hotswaper.java

示例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);
}
 
開發者ID:dodie,項目名稱:scott,代碼行數:22,代碼來源:ConstructorTransformerMethodVisitor.java


注:本文中的java.lang.instrument.UnmodifiableClassException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。