Java 如何检查类是否有按方面添加的方法?

Java 如何检查类是否有按方面添加的方法?,java,aspectj,inject,Java,Aspectj,Inject,假设我有一个简单的类: public class TestClass { /*...*/ } 我创建了一个方面,将新方法注入此类: public aspect TestAspect { public void TestClass.aspectMethod() { /*...*/ } } 现在,我如何检查TestClass是否在运行时通过testspect添加了方法 最简单的方法是简单地反思课堂: TestClass.class.getDeclaredM

假设我有一个简单的类:

public class TestClass {
   /*...*/
}
我创建了一个方面,将新方法注入此类:

public aspect TestAspect {
    public void TestClass.aspectMethod() {
        /*...*/
    }
} 

现在,我如何检查
TestClass
是否在运行时通过
testspect
添加了方法

最简单的方法是简单地反思课堂:

TestClass.class.getDeclaredMethod("aspectMethod")

如果不存在,它将抛出NoSuchMethodException。或者,如果您有字节,您可以使用字节码访问者来检查字节码中存在哪些方法,但反射路径不会太混乱。

最简单的方法是简单地对类进行反射:

TestClass.class.getDeclaredMethod("aspectMethod")

如果不存在,它将抛出NoSuchMethodException。或者,如果您有字节,您可以使用字节码访问者来检查字节码中存在哪些方法,但反射路径不会太混乱。

最简单的方法是简单地对类进行反射:

TestClass.class.getDeclaredMethod("aspectMethod")

如果不存在,它将抛出NoSuchMethodException。或者,如果您有字节,您可以使用字节码访问者来检查字节码中存在哪些方法,但反射路径不会太混乱。

最简单的方法是简单地对类进行反射:

TestClass.class.getDeclaredMethod("aspectMethod")

如果不存在,它将抛出NoSuchMethodException。或者,如果您有字节,您可以使用字节码访问者来检查字节码中存在哪些方法,但反射路径不会太混乱。

安迪的答案是正确的,我只想从评论中回答您的后续问题:

Duck类型不是Java特性,但是如果您使用ITD使类实现一个接口,然后拥有aspect扩展类的实例,那么您可以使用MyInterface的实例来确定您需要知道的内容。其他方法(也使用反射)也可用:

与稍后要通过ITD添加的方法的接口:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
示例驱动程序应用程序:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
应用程序输出:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
true
真的
界面de.scrum\u master.app.MyInterface
界面de.scrum\u master.app.MyInterface

安迪的答案是正确的,我只想从评论中回答你的后续问题:

Duck类型不是Java特性,但是如果您使用ITD使类实现一个接口,然后拥有aspect扩展类的实例,那么您可以使用MyInterface的实例来确定您需要知道的内容。其他方法(也使用反射)也可用:

与稍后要通过ITD添加的方法的接口:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
示例驱动程序应用程序:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
应用程序输出:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
true
真的
界面de.scrum\u master.app.MyInterface
界面de.scrum\u master.app.MyInterface

安迪的答案是正确的,我只想从评论中回答你的后续问题:

Duck类型不是Java特性,但是如果您使用ITD使类实现一个接口,然后拥有aspect扩展类的实例,那么您可以使用MyInterface的实例来确定您需要知道的内容。其他方法(也使用反射)也可用:

与稍后要通过ITD添加的方法的接口:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
示例驱动程序应用程序:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
应用程序输出:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
true
真的
界面de.scrum\u master.app.MyInterface
界面de.scrum\u master.app.MyInterface

安迪的答案是正确的,我只想从评论中回答你的后续问题:

Duck类型不是Java特性,但是如果您使用ITD使类实现一个接口,然后拥有aspect扩展类的实例,那么您可以使用MyInterface的实例来确定您需要知道的内容。其他方法(也使用反射)也可用:

与稍后要通过ITD添加的方法的接口:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
示例驱动程序应用程序:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
应用程序输出:

package de.scrum_master.app;

public interface MyInterface {
    void myMethod();
}
package de.scrum_master.app;

import java.lang.reflect.Type;

public class Application {
    public static void main(String[] args) {
        Application application = new Application();

        // Use an instance
        System.out.println(application instanceof MyInterface);
        System.out.println(MyInterface.class.isInstance(application));

        // Use the class
        for (Type type : Application.class.getGenericInterfaces())
            System.out.println(type);
        for (Class<?> clazz : Application.class.getInterfaces())
            System.out.println(clazz);
    }
}
true
真的
界面de.scrum\u master.app.MyInterface
界面de.scrum\u master.app.MyInterface

是的,这似乎是最简单的方法。我认为有一些非反射性的语言特性可以做到这一点。Duck类型对于AspectJ中的类型间声明来说是非常有用的特性。是的,这似乎是最简单的方法。我认为有一些非反射性的语言特性可以做到这一点。Duck类型对于AspectJ中的类型间声明来说是非常有用的特性。是的,这似乎是最简单的方法。我认为有一些非反射性的语言特性可以做到这一点。Duck类型对于AspectJ中的类型间声明来说是非常有用的特性。是的,这似乎是最简单的方法。我认为有一些非反射性的语言特性可以做到这一点。Duck类型对于AspectJ中的类型间声明来说是非常有用的特性。我觉得这比安迪的答案更“自然”。简单,但很好的方法,克里加克斯。我觉得这比安迪的答案更“自然”。简单,但很好的方法,克里加克斯。我觉得这比安迪的答案更“自然”。简单,但很好的方法,克里加克斯。我觉得这比安迪的回答更“自然”。