java自定义类型参数的方法时出现NoSuchMethodException

01 public class Test {
02 // 需要反射的方法
03     public Iphone5S test4(Iphone5S iphone){
04          
05  
06         System.out.println("test4 运行了!!!!");
07         return iphone;
08     }
09  
10 }
01 public class Iphone5S {
02     private int price;
03     private String volume ;
04      
05     public int getPrice() {
06         return price;
07     }
08     public void setPrice(int price) {
09         this.price = price;
10     }
11     public String getVolume() {
12         return volume;
13     }
14     public void setVolume(String volume) {
15         this.volume = volume;
16     }
17      
18 }




01 public static void main(String[] args) throws Exception {
02      
03          
04         Class iphone5sClass = ClassUtils.getClass("test.Iphone5S");
05          
06         Object iphone5s = iphone5sClass.newInstance();
07         ClassUtils.setPropertyValue(iphone5s, "price"5899);
08         ClassUtils.setPropertyValue(iphone5s, "volume""16g");
09          
10          
11         Class clazz = ClassUtils.getClass("test.Test");
12         Method test4 = clazz.getMethod("test4", iphone5sClass);
13  
14          
15         test4.invoke(clazz.newInstance(), iphone5s);
16  
17  
18     }


异常信息:

Exception in thread "main" java.lang.NoSuchMethodException: test.Test.test4(test.Iphone5S)
    at java.lang.Class.getMethod(Class.java:1605)
    at Test.demo.Main.main(Main.java:33)

Java 注解可以附加在类、方法、字段等地方,提供元数据,供编译器、工具或运行环境使用。自定义注解不仅能记录元数据,还可通过反射或 AOP 等技术进一步处理。若 Java 的基本注解和元注解不能满足需求,就可以自定义注解 [^1][^2]。 ### 自定义注解的基本语法 使用 `@interface` 关键字定义注解,还可以使用元注解(如 `@Retention`、`@Target` 等)来修饰自定义注解,以指定注解的保留策略和使用范围。以下是一个简单的自定义注解示例: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; // 定义注解的保留策略为运行,这样在运行可以通过反射获取注解信息 @Retention(RetentionPolicy.RUNTIME) // 定义注解可以使用的目标类型,这里表示可以用于类、方法和字段 @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) @interface MyAnnotation { // 定义注解的属性,这里定义了一个名为 value 的属性,默认值为空字符串 String value() default ""; } ``` ### 自定义注解的使用示例 下面展示如何在类、方法和字段上使用自定义注解,并通过反射获取注解信息。 ```java // 在类上使用自定义注解 @MyAnnotation("This is a class annotation") class MyClass { // 在字段上使用自定义注解 @MyAnnotation("This is a field annotation") private String myField; // 在方法上使用自定义注解 @MyAnnotation("This is a method annotation") public void myMethod() { System.out.println("Hello, World!"); } } public class Main { public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException { // 获取类的注解信息 MyAnnotation classAnnotation = MyClass.class.getAnnotation(MyAnnotation.class); if (classAnnotation != null) { System.out.println("Class annotation value: " + classAnnotation.value()); } // 获取字段的注解信息 java.lang.reflect.Field field = MyClass.class.getDeclaredField("myField"); MyAnnotation fieldAnnotation = field.getAnnotation(MyAnnotation.class); if (fieldAnnotation != null) { System.out.println("Field annotation value: " + fieldAnnotation.value()); } // 获取方法的注解信息 java.lang.reflect.Method method = MyClass.class.getMethod("myMethod"); MyAnnotation methodAnnotation = method.getAnnotation(MyAnnotation.class); if (methodAnnotation != null) { System.out.println("Method annotation value: " + methodAnnotation.value()); } } } ``` 在上述代码中,首先定义了一个自定义注解 `MyAnnotation`,然后在 `MyClass` 类、类的字段 `myField` 和方法 `myMethod` 上使用了该注解。最后在 `Main` 类的 `main` 方法中,通过反射获取了类、字段和方法上的注解信息并打印输出。 ### IDEA 中自定义注释模板示例 在 IDEA 中可以设置自定义注释模板。例如,在设置 Abbreviation ,它是生成注释选择 Live Template 模版的快捷提示。IDEA 生成注释的方式是模板名 + 快捷键(如设置模板名为 a,快捷键用 Tab,则生成方式为 a + Enter),但不要把 Abbreviation 设置成斜杠,因为这会导致注释无法获取方法参数,可设置成其他字母或符号 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值