如何使用java代理在java中为一个类文件生成多个公共类

时间:2017-02-21 03:01:18

标签: java classloader javaagents

我使用java代理基于java中的一个输入类文件为两个公共类生成字节码。

现在,我已经在ClassFileTransformer的子类中的transform函数中获得了两个字节数组,其中包含生成类的字节代码。如何直接将它们加载到JVM中而不将它们保存到磁盘文件中?

代码看起来像这样

#!/usr/bin/env python

from collections import defaultdict

MemberInfo = [
    {
        "UserName": "Tom Las",
        "title": "Director"
    },
    {
        "UserName": "Mike Sea",
        "title": "senior manager"
    },
    {
        "UserName": "Jojo Lee",
        "title": "manager",
        "direct Report": "Mike Sea"
    },
    {
        "UserName": "Luke Shi",
        "title": "manager",
        "direct Report": "Mike Sea"
    },
    {
        "UserName": "Bob Aeo",
        "title": "engineer",
        "direct Report": "Luke Shi"
    },
    {
        "UserName": "Zobu hu",
        "title": "engineer",
        "direct Report": "Tom Las"
    }
]

Leader={}
Team=defaultdict(list)

for line in MemberInfo:
    if "direct Report" not in line and line['UserName'] not in Leader:
        Leader[line['UserName']] = line['title']
    elif line['direct Report']:
        Team[line['direct Report']].append(line['UserName'])

for key,value in Leader.iteritems():
    print("{} - {}".format(key,value))
    for Member in Team[key]:
        if Member in Team:
            print("  {}".format(Member))
            print(" "*4 + ' '.join(Team[Member]))
        else:
            print("  {}".format(Member))

1 个答案:

答案 0 :(得分:0)

你不能没有技巧。正如ClassLoader可用,您可以直接调用defineClass方法。但是,这确实需要反射,并且在没有明确导出java.lang包的情况下在Java 9中不再可能。