有没有办法禁用javac 1.6.0_22的限制,阻止我使用JRE内部类sun.awt.event.*?
我不是在寻找:
我只想知道它是否可能,如果是,那么如何.
我知道在Java中,我们可以通过创建一个类的实例new,clone(),Reflection和serializing and de-serializing.
我创建了一个实现Singleton的简单类.
我需要一直停止创建我的类的实例.
public class Singleton implements Serializable{
private static final long serialVersionUID = 3119105548371608200L;
private static final Singleton singleton = new Singleton();
private Singleton() { }
public static Singleton getInstance(){
return singleton;
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cloning of this class is not allowed");
}
protected Object readResolve() {
return singleton;
}
//-----> This is my implementation to stop it but Its not working. …Run Code Online (Sandbox Code Playgroud) 我想在这件事上提供一些帮助,
例:
public class A {
private void foo() {
//Who Invoked me
}
}
public class B extends A { }
public class C extends A { }
public class D {
C.foo();
}
Run Code Online (Sandbox Code Playgroud)
这基本上就是这种情况.我的问题是方法foo()怎么知道谁在调用它?
编辑:基本上我试图做一个数据库层,在类AI中将创建一个将生成SQL语句的方法.通过获取调用类的所有公共属性的值来动态生成此类语句.
是的,通过使用异常获取类名是一个合理的解决方案,但我正在寻找更优雅的东西.
String className = new Exception().getStackTrace()[1].getClassName();
Run Code Online (Sandbox Code Playgroud)
这将主要用于日志记录目的,并确保我的缓存关键字是特定于组件/调用者类的.
我已阅读以下帖子
Oracle停止使用sun.reflect.Reflection.getCallerClass
我想知道这种变化究竟意味着什么.
1).意味着这个类sun.reflect.Reflection.getCallerClass会被重写以提供更多的安全性Java reflection吗?
2).意味着不再需要这门课程?也许另一种方法?
3).反思将在Java 8中结束.method.invoke将抛出UnsupportedOperationException.
4).这会影响与Spring或AspectJ面向方面编程相关的任何事情吗?
我很想知道因为我们使用Reflection method.invoke在发送到数据库之前为类提供了一些标志.如果反思是我可以用什么方法来提供我的行为,这就提出了另一个问题.我认为AOP是一种可行的方式.
非常感谢.
我知道我可以从中获取方法和类名,StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();但这不是我想要的.我想要类对象,所以我可以访问他的界面,注释等...
有可能的?
Class<?> classObject = getCallerClass();
我看到了这个问题,但这只是针对类名.
编辑:现在我以这种方式传递课程:
someService.dummyMethod(foo1, foo2, new Object(){}.getClass());
someService(String foo1, int foo2, Class<?> c) {
// stuff here to get the methodname,
// the interface of the class and an annotation from the interface.
}
Run Code Online (Sandbox Code Playgroud)
我从很多不同的类中调用someService,如果不可能,我将继续这种方式,但是如果有一种方法可以在运行时获取调用者类,我更喜欢这种方式.