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));
}
}