I'm making a command-line based tool in Java, and I was thinking I might be able to make it all a bit easier on my self if I could take user input and automatically find the needed functions based on the users input.
What I want to do, is, that if a user types:
User create newUser
The java code looks for a class called user, then looks for a function called create, and inputs newUser as the first argument of the function. Meaning typing the command "User create newUser" would make the java code trigger this line of code:
User.create("newUser");
And of cause, return errors if the class or function was not found, or so.
So far I've been playing with
Class.forName(cmdArg[0])
Where cmdArg[0] is the first word given by the Scanner, found by splitting where there's spaces.
Anyway, is it possible to achieve this in Java? Any help would be much appreciated.
My solution:
Okay, i got it working, my final solution was a combination of duffymo's and user978548's answer, my solution looks like this:
Class comArgs[] = new Class[1];
comArgs[0] = String.class;
String[] args = new String[1];
args[0] = commandArray[2];
Class.forName("code."+commandArray[0])
.getDeclaredMethod(commandArray[1], comArgs)
.invoke(null, args);
This is surrounded by a try/catch with allot of exceptions, but it works.
I also have a HashMap for which commands i want to receive.
The args variable can should be as long as the number of arguments needed for the called method.
You can use the features built into the java.lang.Class class:
Class.forName(args[0]).newInstance();
Have your users input the fully-resolved class name and you don't have to worry about all those shenanigans. You'll need them anyway, because the short name might not be unique.
Another approach is to put the Class instances that you want users to be able to create in a Map and have them input the key.
as duffymo said, Class.forName(args[0]).newInstance(); for the class, and as Chin Boon said, you have all that you want in reflections method. Like, to run your method:
Object.class.getMethods()[find your method here].invoke(obj, args)
What you are looking for is Java reflection.
duffymo is correct - reflection is what you are prob talking about.
However, I would maybe suggest looking at a combination of Builder/Factory design patterns to make this a little nicer rather than using reflection to attempt to find the class/methods you want (although obviously this depends on the context of the problem and I am making some assumptions here!).
Related
I am working a small program which can receive several commands. Each of these commands should cause different methods to run. I was thinking that if there were a way to put all the methods into a HashMap and invoke them directly by getting the value paired with the command Key instead of using if statements, it could make things much simpler but as far as I understand this is not possible since methods aren't treated as objects in Java. Still, it will be educative to find out if there is a way to do this.
Methods aren't objects (at least mostly not), but there is a concept that matches what you want: the functional interface, which is defined as an interface that has exactly one abstract method. Two out-of-the-box candidates are Runnable, which takes no parameters, and Consumer, which takes a single parameter and might be the best option if you want to pass in some kind of input (like a Scanner). (If you also want a configurable output, BiConsumer taking a Scanner and a PrintWriter might be suitable.)
Java has a convenience feature called method references that can automatically transform a method into an instance of a functional interface. Put together, it might look like this:
Map<String, Consumer<Scanner>> commands = new HashMap<>();
...
commands.put("foo", someCommand::go); // where someCommand is a variable with a go(Scanner) method
commands.put("bar", new OtherCommand());
commands.put("hello", unused -> { System.out.println("Hello!"); });
...
String commandName = scanner.next();
commands.get(commandName).accept(scanner);
This is not a good idea, make methods as hashmap value don't satisfied shell command complex scene, maybe you can use Runnable Object as value.
Another solution, you can use Spring Shell.
#ShellMethod("commandName")
public String doSomething(String param) {
return String.format("Hi %s", param);
}
I am trying to automate testing for a program written in Java. The problem I have is that there needs to be a way to add/remove more tests cases without changing the code.
For example, you have two strings and want to check if they are within a list of strings. You pass the the strings over and check. In Java code, I could make an if statement and check if the two strings match some string in a given list. However, if I wanted to add more strings to search for from that list, I would have to go back to the program and add more code.
Probably a bad example, but I hope you get my point. Of course, there can be many more test cases that might be completely different. And if I wanted to give the program to someone else, they might want to add their own as well.
I was thinking to create some kind of template with arguments and method names to call that's outside of the code. Basically a file with rules. The Java code will then interpret what to do with the given rules. I was reading this: https://blog.codecentric.de/en/2016/01/robot-framework-tutorial-2016-keywords/ but wasn't really understanding it.
My goal is to write some generic Java code that can interpret a template file and run the test cases defined in there. Any help would be appreciated, thank you!
Example:
Template file
Details: Checks if String in database
Method: testID
Arguments: Hello
Foobar.java
public class Foobar {
public void testID(String str1)
{
// Expected output will be taken from a database
Assert.assertEquals(str1, expected_output);
}
}
The String str1 can be taken from the template file under Arguments.
The problem with this is that if I now want to test if str1 is a certain length, I would have to go back and add more code. This would be fine if the program was just for me and my team. However, when you give it to another company or person that doesn't know how to code but they want to run their own tests, it won't be that functional. That's why I was hoping, a person not on the team could just add their test case given they follow the format of the template and the Java program will know what to do with it.
I'm sorry I don't know how to explain it that well. Hope it's not confusing.
As far as I understand your question,
You are looking to give a generic framework to whole lot of users where each of them would want it to use it in there own way!
My friend this is where "Keyword driven framework" come in to picture!
To be more specific as you said you write a function to "check string in a list" but if someone wants to "check the length" then u have to edit your function again!!
All you need to do is analyze the different operations that you have to support.
Say, check for string in list, check length of string, check size of list, check string not in list, add string to list etc.
So instead of you writing a function with mixture of operations! you make it modular instead and give it back to the users to use it in their own way!
So if you declare the above 4 keywords (functions). they can call it in the order they want
e.g. test cases would then look like:
test1:
check for string in list <string1>
test2:
check length of string <string1>
check size of list
check for string not in list <string1>
You define templates for each of these functions and pass on info to users! so that can use these modularized keywords as they want it. This is how robotframework is done!
Hope it is useful!
There are many ways to create and read test resources for use in Java.
You can use straight text strings in .txt files, xml formats, comma delimited formats, etc.
It will really depend on the amount and depth of test data that you are trying to inject.
There are many questions/answers here that you can search for on how using resource files or other file read methods.
Here is an example: How should I load files into my Java application?
I would like to save some work on my app, is it possible to get the string, for example "level1" and then use the corresponding function, which would be level1();? my main point is not to make a huge switch-case statement, but only make a few level functions in a storage class, and whenever you level up, the string would change to "level" + number where number is the int, so lets say that right now you are in level 10, the function that would run is level10();
I hope i explained it clearly.. sorry if not.. hope you get the idea!
Thanks!
I believe you want to call a method at runtime using its name as a string.
You can do it via reflection.
Class.getMethod(String methodName, Class... parameterTypes)
Don't think of this in terms of method names, unless you want to muck around with reflection (you don't want to, and it's not necessary).
If you really do need to convert strings to method calls – and that's a big "if" – create a Map<String, Foo> where Foo implements some "callable"-like interface. Then a string-to-method lookup is simply:
Map<String, Foo> commands = /* ... */;
Foo foo = commands.get("level42");
foo.bar();
It really sounds like you should just have a
void setLevel(int level)
call. That can feel free to ignore (say) levels 11-14 or whatever... but it would be very ugly to have separate methods and invoke them by name. You can do so with reflection, but you should think about other options first.
Please see the top answer to this post:
Java dynamic function calling
I would also recommend following their advice regarding structure, to create a more object-oriented solution instead of using reflection.
This is a complicated question which i will find hard to explain so i appologise in advance.
Imagine an application that invokes another projects methods. I need a way of generating data to match the parameter list. Obviously if the parameter types are of some class that I have no way of generating then it should fail but if its an int[] and int[][] a List<String> a Map<Integer, String> then it should be possible.
What i am struggling with is a decent approach for solving this. I can get the types of parameters via method.getGenericParameterTypes(); example for the parameter HashMap would be java.util.HashMap<java.lang.String, java.lang.Integer> but there are quite a lot of different possibilities right!
I assume generics has some use here? The only issue with that being I have no control over the code that is being invoked. How can I use one of these types and then generate data for it?
I am sorry for the poor explanation, any help appreciated
Thanks
Here we see an example of a method i want to invoke, I want to record how long it takes to run (this is being done via reflection) however, i need to generate data for the parameters. I need a way of generating data to match
public void someMethod(Param a, Param b, Param c)
{
//some user code I have no control over
}
I think perhaps the question should be why you need to do this. Perhaps if you explained your use/business case, we can provide a cleaner (and easier) solution.
From your very last lines, you are talking about wanting to profile the method. Generally speaking, it is rare to want to profile just one small tiny method in the middle of chain of processing. That being said, I can imagine some convoluted cases where this might occur. However, even at that, it should be a handful of cases that you would be able to code yourself.
If it is a question of actual profiling, but you are not sure how to do it, and consequently feel that the only way is to call each method individually so you can "wrap" it with a start/stop timer, I would strongly recommend looking into AOP. Both Spring and AspectJ are great for AOP, with AspectJ able to do byte-weaving that Spring is unable to accomplish.
Consequently, with AspectJ you would be able to create your own profiling timer classes and weave them into the byte-code at compile time and then run your standard tests, but visualize all the profiling info that you want/need on a per-method basis.
And best of all, it would avoid you needing to come up with some convoluted scheme for producing random test data that isn't really relevant to the method being tested.
Sounds like a horrible problem.
If you have the class[] that represents the parameter types of the method then can't you create a new instance of each and pass that as the arguments list to the method? This is untested:
Class[] parameterTypes = method.getGenericParameterTypes();
Object[] args = new Object[parameterTypes.length];
for (int i=0; i < parameterTypes.length; i++) {
args[i] = parameterTypes[i].newInstance(); // assuming the classes can be instantiated without params
}
method.invoke(obj, args); // I assume you've instantiated the object already
I need to change the signature of a method used all over the codebase.
Specifically, the method void log(String) will take two additional arguments (Class c, String methodName), which need to be provided by the caller, depending on the method where it is called. I can't simply pass null or similar.
To give an idea of the scope, Eclipse found 7000 references to that method, so if I change it the whole project will go down. It will take weeks for me to fix it manually.
As far as I can tell Eclipse's refactoring plugin of Eclipse is not up to the task, but I really want to automate it.
So, how can I get the job done?
Great, I can copy a previous answer of mine and I just need to edit a tiny little bit:
I think what you need to do is use a source code parser like javaparser to do this.
For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitor as base class, and override (at least) visit(MethodCallExpr, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.
I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).
Eclipse is able to do that using Refactor -> Change Method signature and provide default values for the new parameters.
For the class parameter the defaultValue should be this.getClass() but you are right in your comment I don't know how to do for the method name parameter.
IntelliJ IDEA shouldn't have any trouble with this.
I'm not a Java expert, but something like this could work. It's not a perfect solution (it may even be a very bad solution), but it could get you started:
Change the method signature with IntelliJ's refactoring tools, and specify default values for the 2 new parameters:
c: self.getClass()
methodName: Thread.currentThread().getStackTrace()[1].getMethodName()
or better yet, simply specify null as the default values.
I think that there are several steps to dealing with this, as it is not just a technical issue but a 'situation':
Decline to do it in short order due to the risk.
Point out the issues caused by not using standard frameworks but reinventing the wheel (as Paul says).
Insist on using Log4j or equivalent if making the change.
Use Eclipse refactoring in sensible chunks to make the changes and deal with the varying defaults.
I have used Eclipse refactoring on quite large changes for fixing old smelly code - nowadays it is fairly robust.
Maybe I'm being naive, but why can't you just overload the method name?
void thing(paramA) {
thing(paramA, THE_DEFAULT_B, THE_DEFAULT_C)
}
void thing(paramA, paramB, paramC) {
// new method
}
Do you really need to change the calling code and the method signature? What I'm getting at is it looks like the added parameters are meant to give you the calling class and method to add to your log data. If the only requirement is just adding the calling class/method to the log data then Thread.currentThread().getStackTrace() should work. Once you have the StackTraceElement[] you can get the class name and method name for the caller.
If the lines you need replaced fall into a small number of categories, then what you need is Perl:
find -name '*.java' | xargs perl -pi -e 's/log\(([^,)]*?)\)/log(\1, "foo", "bar")/g'
I'm guessing that it wouldn't be too hard to hack together a script which would put the classname (derived from the filename) in as the second argument. Getting the method name in as the third argument is left as an exercise to the reader.
Try refactor using intellij. It has a feature called SSR (Structural Search and Replace). You can refer classes, method names, etc for a context. (seanizer's answer is more promising, I upvoted it)
I agree with Seanizer's answer that you want a tool that can parse Java. That's necessary but not sufficient; what you really want is a tool that can carry out a reliable mass-change.
To do this, you want a tool that can parse Java, can pattern match against the parsed code, install the replacement call, and spit out the answer without destroying the rest of the source code.
Our DMS Software Reengineering Toolkit can do all of this for a variety of languages, including Java. It parses complete java systems of source, builds abstract syntax trees (for the entire set of code).
DMS can apply pattern-directed, source-to-source transformations to achieve the desired change.
To achieve the OP's effect, he would apply the following program transformation:
rule replace_legacy_log(s:STRING): expression -> expression
" log(\s) " -> " log( \s, \class\(\), \method\(\) ) "
What this rule says is, find a call to log which has a single string argument, and replace it with a call to log with two more arguments determined by auxiliary functions class and method.
These functions determine the containing method name and containing class name for the AST node root where the rule finds a match.
The rule is written in "source form", but actually matches against the AST and replaces found ASTs with the modified AST.
To get back the modified source, you ask DMS to simply prettyprint (to make a nice layout) or fidelity print (if you want the layout of the old code preserved). DMS preserves comments, number radixes, etc.\
If the exisitng application has more than one defintion of the "log" function, you'll need to add a qualifier:
... if IsDesiredLog().
where IsDesiredLog uses DMS's symbol table and inheritance information to determine if the specific log refers to the definition of interest.
Il fact your problem is not to use a click'n'play engine that will allow you to replace all occurences of
log("some weird message");
by
log(this.getClass(), new Exception().getStackTrace()[1].getMethodName());
As it has few chances to work on various cases (like static methods, as an example).
I would tend to suggest you to take a look at spoon. This tool allows source code parsing and transformation, allowing you to achieve your operation in a -obviously code based- slow, but controlled operation.
However, you could alos consider transforming your actual method with one exploring stack trace to get information or, even better, internally use log4j and a log formatter that displays the correct information.
I would search and replace log( with log(#class, #methodname,
Then write a little script in any language (even java) to find the class name and the method names and to replace the #class and #method tokens...
Good luck
If the class and method name are required for "where did this log come from?" type data, then another option is to print out a stack trace in your log method. E.g.
public void log(String text)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
new Throwable.printStackTrace(pw);
pw.flush();
sw.flush();
String stackTraceAsLog = sw.toString();
//do something with text and stackTraceAsLog
}