导航:首页 > 编程语言 > javanewthrowable

javanewthrowable

发布时间:2023-08-20 04:03:08

java运行一个方法时如何得到该个对象的名字(不是类的名字).

可以通过StackTrace从栈顶往下倒


Stringclsname="ClassName";
StackTraceElementstack[]=(newThrowable()).getStackTrace();
intix=0;
while(ix<stack.length){
StackTraceElementframe=stack[ix];
Stringcname=frame.getClassName();
if(cname.equals(clsname)){
break;
}
ix++;
}

while(ix<stack.length){
StackTraceElementframe=stack[ix];
Stringcname=frame.getClassName();
if(!cname.equals(clsname)){
System.out.println("类名:"+cname);
System.out.println("方法名:"+frame.getMethodName());
}
ix++;
}


㈡ java如何获取内部静态类变量

静态方法不与特定实例关联,不能引用this,要得到当前类名,没有直接的办法。
通过查资料和试验,可以用下面几种方式:
public static void testGetClassName()
{
// 方法1:通过SecurityManager的保护方法getClassContext()
String clazzName = new SecurityManager()
{ public String getClassName()
{
return getClassContext()[1].getName();
}
}.getClassName();
System.out.println(clazzName);
// 方法2:通过Throwable的方法getStackTrace()
String clazzName2 = new Throwable().getStackTrace()[1].getClassName();
System.out.println(clazzName2);
// 方法3:通过分析匿名类名称()
String clazzName3 = new Object() {
public String getClassName()
{
String clazzName = this.getClass().getName();
return clazzName.substring(0, clazzName.lastIndexOf('$'));
}
}.getClassName();
System.out.println(clazzName3);
}
分别调用10万次,
方法1:219ms
方法2:953ms
方法3:31ms

㈢ Java怎么获取当前跟踪的堆栈

解决方法 1:
您可以使用Thread.currentThread().getStackTrace()
返回的数组的 StackTraceElement s 表示程序的当前堆栈跟踪。

解决方法 2:
Thread.currentThread().getStackTrace();

如果你不版在乎堆栈的第一个元素是什么权。
new Throwable().getStackTrace();

会有一个定义的位置,您当前方法的问题。
解决方法 3:
愚蠢是我,Thread.currentThread().getStackTrace();
解决方法 4:
try {
}
catch(Exception e) {
StackTraceElement[] traceElements = e.getStackTrace();
//...
}


Thread.currentThread().getStackTrace()

㈣ Java中怎样得到当前类名

this.getClass().getSimpleName()

㈤ 什么情况下,JAVA中执行代码出异常时不经过Catch而直接跳入finally

有一种可能会出现没有catch异常的情况:当抛出的不是Exception及其子类时,catch(Exception e)将无法捕获该异常。请看如下代码:
public static void main(String[] args) throws Throwable {

try{
throw new Throwable(){

};
}
catch(Exception e){
System.out.println("err");
}
finally{
System.out.println("finally");
}

}
就会不经catch而到输出 finally。

有一个测试方法是加入catch(Throwable t)即可发现是否有上述情况,示例如下:
public static void main(String[] args) throws Throwable {

try{
throw new Throwable(){

};
}
catch(Exception e){
System.out.println("err");
}
catch(Throwable t){
System.out.println("t");
}
finally{
System.out.println("finally");
}

}

这就会输出
t
finally

㈥ java 获取方法调用者 的参数

区分是调用哪个test是由你调用的时候的参数决定的。
例如:调用时test(1),那么你调专用的是
test(int i){
new A().getMethod();
}
这个方属法
如果调用的时候是test("str")调用的就是
test(String i){
new A().getMethod();
}
了。

㈦ java反射无法动态获取注解

@Action
public void test() throws NoSuchMethodException, SecurityException{
StackTraceElement[] stack = new Throwable().getStackTrace();
Method method = this.getClass().getMethod(stack[0].getMethodName());
for(Annotation an : method.getAnnotations()){
System.out.println(an);
}
}

也可以写个公共方法来获取,stack[0]这里专要属改成stack[1]

阅读全文

与javanewthrowable相关的资料

热点内容
录入数据库的图片如何更改 浏览:132
怎样获取邮箱帐号和密码 浏览:809
怎么通过js实现回到指定页面 浏览:140
如何用网络签字 浏览:552
三星电视拆机教程 浏览:19
创维怎么连接网络 浏览:868
2007版word绘图在哪里 浏览:311
可以拍车牌的app是什么 浏览:508
文件加个井字号什么意思 浏览:155
怎么删除多重网络 浏览:999
求生之路2局域网联机工具 浏览:827
说明文件结尾用什么词 浏览:578
发送的文件名变数字 浏览:778
档案数据库管理 浏览:992
微信acl是金融传销吗 浏览:620
企业如何通过进行网络营销 浏览:551
微信json转换错误 浏览:364
拉勾勾是什么网站 浏览:556
长沙哪个学校有大数据技术与应用 浏览:137
qq语音停止运行 浏览:312

友情链接