简体   繁体   English

如何使用默认访问权限(或默认包)访问的构造函数

[英]How to access a constructor of with default access (or package-default)

I'm trying to instantiate the constructor of a class which I imported as a Maven dependency via it's coordinates. 我正在尝试实例化通过其坐标作为Maven依赖项导入的类的构造函数。 The problem I have is that the particular constructor of this class, is invisible to me because it has no access modifier associated with it so it is default, meaning I can't access it from outside. 我的问题是该类的特定构造函数对我不可见,因为它没有与之关联的访问修饰符,因此它是默认的,这意味着我无法从外部访问它。

I know there is a way to access private methods via reflections, using getDeclaredMethod() method of class Method, but this doesn't work for constructors (please correct me if I'm wrong). 我知道有一种方法可以使用Method类的getDeclaredMethod()方法通过反射访问私有方法,但这不适用于构造函数(如果我错了,请纠正我)。

The class I'm trying to use is here: 我要使用的班级在这里:

public class DecisionTableBuilder {

   // Notice no access modifier here so it's package-default
   DecisionTableBuilder(Log log, File in, File out) {
      some stuff ...
   }

   // public constructor
   public DecisionTableBuilder() {}

   // Method 1
   public void compiler(File schema) {
      some stuff ...
   }

   // Method 2
   public void linker(File attribute) {
      some stuff ...
   }
}

Here is my toplevel in a separate project: 这是我在一个单独项目中的顶层:

public class TopLevel {

   public void testDecisionTableBuilder() {

      // I get an error saying the constructor DecisionTableBuilder is not visible
      DecisionTableBuilder builder = new DecisionTableBuilder();

      // This works just fine, but no constructor...
      DecisionTableBuilder builder2;

      // This doesn't really work
      Method[] m = DecisionTableBuilder.class.getDeclaredMethods("DecisionTableBuilder", "Log", "File", "File");

   }
}

How can I access the Constructor and methods in a toplevel class which I created in a new project? 如何在新项目中创建的顶级类中访问构造函数和方法? Any assistance would be much appreciated 任何帮助将不胜感激

EDIT 编辑

public File graphDir;
public File outputDir;
public Log log;

Constructor<DecisionTableBuilder> constructor = DecisionTableBuilder.class.getDeclaredConstructor(Log.class,File.class,File.class);
constructor.setAccessible(true);
DecisionTableBuilder builder =constructor.newInstance(log, graphDir, outputDir);

Would this be correct? 这是正确的吗?

You cannot access constructors with getDeclaredMethod or getDeclaredMethods . 您不能使用getDeclaredMethodgetDeclaredMethods访问构造函数。 The Java reflection mechanism distinguishes between methods and constructors, and has separate methods for accessing them. Java反射机制区分方法和构造函数,并具有用于访问它们的单独方法。

Try the getDeclaredConstructors method , 试试getDeclaredConstructors方法

Constructor[] c = DecisionTableBuilder.class.getDeclaredConstructors();

or for a specific constructor, getDeclaredConstructor . 或对于特定的构造函数, getDeclaredConstructor Pass in the Class objects representing the parameter types, not the string names of the classes. 传入表示参数类型的Class对象,而不是类的字符串名称。

Constructor<DecisionTableBuilder> constructor =
    DecisionTableBuilder.class.getDeclaredConstructor(Log.class, File.class, File.class);

You will want to set it accessible and then call newInstance to create a DecisionTableBuilder .' 您将需要将其设置为可访问,然后调用newInstance来创建DecisionTableBuilder

constructor.setAccessible(true);
DecisionTableBuilder dtb = constructor.newInstance(yourLog, inFile, outFile);

You'll of course need to catch the several exceptions that these reflection calls can throw. 您当然需要catch这些反射调用可能引发的几个异常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM