反编译spring aop动态代理生成的class

  1. 用到的工具 arthas
    工具快速入门:https://arthas.aliyun.com/doc/quick-start.html
  2. 详细步骤
Microsoft Windows [版本 10.0.22000.434]
(c) Microsoft Corporation。保留所有权利。

C:\WINDOWS\system32>cd /d D:\Users\uniz\Desktop\temp

D:\Users\uniz\Desktop\temp>java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.5.3
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 17288 org.codehaus.plexus.classworlds.launcher.Launcher
1
[INFO] arthas home: C:\Users\uniz\.arthas\lib\3.5.5\arthas
[INFO] Try to attach process 17288
[INFO] Found java home from System Env JAVA_HOME: D:\FA\tools\Java\jdk1.7.0_80
[INFO] Attach process 17288 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.5
main_class
pid        17288
time       2022-01-24 17:34:39

[arthas@17288]$ sc com.sun.proxy.$Proxy91
com.sun.proxy.$Proxy91
Affect(row-cnt:1) cost in 53 ms.
[arthas@17288]$ jad com.sun.proxy.$Proxy91

ClassLoader:





     ssRealm[plugin>org.codehaus.mojo:tomcat-maven-plugin:1.1, parent: sun.misc.Launcher$AppClassLoader@6521f956]
  +-ClassRealm[plugin>org.codehaus.mojo:tomcat-maven-plugin:1.1, parent: sun.misc.Launcher$AppClassLoader@6521f956]

Location:

核心操作步骤:

1、进入arthas-boot.jar所在目录
cd /d D:\Users\uniz\Desktop\temp
  
2、启动arthas
java -jar arthas-boot.jar

3、启动后,选择java线程前面的序号,我这里只启动了一个java web项目,选择1,然后回车即可

4、debug 代码,获取类名
DemoService service = AppContext.getBean(DemoService.class);
System.out.println(service.getClass().getName());// com.sun.proxy.$Proxy91

5、查看jvm是否加载
sc com.sun.proxy.$Proxy91

6、反编译获取源码
jad com.sun.proxy.$Proxy91

反编译得到的java源码:


/*
 * Decompiled with CFR.
 *
 * Could not load the following classes:
 *  com.uniz.manager.exceptions.ManagerException
 *  com.uniz.service.DemoService
 *  org.aopalliance.aop.Advice
 *  org.springframework.aop.Advisor
 *  org.springframework.aop.SpringProxy
 *  org.springframework.aop.TargetSource
 *  org.springframework.aop.framework.Advised
 *  org.springframework.aop.framework.AopConfigException
 */
package com.sun.proxy;

import com.uniz.manager.exceptions.ManagerException;
import com.uniz.service.DemoService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopConfigException;

public final class $Proxy91
extends Proxy
implements DemoService,
SpringProxy,
Advised {
    private static Method m21;
    private static Method m3;
    private static Method m12;
    private static Method m6;
    private static Method m2;
    private static Method m15;
    private static Method m18;
    private static Method m13;
    private static Method m4;
    private static Method m26;
    private static Method m10;
    private static Method m9;
    private static Method m7;
    private static Method m8;
    private static Method m19;
    private static Method m25;
    private static Method m17;
    private static Method m5;
    private static Method m14;
    private static Method m22;
    private static Method m1;
    private static Method m16;
    private static Method m24;
    private static Method m20;
    private static Method m0;
    private static Method m11;
    private static Method m23;

    public $Proxy91(InvocationHandler invocationHandler) {
        super(invocationHandler);
    }

    static {
        try {
            m21 = Class.forName("org.springframework.aop.framework.Advised").getMethod("isPreFiltered", new Class[0]);
            m3 = Class.forName("com.uniz.service.DemoService").getMethod("updateComments", Class.forName("java.lang.String"), Class.forName("java.lang.String"));
            m12 = Class.forName("org.springframework.aop.framework.Advised").getMethod("isProxyTargetClass", new Class[0]);
            m6 = Class.forName("org.springframework.aop.framework.Advised").getMethod("isFrozen", new Class[0]);
            m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
            m15 = Class.forName("org.springframework.aop.framework.Advised").getMethod("removeAdvice", Class.forName("org.aopalliance.aop.Advice"));
            m18 = Class.forName("org.springframework.aop.framework.Advised").getMethod("removeAdvisor", Class.forName("org.springframework.aop.Advisor"));
            m13 = Class.forName("org.springframework.aop.framework.Advised").getMethod("setExposeProxy", Boolean.TYPE);
            m4 = Class.forName("org.springframework.aop.framework.Advised").getMethod("indexOf", Class.forName("org.springframework.aop.Advisor"));
            m26 = Class.forName("org.springframework.aop.framework.Advised").getMethod("getTargetClass", new Class[0]);
            m10 = Class.forName("org.springframework.aop.framework.Advised").getMethod("getTargetSource", new Class[0]);
            m9 = Class.forName("org.springframework.aop.framework.Advised").getMethod("addAdvisor", Integer.TYPE, Class.forName("org.springframework.aop.Advisor"));
            m7 = Class.forName("org.springframework.aop.framework.Advised").getMethod("isExposeProxy", new Class[0]);
            m8 = Class.forName("org.springframework.aop.framework.Advised").getMethod("addAdvisor", Class.forName("org.springframework.aop.Advisor"));
            m19 = Class.forName("org.springframework.aop.framework.Advised").getMethod("addAdvice", Integer.TYPE, Class.forName("org.aopalliance.aop.Advice"));
            m25 = Class.forName("org.springframework.aop.framework.Advised").getMethod("getProxiedInterfaces", new Class[0]);
            m17 = Class.forName("org.springframework.aop.framework.Advised").getMethod("removeAdvisor", Integer.TYPE);
            m5 = Class.forName("org.springframework.aop.framework.Advised").getMethod("indexOf", Class.forName("org.aopalliance.aop.Advice"));
            m14 = Class.forName("org.springframework.aop.framework.Advised").getMethod("setPreFiltered", Boolean.TYPE);
            m22 = Class.forName("org.springframework.aop.framework.Advised").getMethod("replaceAdvisor", Class.forName("org.springframework.aop.Advisor"), Class.forName("org.springframework.aop.Advisor"));
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m16 = Class.forName("org.springframework.aop.framework.Advised").getMethod("getAdvisors", new Class[0]);
            m24 = Class.forName("org.springframework.aop.framework.Advised").getMethod("isInterfaceProxied", Class.forName("java.lang.Class"));
            m20 = Class.forName("org.springframework.aop.framework.Advised").getMethod("addAdvice", Class.forName("org.aopalliance.aop.Advice"));
            m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
            m11 = Class.forName("org.springframework.aop.framework.Advised").getMethod("setTargetSource", Class.forName("org.springframework.aop.TargetSource"));
            m23 = Class.forName("org.springframework.aop.framework.Advised").getMethod("toProxyConfigString", new Class[0]);
            return;
        }
        catch (NoSuchMethodException noSuchMethodException) {
            throw new NoSuchMethodError(noSuchMethodException.getMessage());
        }
        catch (ClassNotFoundException classNotFoundException) {
            throw new NoClassDefFoundError(classNotFoundException.getMessage());
        }
    }

    public final boolean equals(Object object) {
        try {
            return (Boolean)this.h.invoke(this, m1, new Object[]{object});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final String toString() {
        try {
            return (String)this.h.invoke(this, m2, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final int hashCode() {
        try {
            return (Integer)this.h.invoke(this, m0, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final int indexOf(Advisor advisor) {
        try {
            return (Integer)this.h.invoke(this, m4, new Object[]{advisor});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final int indexOf(Advice advice) {
        try {
            return (Integer)this.h.invoke(this, m5, new Object[]{advice});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean isFrozen() {
        try {
            return (Boolean)this.h.invoke(this, m6, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean isExposeProxy() {
        try {
            return (Boolean)this.h.invoke(this, m7, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void addAdvisor(int n, Advisor advisor) throws AopConfigException {
        try {
            this.h.invoke(this, m9, new Object[]{n, advisor});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void addAdvisor(Advisor advisor) throws AopConfigException {
        try {
            this.h.invoke(this, m8, new Object[]{advisor});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final TargetSource getTargetSource() {
        try {
            return (TargetSource)this.h.invoke(this, m10, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void setTargetSource(TargetSource targetSource) {
        try {
            this.h.invoke(this, m11, new Object[]{targetSource});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean isProxyTargetClass() {
        try {
            return (Boolean)this.h.invoke(this, m12, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void setExposeProxy(boolean bl) {
        try {
            this.h.invoke(this, m13, new Object[]{bl});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void setPreFiltered(boolean bl) {
        try {
            this.h.invoke(this, m14, new Object[]{bl});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final Class getTargetClass() {
        try {
            return (Class)this.h.invoke(this, m26, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean removeAdvice(Advice advice) {
        try {
            return (Boolean)this.h.invoke(this, m15, new Object[]{advice});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final Advisor[] getAdvisors() {
        try {
            return (Advisor[])this.h.invoke(this, m16, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void removeAdvisor(int n) throws AopConfigException {
        try {
            this.h.invoke(this, m17, new Object[]{n});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean removeAdvisor(Advisor advisor) {
        try {
            return (Boolean)this.h.invoke(this, m18, new Object[]{advisor});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void addAdvice(Advice advice) throws AopConfigException {
        try {
            this.h.invoke(this, m20, new Object[]{advice});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void addAdvice(int n, Advice advice) throws AopConfigException {
        try {
            this.h.invoke(this, m19, new Object[]{n, advice});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean isPreFiltered() {
        try {
            return (Boolean)this.h.invoke(this, m21, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean replaceAdvisor(Advisor advisor, Advisor advisor2) throws AopConfigException {
        try {
            return (Boolean)this.h.invoke(this, m22, new Object[]{advisor, advisor2});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final String toProxyConfigString() {
        try {
            return (String)this.h.invoke(this, m23, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final boolean isInterfaceProxied(Class clazz) {
        try {
            return (Boolean)this.h.invoke(this, m24, new Object[]{clazz});
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final Class[] getProxiedInterfaces() {
        try {
            return (Class[])this.h.invoke(this, m25, null);
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public final void updateComments(String string, String string2) throws ManagerException {
        try {
            this.h.invoke(this, m3, new Object[]{string, string2});
            return;
        }
        catch (Error | RuntimeException throwable) {
            throw throwable;
        }
        catch (Throwable throwable) {
            throw new UndeclaredThrowableException(throwable);
        }
    }
}

原DemoService实现类源码

package com.uniz.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mycim.dao.wip.lot.DemoDao;


@Service
@Transactional
public class DemoServiceImpl implements DemoService {

	@Autowired
	protected DemoDao dao;

	@Override
	public void updateComments(String id, String comments) {
		dao.updateComments(id, comments);
	}
}

注意:动态生成的代理类必须被jvm加载后才能通过sc命令查看到,也才能够用jad命令进行反编译。项目启动,代理类并未生成,也没有被jvm加载,可以先执行一下相关方法。

总结:从反编译源码可以看出,此处就是基于jdk的动态代理实现的

上一篇:Java AOP


下一篇:运用Spring Aop,一个注解实现日志记录