Java 如何找到包含特定注释的类或方法?
假设我们有一个类,在同一个包中有单独的用户定义注释,如Java 如何找到包含特定注释的类或方法?,java,reflection,annotations,Java,Reflection,Annotations,假设我们有一个类,在同一个包中有单独的用户定义注释,如 @One class A { } @Two class B { } @One class C { } 我如何找到哪个类正在使用@one注释,哪个@Two 或 我们有一个类,在这个类中对方法进行了注释,我想检索哪个注释有哪个注释 比如: class A { @two public method1() { } @one public method2() { } @two
@One
class A
{
}
@Two
class B
{
}
@One
class C
{
}
我如何找到哪个类正在使用@one
注释,哪个@Two
或
我们有一个类,在这个类中对方法进行了注释,我想检索哪个注释有哪个注释
比如:
class A
{
@two
public method1()
{
}
@one
public method2()
{
}
@two
public method3()
{
}
}
我正在使用核心java反射和注释
任何其他类似的想法都将受到赞赏。
任何帮助都将不胜感激
这是我的追踪代码,它不起作用。如果你有什么新的方法来指引我
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
@GUI
public class Demo{
public static void example() {
Demo ob = new Demo();
try {
Class c = ob.getClass();
// get the method example
Method m = c.getMethod("example");
// get the annotations
Annotation[] annotation = m.getAnnotations();
// print the annotation
for (int i = 0; i < annotation.length; i++) {
System.out.println(annotation[i]);
}
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
public static void show()
{
java.lang.annotation.Annotation[] annotations = null;
try {
annotations = Class.forName("Demo").getAnnotations();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//prints [@java.lang.Deprecated()]
System.out.println(Arrays.toString(annotations));
}
public static void myMeth() {
Demo ob = new Demo();
try {
Annotation annos[] = ob.getClass().getAnnotations();
System.out.println("All annotations for Meta2:");
for (Annotation a : annos)
System.out.println(a);
Method m = ob.getClass().getMethod("myMeth");
annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for (Annotation a : annos)
System.out.println(a);
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
example();
show();
myMeth();
}
}
import java.lang.annotation.annotation;
导入java.lang.reflect.Method;
导入java.util.array;
@桂
公开课演示{
公共静态void示例(){
Demo ob=新的Demo();
试一试{
c类=ob.getClass();
//获取方法示例
方法m=c.getMethod(“示例”);
//获取注释
Annotation[]Annotation=m.getAnnotations();
//打印注释
对于(int i=0;i
在类或方法上找到注释后,可以检查注释的类型
Annotation a = .....
if (a.annotationType().equals(GUI.class)) {
System.out.println("Found");
}
编辑:完全工人阶级
@Deprecated
public class Demo {
public static void example() {
try {
Class c = Demo.class;
// get the annotations
Annotation[] classAnnotations = c.getAnnotations();
// print Class annotation
for (int i = 0; i < classAnnotations.length; i++) {
if (classAnnotations[i].annotationType().equals(Deprecated.class)) {
System.out.println("Class Annotation: " + classAnnotations[i].annotationType());
}
}
// get the method show
Method m = c.getMethod("show");
Annotation[] methodAnnotations = m.getDeclaredAnnotations();
// print Methods annotation
for (int i = 0; i < methodAnnotations.length; i++) {
if (methodAnnotations[i].annotationType().equals(Deprecated.class)) {
System.out.println("Method Annotation: " + methodAnnotations[i].annotationType());
}
}
} catch (NoSuchMethodException exc) {
exc.printStackTrace();
}
}
@Deprecated
public static void show()
{
}
public static void main(String args[]) {
example();
}
}
@已弃用
公开课演示{
公共静态void示例(){
试一试{
c类=演示类;
//获取注释
Annotation[]classAnnotations=c.getAnnotations();
//打印类批注
for(int i=0;i
我通过一些不同的方法找到了这个问题的解决方案:-
创建用户定义的注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation
{
String testText();
}
创建使用此注释的类
public class TestClass {
@TestAnnotation(testText="arg")
public void doSomething(){
System.out.println("kaam banta arg 1111");
}
@TestAnnotation(testText="anu")
public void doSomething1(){
System.out.println("Calling ANU");
}
@TestAnnotation(testText="arg")
public void doSomething2(){
System.out.println("kaam banta arg 22222");
}
使用此逻辑创建一个类
public class ClassesfromPackage {
public static void main(String args[])
{
try {
for (Method method : TestClass.class.getMethods())
{
if (method.isAnnotationPresent((Class<? extends Annotation>) TestAnnotation.class))
{
TestAnnotation ta = method.getAnnotation(TestAnnotation.class);
if(ta.testText().contains("anu"))
{
String aMethod = method.getName();
System.out.println(aMethod);
Object iClass = TestClass.class.newInstance();
Class params[] = {};
Object paramsObj[] = {};
Method thisMethod = TestClass.class.getDeclaredMethod(aMethod, params);
// call the method
thisMethod.invoke(iClass, paramsObj);
}
/*else
{
String aMethod = method.getName();
Object iClass = TestClass.class.newInstance();
Class params[] = {};
System.out.println("Inside Else");
Object paramsObj[] = {};
Method thisMethod = TestClass.class.getDeclaredMethod(aMethod, params);
// call the method
thisMethod.invoke(iClass, paramsObj);
}*/
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
公共类ClassesfromPackage{
公共静态void main(字符串参数[])
{
试一试{
对于(方法:TestClass.class.getMethods())
{
如果(方法)isAnnotationPresent((类您搜索过吗?它对我不起作用。您有其他解决方案吗请在上面附加注释处理代码。请查找附加的代码,但我希望检索类中存在的所有注释,并根据所选注释执行操作。通过此操作,我在上查找类级别存在的注释对于一个类,我有很多测试用例。相反,我有一个类,测试用例有GUI、Functional和其他这三类测试用例。我想根据类别找到方法或类,并执行可能的方法或类。我在你的代码中看到你能够找到类和方法的所有注释。这个解决方案选项将允许您标识存在的批注。标识该方法后,您可以使用method.invoke….
执行该方法。您还可以使用反射创建类的新对象。例如,object instance=class.forName(“MyClass”).newInstance();
我的myMeth方法在给定的方法上给出注释,就像我的例子中的myMeth一样。但是我可能有多个方法和多个注释,我不会为每个方法声明。