开发者

Can i Call methods From Properties File in Java

开发者 https://www.devze.com 2023-03-25 03:25 出处:网络
I am reading an Excel file using java, I have written some methods for my business logic and i want to implement using properties File.

I am reading an Excel file using java, I have written some methods for my business logic and i want to implement using properties File.

What i want to Do is :

I am having 10 Columns and 10 methods, i need to declare this meth开发者_开发知识库ods in Properties file use from that file.

Suppose for column A i want only 2 methods then i can use only those 2 methods, For next column B suppose 10 methods and So on.

Can i Do this or is there any other way to implement this.

Please Suggest me if there is any other way to implement this. Thanks in Advance.


To prevent misunterstandings: properties files are just text files, so you need to use Java code in order to process them. They'll only provide a String->String (key->value) mapping, thus you need to parse and interpret the strings yourself.

That being said, here's a suggestion on what you might (want to) do:

Since your question isn't that clear I'll make a couple of assumptions:

  1. Your properties file contains something like:

    column1=method1,method7
    column2=method1,method2,method3
    etc.

  2. The methods are all declared in one class using a common signature

  3. When parsing your excel file you might want to apply the methods to the columns based on the properties file.

So, here are some hints on what you could do:

  1. Parse the properties file and create a list of method names per column
  2. When working on a cell/column get the method names for the respective column
  3. Use YourClass.class.getMethod(methodName, parameterTypes) to get the Method instances.
  4. Call Method#invoke(...) to invoke the method.

Edit:

As an alternative to having all methods in one class you could also use the fully qualified class name, e.g. yourpackage.YourClass#method1, split at # to get the class name and the method, then use Class.forName(fqcn) to get the class and finally call getMethod(...) on that.

If the signature differs, you might have to use a more complicated notation and parse the parameter classes as well.

There be parsers ready to use for this, but I don't know any. However, some apache commons projects like El and Configuration might prove useful for this task.


It's not at all clear what you are trying to do, but in any case you definitely cannot "call" any Java methods from a .properties file (or call from Java to a method contained in a .properties file). A .properties file is not executable, nor is it treated by any standard Java utility as executable/interpretable code. In essence, all it contains is data, and data cannot call or be called.

What you could do however is roll your own framework for doing this, either by using reflection to map the textual method calls in your input file to actual method calls against one or more Java objects, or more simply (but less flexibly/extensibly) by storing a predetermined set of codes which you map directly to method calls using a switch/if-else block, or perhaps by storing your desired code as JavaScript and using an existing Java-based JavaScript interpreter to execute the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号