java获取方法名
⑴ java如何获取方法参数中的名称
一、从注解中获取抄
使用注解方式,我们需要自定义一个注解,在注解中指定参数名,然后通过反射机制,获取方法参数上的注解,从而获取到相应的注解信息。这里自定义的注解是Param,通过value参数指定参数名,定义了一个工具类ParameterNameUtils来获取指定方法的参数名列表,这里获取测试类ParameterNameTest中定义的方法method1的参数名列表表,下面是具体的代码。
⑵ 如何在java方法中获得当前方法的名称
在java方法中获得当前方法的名称方法:
一、获得当前类名:
Java代码
this.getClass().getName();
newException().getStackTrace()[i].getMethodName();//其中i=0就是当前的类的方法名字;i==1就是调用者的方法
Thread.currentThread().getStackTrace()[1].getMethodName();//具体使用数组的那个元素和JVM的实现有关,我在SUNJDK6下面测试的是第二个元素,具体说明可以查看Thread.getStackTrace方法的javadoc
二、获得当前方法名臣:
JDK1.4
Java代码
JDK1.5之后可用
Java代码
⑶ Java是否有办法获取一个方法传入的对象的名字
局部变量在方法栈帧中根本没有名字,只有偏移地址。变量名是给程序员看的。
如果你了解了这点,你就知道你的要求是从原理上不可能实现的。
当然,你可以封装一个类:
class MyArray {
private String name;
public double[] array;
public MyArray(String name, double[] array){
this.name = name;
this.array = array;
}
public String toString() {
return this.name + "的内容是:" + this.array;
}
}
然后输出这个类的对象:
MyArray a = new MyArray("array1", new double[]{0.1, 0.2, 0.3});
System.out.print(a);
⑷ java如何获取方法参数名
用反射机制,简单写了一个例子,不懂的可以看一下相关api public class OwerMethodParam {
public static void main(String[] args) {
new OwerMethodParam().test("bb");
}
public void test(String aa) {
Method[] methods = OwerMethodParam.class.getDeclaredMethods(); //取得这个类的所有方法
if (methods != null) {
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if ("test".equals(method.getName())) { //取得本方法,这个方法是test,所以就用test比较
Class<?>[] paramsClass = method.getParameterTypes(); //取得参数列表的所有类
if (paramsClass != null) {
for (Class<?> class1 : paramsClass) {
System.out.println(class1.getName());
}
}
break;
}
}
⑸ java如何动态获取方法名
你可以用Class的方法
getDeclaredMethods()
得到A类型里的所有方法(不包括继承来的);
getMethods()
得到所有的方法
返回值是 Method[]
循环对照吧。加上get,set
Method类型有getName()的方法,看看名字是不是你要的就行了。
⑹ java怎么获取方法参数名
在java中,可以来通过反射获取到类、源字段、方法签名等相关的信息,像方法名、返回值类型、参数类型、泛型类型参数等,但是不能够获取方法的参数名。在实际开发场景中,有时需要根据方法的参数名做一些操作,比如像spring-mvc中,@RequestParam、@PathVariable注解,如果不指定相应的value属性,默认就是使用方法的参数名做为HTTP请求的参数名,它是怎么做到的呢?
在这样情况下,有两种方法获取方法来解决这种需求,第一种方法是使用注解,在注解中指定对应应的参数名称,在需要使用参数名称时,获取注解中相应的值即可。第二种方法是从字节码中获取方法的参数名,但是这有一个限制,只有在编译时使用了-g或-g:vars参数生成了调试信息,class文件中才会生成方法参数名信息(在本地变量表LocalVariableTable中),而使用-g:none方式编译的class文件中是没有方法参数名信息的。所以要想完全不依赖class文件的编译模式,就不能使用这种方式。
⑺ Java中怎样获取类中的方法
//通过包获取类对应的类对象
Class<?> f= Class.forName("java.io.File");
//通过对象去获取对象对应的类的类对象
//File file=new File("");
Class<?> f= file.getClass();
//通过类的class属性去获取类的类对象
Class<?> f= File.class;
//获取方法的数组
Method[] methods= f.getMethods();
循环得到你想要的这 类中的方法,字段,属性
⑻ 请问java中类名.方法名().方法名()是什么意思
你可以从左往右一点一点的看。
DecimalFormat 类,DecimalFormat.getCurrencyInstance() 调用了这个类里的静态方法,DecimalFormat.getCurrencyInstance().format() 说明前面DecimalFormat.getCurrencyInstance()返回了一个对象,这个对象含有.format()方法。
其实这个也可以拆开写:
NumberFormat a = DecimalFormat.getCurrencyInstance();
String b = a.format(1234567);
这样写比较麻烦,连起来写比较方便
⑼ java中如何获取方法名字
import java.lang.reflect.Method;
public class Users {
public void print() {
}
public void getName() {}
public static void main(String[] args) {
Class c = Users.class;
Method m[] = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++) {
System.out.println("方法名" + m[i].getName());
}
}
}
⑽ Java如何获取方法参数的参数名称
packagecom.mikan;
importjava.lang.annotation.*;
/**
*@
*@date2015-08-0423:39
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public@interfaceParam{
Stringvalue();
}
获取注解中的参数名的工具类:
packagecom.mikan;
importjava.lang.annotation.Annotation;
importjava.lang.reflect.Method;
/**
*@authorMikan
*@date2015-08-0500:26
*/
publicclassParameterNameUtils{
/**
*获取指定方法的参数名
*
*@parammethod要获取参数名的方法
*@return按参数顺序排列的参数名列表
*/
publicstaticString[](Methodmethod){
Annotation[][]parameterAnnotations=method.getParameterAnnotations();
if(parameterAnnotations==null||parameterAnnotations.length==0){
returnnull;
}
String[]parameterNames=newString[parameterAnnotations.length];
inti=0;
for(Annotation[]parameterAnnotation:parameterAnnotations){
for(Annotationannotation:parameterAnnotation){
if(annotationinstanceofParam){
Paramparam=(Param)annotation;
parameterNames[i++]=param.value();
}
}
}
returnparameterNames;
}
}
测试类:
packagecom.mikan;
importjava.lang.reflect.Method;
importjava.util.Arrays;
/**
*@authorMikan
*@date2015-08-0423:40
*/
publicclassParameterNameTest{
publicvoidmethod1(@Param("parameter1")Stringparam1,@Param("parameter2")Stringparam2){
System.out.println(param1+param2);
}
publicstaticvoidmain(String[]args)throwsException{
Class<ParameterNameTest>clazz=ParameterNameTest.class;
Methodmethod=clazz.getDeclaredMethod("method1",String.class,String.class);
String[]parameterNames=ParameterNameUtils.(method);
System.out.println(Arrays.toString(parameterNames));
}
}