使用反射的Java动态转换

使用反射的Java动态转换,java,reflection,Java,Reflection,我有以下代码 父类: class Parent{ void a() { if(// some logic to check whether child class is calling this method or some other class//) System.out.println("Child class is calling..")} } 儿童班: class Child

我有以下代码

父类:

class Parent{

        void a()
        {   
             if(// some logic to check whether child class is calling this method or some other class//)
             System.out.println("Child class is calling..")}
        }
儿童班:

class Child extends Parent{

        void b(){System.out.println(new Parent().a());}
}
另一类:

 class Child1{

        void b(){System.out.println(new Parent().a());}
}
在这里,我从一个子类和另一个子类调用Parents()方法

我的问题是,我应该在父方法的if块中放入什么逻辑,以便确定哪个类调用此方法

我有一些代码,但不起作用。请帮助我这个代码有什么问题

if(this.getClass().getClassLoader()
            .loadClass(new Throwable().getStackTrace()[0].getClassName())
            .newInstance() instanceof Parent)
 {
           System.out.println("Child class is calling..")}
 }

您有以下选择:

  • Parent
    移动到另一个包中,并使
    a()受保护。由于新包中没有其他类,因此只有子类能够调用该方法

  • 您可以尝试堆栈跟踪方法,但查找的信息不在第一帧中-第一帧是调用
    new Throwable()
    的位置。请改为尝试第1帧(即调用
    a()
    的位置):

    如果您关心性能,那么应该缓存此结果


  • 也就是说,我想知道这段代码的目的是什么。我的直觉是,你正在解决一个更深层次的问题,创造一个脆弱的工作环境,这将导致不同的痛苦。也许你应该告诉我们你想要实现什么(大局)。

    我想出了这样的办法,也许这就是你想要的

    package pack;
    
    class Parent {
    
        void a() {
            //my test
            try{
                throw new Exception();
            }
            catch (Exception e){
                //e.printStackTrace();
                if (!e.getStackTrace()[1].getClassName().equals("pack.Parent"))
                    throw new RuntimeException("method can be invoked only in Parrent class");
            }
    
            //rest of methods body
            System.out.println("hello world");
    
        }
    
        public void b(){
            a();
        }
    
        //works here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }
    class Child extends Parent{
        //RuntimeException here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }
    class Child2{
        //RuntimeException here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }
    

    忘了提到
    b
    方法在所有类中都可以正常工作。希望这就是你想要的。顺便说一句,如果您不想要RuntimeException,请尝试返回或抛出其他异常。

    @Dave:我想限制非子类访问受保护的方法。@Amingh然后生成方法private@Ivan这不是要求。子类可以访问我的方法。如果需要,我如何能够动态强制转换到调用的同一对象类型这个方法可能行得通,猜猜看?我该怎么做?是的,我让它受到保护,但要求父类的方法a()只能由子类访问。所以我必须在if块中进行一些检查。当它受到保护时,如何从其他类访问
    Parent.a()
    ?同一个包中的类也可以访问,但我想限制它们。嗯。。。你说得对。我从不使用包访问,所以我完全忽略了
    protected
    包括包访问。我在父类中有一个受保护的方法a()。我的要求是,只有子类可以访问此方法。子类与父类具有类似的行为。我可以通过在方法a()中放置一些签入来实现这一点,在方法a()中,我检查哪个类调用此方法。
    package pack;
    
    class Parent {
    
        void a() {
            //my test
            try{
                throw new Exception();
            }
            catch (Exception e){
                //e.printStackTrace();
                if (!e.getStackTrace()[1].getClassName().equals("pack.Parent"))
                    throw new RuntimeException("method can be invoked only in Parrent class");
            }
    
            //rest of methods body
            System.out.println("hello world");
    
        }
    
        public void b(){
            a();
        }
    
        //works here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }
    class Child extends Parent{
        //RuntimeException here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }
    class Child2{
        //RuntimeException here
        public static void main(String[] args) {
            new Parent().a();
            new Parent().b();
        }
    }