本文整理汇总了Java中java.lang.instrument.Instrumentation类的典型用法代码示例。如果您正苦于以下问题:Java Instrumentation类的具体用法?Java Instrumentation怎么用?Java Instrumentation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Instrumentation类属于java.lang.instrument包,在下文中一共展示了Instrumentation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTransformer
import java.lang.instrument.Instrumentation; //导入依赖的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.Instrumentation; //导入依赖的package包/类
/**
* 启动时加载的agent入口方法
*
* @param agentArg 启动参数
* @param inst {@link Instrumentation}
*/
public static void premain(String agentArg, Instrumentation inst) {
try {
JarFileHelper.addJarToBootstrap(inst);
if (!loadConfig(JarFileHelper.getLocalJarParentPath())) {
return;
}
readVersion();
// 初始化插件系统
JsPluginManager.init();
CheckerManager.init();
initTransformer(inst);
String message = "OpenRasp Initialized [" + projectVersion + " (build: GitCommit=" + gitCommit + " date="
+ buildTime + ")]";
System.out.println(message);
Logger.getLogger(Agent.class.getName()).info(message);
HookHandler.enableHook.set(true);
} catch (Exception e) {
System.out.println("[OpenRASP] Failed to initialize, will continue without security protection.");
e.printStackTrace();
}
}
示例3: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(String agentArg, Instrumentation inst)
{
String[] args = agentArg.split(":");
if (args.length > 2) {
System.err.println(USAGE);
}
int port = 7400;
try {
port = Integer.parseInt(args[0]);
} catch(Exception e) {
System.err.println(USAGE + ". No port specified so using default port for cassandra prometheus exporter " + port);
}
String path = "metrics";
if (args.length == 2) {
if (args[1].length() > 0) {
path = args[1];
} else {
System.err.println(USAGE + ". Invalid path defined so using default path for cassandra prometheus exporter: " + path);
}
}
PrometheusExporter exp = new PrometheusExporter(port, path);
}
示例4: initManager
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
private void initManager(final Instrumentation inst,
final CoreConfigure cfg) {
logger.debug("{} was init", EventListenerHandlers.getSingleton());
final ModuleLifeCycleEventBus moduleLifeCycleEventBus = new DefaultModuleLifeCycleEventBus();
final LoadedClassDataSource classDataSource = new DefaultLoadedClassDataSource(inst);
final ClassLoader sandboxClassLoader = getClass().getClassLoader();
// 初始化模块资源管理器
this.moduleResourceManager = new DefaultModuleResourceManager();
moduleLifeCycleEventBus.append(this.moduleResourceManager);
// 初始化服务管理器
final ProviderManager providerManager = new DefaultProviderManager(cfg, sandboxClassLoader);
// 初始化模块管理器
this.coreModuleManager = new DefaultCoreModuleManager(
inst, classDataSource, cfg, sandboxClassLoader, moduleLifeCycleEventBus, providerManager
);
}
示例5: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Hello from RedefineMethodInBacktraceAgent!");
System.out.println("isRedefineClassesSupported()=" +
inst.isRedefineClassesSupported());
instrumentation = inst;
}
示例6: premain
import java.lang.instrument.Instrumentation; //导入依赖的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);
}
}
}
}
示例7: testAddReads
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
/**
* Use redefineModule to update java.base to read java.instrument
*/
public void testAddReads() {
Module baseModule = Object.class.getModule();
Module instrumentModule = Instrumentation.class.getModule();
// pre-conditions
assertFalse(baseModule.canRead(instrumentModule));
// update java.base to read java.instrument
Set<Module> extraReads = Set.of(instrumentModule);
redefineModule(baseModule, extraReads, Map.of(), Map.of(), Set.of(), Map.of());
assertTrue(baseModule.canRead(instrumentModule));
}
示例8: agentmain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
/**
* 动态加载
*
* @param featureString 启动参数
* [namespace,token,ip,port,prop]
* @param inst inst
*/
public static void agentmain(String featureString, Instrumentation inst) {
LAUNCH_MODE = LAUNCH_MODE_ATTACH;
final Map<String, String> featureMap = toFeatureMap(featureString);
writeAttachResult(
getNamespace(featureMap),
getToken(featureMap),
main(featureMap, inst)
);
}
示例9: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(final String args, Instrumentation instrumentation) throws Exception {
instrumentation.addTransformer(new MenuItemTransformer());
instrumentation.addTransformer(new FileChooserTransformer());
logger.info("JavaVersion: " + System.getProperty("java.version"));
final int port;
if (args != null && args.trim().length() > 0) {
port = Integer.parseInt(args.trim());
} else {
throw new Exception("Port number not specified");
}
windowTitle = System.getProperty("start.window.title", "");
ObservableList<Stage> stages = StageHelper.getStages();
stages.addListener(new ListChangeListener<Stage>() {
boolean done = false;
@Override public void onChanged(javafx.collections.ListChangeListener.Change<? extends Stage> c) {
if (done) {
return;
}
if (!"".equals(windowTitle)) {
logger.warning("WindowTitle is not supported yet... Ignoring it.");
}
c.next();
if (c.wasAdded()) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override public Object run() {
return new JavaFxRecorderHook(port);
}
});
done = true;
}
}
});
}
示例10: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
try {
PromagentCollectorRegistry registry = new PromagentCollectorRegistry();
ManagementFactory.getPlatformMBeanServer().registerMBean(new Exporter(registry), new ObjectName("io.promagent:type=exporter"));
Map<String, String> args = parseCmdline(agentArgs);
if (args.containsKey("port")) {
BuiltInServer.run(args.get("host"), args.get("port"), registry);
}
ClassLoaderCache classLoaderCache = ClassLoaderCache.getInstance();
List<Path> hookJars = classLoaderCache.getPerDeploymentJars();
SortedSet<HookMetadata> hookMetadata = new HookMetadataParser(hookJars).parse();
MetricsStore metricsStore = new MetricsStore(registry);
Delegator.init(hookMetadata, metricsStore, classLoaderCache);
printHookMetadata(hookMetadata);
AgentBuilder agentBuilder = new AgentBuilder.Default()
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE);
// .with(AgentBuilder.Listener.StreamWriting.toSystemError()); // use this to see exceptions thrown in instrumented code
agentBuilder = applyHooks(agentBuilder, hookMetadata, classLoaderCache);
agentBuilder.installOn(inst);
// TODO -- the following is an experiment supporting collectors directly (in addition to hooks)
// io.prometheus.client.Collector jmxCollector = (io.prometheus.client.Collector) classLoaderCache.currentClassLoader().loadClass("io.promagent.collectors.JmxCollector").newInstance();
// registry.registerNoJmx(jmxCollector);
} catch (Throwable t) {
t.printStackTrace();
}
}
示例11: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(String arg, Instrumentation instrumentation) {
try {
log("launched from javaagent");
if (arg != null && !arg.isEmpty()) {
System.setProperty("org.to2mbn.authlibinjector.config", arg);
}
bootstrap(instrumentation::addTransformer);
} catch (Throwable e) {
// prevent the exception being thrown to VM
e.printStackTrace();
}
}
示例12: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(final String args, final Instrumentation instrumentation) throws Exception {
final BootstrapConfiguration configuration = getConfigurationFromFile();
loadMavenArtifacts(instrumentation, configuration);
loadClassPathFiles(instrumentation, configuration.files);
loadClassPathUrlsAsString(instrumentation, configuration.urls);
loadShutdownHooks(configuration);
loadApplicationBootstrapClassName(configuration);
}
示例13: loadMavenArtifacts
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
private static void loadMavenArtifacts(final Instrumentation instrumentation, final BootstrapConfiguration configuration) throws Exception {
if (configuration.hasMavenArtifact()) {
initMavenDependencyResolver(instrumentation);
addMavenRepositories(configuration.maven_repositories);
resolveAndLoadMavenArtifacts(instrumentation, configuration.maven_artifacts);
}
}
示例14: DefaultCoreModuleManager
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
/**
* 模块模块管理
*
* @param inst inst
* @param classDataSource 已加载类数据源
* @param cfg 模块核心配置
* @param sandboxClassLoader 沙箱加载ClassLoader
* @param moduleLifeCycleEventBus 模块生命周期通知总线
* @param providerManager 服务提供者管理器
*/
public DefaultCoreModuleManager(final Instrumentation inst,
final LoadedClassDataSource classDataSource,
final CoreConfigure cfg,
final ClassLoader sandboxClassLoader,
final ModuleLifeCycleEventBus moduleLifeCycleEventBus,
final ProviderManager providerManager) {
this.inst = inst;
this.classDataSource = classDataSource;
this.cfg = cfg;
this.sandboxClassLoader = sandboxClassLoader;
this.moduleLifeCycleEventBus = moduleLifeCycleEventBus;
this.providerManager = providerManager;
// 初始化模块目录
this.moduleLibDirArray = mergeFileArray(
new File[]{new File(cfg.getSystemModuleLibPath())},
cfg.getUserModuleLibFilesWithCache()
);
// 初始化加载所有的模块
try {
reset();
} catch (ModuleException e) {
logger.warn("init module[id={};] occur error={}.", e.getUniqueId(), e.getErrorCode(), e);
}
}
示例15: premain
import java.lang.instrument.Instrumentation; //导入依赖的package包/类
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Hello from ManifestTestAgent!");
System.out.println("isNativeMethodPrefixSupported()=" +
inst.isNativeMethodPrefixSupported());
System.out.println("isRedefineClassesSupported()=" +
inst.isRedefineClassesSupported());
System.out.println("isRetransformClassesSupported()=" +
inst.isRetransformClassesSupported());
}