简体   繁体   English

有没有办法在没有反射的情况下从不同的运行时包访问受保护的类成员?

[英]Is there a way to access protected class members from different runtime package without reflection?

I'm making a JCL logging bridge that operates over several (let's say two) logging implementations (like log4j).我正在制作一个 JCL 日志桥,它可以在多个(假设是两个)日志实现(如 log4j)上运行。 Depending on configuration of all these implementations the bridge than makes custom global decision whether to log and than logs to all or none of the underlying implementations.根据所有这些实现的配置,桥接器会做出自定义的全局决定,是记录所有底层实现还是不记录所有底层实现。

Logging implementations usually have methods like this:日志实现通常有这样的方法:

  • public isLevelEnabled() to test "loggability" public isLevelEnabled()来测试“可记录性”
  • public logLevel(msg) which use the test methods and eventually logs messages via -> public logLevel(msg)使用测试方法并最终通过 -> 记录消息
  • protected logInternal(lvl, msg) which logs without additional tests受保护的logInternal(lvl, msg)无需额外测试即可记录

Now since I make custom decision if level is enabled, I cannot use the public log methods that make the standard decision and I need to use the protected method directly.现在,由于我在启用级别时做出自定义决定,因此我无法使用做出标准决定的公共日志方法,我需要直接使用受保护的方法。

I know, that I can access the protected members from the same package so I added an "intruder" class with the same package as the logger class of target implementation:我知道,我可以从同一个包访问受保护的成员,所以我添加了一个“入侵者”类,该类与目标实现的记录器类具有相同的包:

package com.log.lib;

//logging library logger in dependency jar
public class Logger {
  protected void logInternal(int lvl, String msg){
    //library logging without additional level testing
  }
}
package com.log.lib;

//my class to access protected method of library logger
public class LoggerAccessor {
  public void log(Logger logger, int lvl, String msg) {
    logger.logInternal(lvl, msg);
  }
}

This worked fined until I ran the code on an application server which provides one of the logging implementations loaded by different classloader than my application.这一直很好,直到我在应用程序服务器上运行代码,该服务器提供由与我的应用程序不同的类加载器加载的日志记录实现之一。 I got an IllegalAccessException .我得到了一个IllegalAccessException So this is how I learned about runtime package s (eg quick explanation here ).所以这就是我了解运行时包的方式(例如这里的快速解释)。

I can invoke the method via reflection by granting accessibility and I even have an automatic test of runtime package and select between direct invocation and reflection.我可以通过授予可访问性来通过反射调用该方法,我什至可以自动测试运行时包并在直接调用和反射之间进行选择。 But the reflection is always used on the application server where the application runs.但是反射总是在应用程序运行的应用程序服务器上使用。 And since logging is an extensively used activity the reflection is kind of bottleneck here.由于日志记录是一项广泛使用的活动,因此反射在这里是一种瓶颈。

So I ask: Is there a better (faster) way to invoke the protected method from different runtime package (or to log eg via log4j without its decision making)?所以我问:有没有更好(更快)的方法来从不同的运行时包调用受保护的方法(或者在没有决策的情况下通过 log4j 进行记录)?

You could change the access to public by creating you own version of the code, or您可以通过创建自己的代码版本来更改对 public 的访问,或者

you could use a sub-classed Logger instead.您可以改用子类 Logger。

The simplest solution is likely to be to use reflection.最简单的解决方案可能是使用反射。 I would check your application server doesn't prevent this as well.我会检查您的应用程序服务器也不会阻止这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类无法使用来自不同包的反射来访问其自己的受保护的成员变量 - Class can't access its own protected member variable using reflection from different package 访问控制 - 保护成员不受包外的影响 - Access control — protected members from outside the package 在不使用public的情况下访问不同包中的受保护的类变量 - Access protected class variable in different package without using public 使用Java反射从包外部访问非公共类的成员 - Access members of a non public class from outside the package using java reflection 程序包访问类|(在公共和受保护之间有所不同) - package access class |(different between public and protected) 在Java中是否可以在不使用反射的情况下从其他非子类调用受保护的方法? - Is it possible in Java to call a protected method from a different non-child class without using reflection? 使用反射访问受保护的类 - access protected class using reflection 如何从另一个 package 访问受保护的内部 class? - How do I access a protected inner class from another package? 有没有办法在有或没有反射的情况下访问内部类中的私有方法 - Is there a way to access private method in inner-class with or without reflection Java反射,从不同的包创建私有类的对象 - Java Reflection, creating object of private class from different package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM