- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为一项服务开发一个插件。为了使其正常运行,它需要一些服务不提供的数据。
插件有严格的加载/卸载规范。裸插件如下所示:
public class Plugin extends JavaPlugin
{
@Override
public void onEnable() {} //Plugin enters here. Comparable to main(String[] args)
@Override
public void onDisable() {} //Plugin exits here, when service shuts down.
}
有一个名为 org.service.aClass 的包。它里面有一个方法。一个方法看起来像这样:
public boolean aMethod(boolean bool) {
return bool;
}
一个过于简单的场景,但它有效。 每当调用 aMethod 时,我的插件需要知道 bool 的值。这对我的程序来说绝对是至关重要的;我没有其他方法可以获得该值。
我会建议使用 aMethod,但由于我的插件是在服务之后加载的,所以这将不起作用。据我所知,加载时间编织也不适合这里,因为是在之后加载的。
尽管它不起作用,但这是我正在使用的方面,以防它有任何用处:
public aspect LogAspect {
pointcut publicMethodExecuted(): execution(* org.service.aClass.aMethod(..));
after(): publicMethodExecuted(){
cat(String.format("Entered method: %s.",
thisJoinPoint.getSignature()));
List<Object> arguments = Arrays.asList(thisJoinPoint.getArgs());
List<Object> argTypes = new ArrayList<Object>();
for (Object o: arguments) {
argTypes.add(o.getClass().toString());
}
cat(String.format("With argument types: %s and values: %s.",
argTypes, arguments));
cat(String.format("Exited method: %s.", thisJoinPoint.getSignature()));
}
public void cat(Object dog) {
System.out.println("[TEST] " + dog);
}
}
我现在打开了 AspectJ:In Action 一书,在所有加载时编织示例中,它都提到程序必须以 -javaagent 标志启动。由于我的 proram 是一个插件,所以不可能发生这种情况。
我还研究了 ASM。我找到了一个关于构建分析器的精彩教程(基本上是我想做的)here .
问题是它在启动时再次使用 -javaagent 标志,以及公共(public)静态预维护,所以它不合适,因为我只有 onEnable 和 onDisable。
然后我发现了Java的Attach API .从它的外观来看,这将允许我在加载类后附加我的代理程序,分析器。看起来很完美,但经过半小时的搜索,我找不到一个我能理解的很好的例子。
有人能帮忙吗?这是一个二合一的问题:AspectJ 可以用于此吗?如果是这样,怎么办?此外,如果它不能,有人可以指出正确的方向,将 Attach API 与 ASM 分析器一起使用吗?
提前致谢!
最佳答案
我做到了!
我为概要分析器创建了一个运行时附件 here .基本上就是这样,premain 重命名为“agentmain”。
我创建了一个 Util 类,其中包含附加程序以及其他有用的功能。附加器通过使用代理创建一个 jar 来工作,并附有声明它可以分析的 list 。 Util 类如下所示:
public class Util {
/**
* Gets the current JVM PID
* @return
* Returns the PID
* @throws Exception
*/
public static String getPidFromRuntimeMBean() {
String jvm = ManagementFactory.getRuntimeMXBean().getName();
String pid = jvm.substring(0, jvm.indexOf('@'));
return pid;
}
/**
* Attaches given agent classes to JVM
*
* @param agentClasses
* A Class<?>[] of classes to be included in agent
* @param JVMPid
* The PID of the JVM to attach to
*/
public static void attachAgentToJVM(Class<?>[] agentClasses, String JVMPid) {
try {
File jarFile = File.createTempFile("agent", ".jar");
jarFile.deleteOnExit();
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
mainAttributes.put(new Attributes.Name("Agent-Class"),
Agent.class.getName());
mainAttributes.put(new Attributes.Name("Can-Retransform-Classes"),
"true");
mainAttributes.put(new Attributes.Name("Can-Redefine-Classes"), "true");
JarOutputStream jos = new JarOutputStream(new FileOutputStream(
jarFile), manifest);
for(Class<?> clazz: agentClasses) {
JarEntry agent = new JarEntry(clazz.getName().replace('.',
'/')
+ ".class");
jos.putNextEntry(agent);
jos.write(getBytesFromIS(clazz.getClassLoader()
.getResourceAsStream(
clazz.getName().replace('.', '/') + ".class")));
jos.closeEntry();
}
jos.close();
VirtualMachine vm = VirtualMachine.attach(JVMPid);
vm.loadAgent(jarFile.getAbsolutePath());
vm.detach();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Gets bytes from InputStream
*
* @param stream
* The InputStream
* @return
* Returns a byte[] representation of given stream
*/
public static byte[] getBytesFromIS(InputStream stream) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
int nRead;
byte[] data = new byte[16384];
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
} catch (Exception e) {
System.err.println("Failed to convert IS to byte[]!");
e.printStackTrace();
}
return buffer.toByteArray();
}
/**
* Gets bytes from class
*
* @param clazz
* The class
* @return
* Returns a byte[] representation of given class
*/
public static byte[] getBytesFromClass(Class<?> clazz) {
return getBytesFromIS(clazz.getClassLoader().getResourceAsStream( clazz.getName().replace('.', '/') + ".class"));
}
}
为了清晰起见,我包含了 JavaDoc 注释。
使用它的一个例子是:
Util.attachAgentToJVM(new Class<?>[] { Agent.class, Util.class,
Profile.class, ProfileClassAdapter.class,
ProfileMethodAdapter.class }, Util.getPidFromRuntimeMBean());
请记住,附加器希望 Agent.class 成为主要代理。您可以轻松更改此设置。 Class[] 的其余部分是要包含在临时 agent.jar 中的类
如果您的 IDE 提示“UnsatisfiedLinkError”,那是因为为此所需的 attach.(dll|so) 仅随 JDK 一起提供。只需将其复制粘贴到您的 %JAVA_PATH%/jre/lib 中即可。此外,添加对 JDK 的 tools.jar 的引用,因为它包含所有 com.sun 导入。
编辑: 我有一个可用的 github 示例,任何人都可能认为这很有用。它的here .
关于java - 分析一个已经加载的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238694/
我刚刚继承了一个旧的 PostgreSQL 安装,需要进行一些诊断以找出该数据库运行缓慢的原因。在 MS SQL 上,您可以使用 Profiler 等工具来查看正在运行的查询,然后查看它们的执行计划。
将目标从Analytics(分析)导入到AdWords中,然后在Analytics(分析)中更改目标条件时,是否可以通过更改将目标“重新导入”到AdWords,还是可以自动选择? 最佳答案 更改目标值
我正在使用google analytics api来获取数据。我正在获取数据,但我想验证两个参数,它们在特定日期范围内始终为0。我正在获取['ga:transactions']和['ga:goalCo
我使用Google API从Google Analytics(分析)获取数据,但指标与Google Analytics(分析)的网络界面不同。 即:我在2015年3月1日获得数据-它返回综合浏览量79
我在我的Web应用程序中使用sammy.js进行剔除。我正在尝试向其中添加Google Analytics(分析)。我很快找到了following plugin来实现页面跟踪。 我按照步骤操作,页面如
当使用 Xcode 分析 (product>analyze) 时,有没有办法忽略给定文件中的任何错误? 例如编译指示之类的? 我们只想忽略第三方代码的任何警告,这样当我们的代码出现问题时,它对我们
目录 EFK 1. 日志系统 2. 部署ElasticSearch 2.1 创建handless服务 2.2 创建s
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
GCC/G++ 是否有可用于输出分析的选项? 能够比较以前的代码与新代码之间的差异(大小、类/结构的大小)将很有用。然后可以将它们与之前的输出进行比较以进行比较,这对于许多目的都是有用的。 如果没有此
我正在浏览 LYAH,并一直在研究处理列表时列表理解与映射/过滤器的使用。我已经分析了以下两个函数,并包含了教授的输出。如果我正确地阅读了教授的内容,我会说 FiltB 的运行速度比 FiltA 慢很
在 MySQL 中可以使用 SET profiling = 1; 设置分析 查询 SHOW PROFILES; 显示每个查询所用的时间。我想知道这个时间是只包括服务器的执行时间还是还包括将结果发送到前
我用 Python 编写了几个用于生成阶乘的模块,我想测试运行时间。我找到了一个分析示例 here我使用该模板来分析我的模块: import profile #fact def main():
前几天读了下mysqld_safe脚本,个人感觉还是收获蛮大的,其中细致的交代了MySQL数据库的启动流程,包括查找MySQL相关目录,解析配置文件以及最后如何调用mysqld程序来启动实例等,有着
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
我有四列形式的数据。前三列代表时间,value1,value 2。第四列是二进制,全为 0 或 1。当第四列中对应的二进制值为0时,有没有办法告诉excel删除时间、值1和值2?我知道这在 C++ 或
我正在运行一个进行长时间计算的 Haskell 程序。经过一些分析和跟踪后,我注意到以下内容: $ /usr/bin/time -v ./hl test.hl 9000045000050000 Com
我有一个缓慢的 asp.net 程序正在运行。我想分析生产服务器以查看发生了什么,但我不想显着降低生产服务器的速度。 一般而言,配置生产盒或仅本地开发盒是标准做法吗?另外,您建议使用哪些程序来实现这一
我目前正在尝试分析 Haskell 服务器。服务器永远运行,所以我只想要一个固定时间的分析报告。我尝试只运行该程序 3 分钟,然后礼貌地要求它终止,但不知何故,haskell 分析器不遵守术语信号,并
是否有工具可以分析 Maven 构建过程本身,以便我可以看到构建花费最多时间的地方? 我们在工作中遇到了关于 Maven 3.0.3 和 3.0b1 的问题。与 3.0.3 (9m00s) 相比,我们
我是一名优秀的程序员,十分优秀!