In Groovy, if class is not called why instantiation exception? - oop

When I execute the following experimental code subset at http://groovyconsole.appspot.com/
class FileHandler {
def rootDir
FileHandler(String batchName) {
rootDir = '.\\Results\\'+batchName+'\\'
}
}
//def fileHandler = new FileHandler('Result-2012-12-15-10-48-55')
An exception results:
java.lang.NoSuchMethodException: FileHandler.<init>()
When I uncomment the last line that instantiates the class, the error goes away.
Can someone explain why this is? I'm basically attempting to segregate the definition and instantiation of the class into 2 files to be evaluated separately. Thanks

I'm not sure of the exact implementation details behind http://groovyconsole.appspot.com/ (source linked to from there points to Gaelyk, which I've not looked over). I'd bet it's looking for a no-arg constructor for the class you've presented, in an effort to find something runnable. (note that if you provide that, it still won't work, as it wants a main() :/)
Running locally in groovyConsole will die a bit sooner, with the following error message:
groovy.lang.GroovyRuntimeException: This script or class could not be run. It should either:
- have a main method,
- be a JUnit test or extend GroovyTestCase,
- implement the Runnable interface,
- or be compatible with a registered script runner.
This is perhaps more descriptive and to the point. If you want to run some Groovy as a simple script, you'll need to supply a jumping-in point. The easiest way is an executable statement in your groovy file, outside of any class definition (e.g, uncommenting your instantiation statement). Alternatively, a class with a main method should do it. (see here).
If 2 files is how you want to break it up, you can save the class file def in one groovy file (e.g., First.groovy) and create a second (e.g., Second.groovy) with just your executable statements. (I believe the first one will be in the classpath automatically when you run groovy Second, if both are in same directory)

Related

Where is Java Main method defined in source?

Pardon me for asking this silly question. Where can i find java main method definition in java source? it is not in object class or system class? so how and where is it defined exactly?
public static void main(String[] args) {}
Where is Java Main method defined in source?
It is declared in a class. Conventionally, it is a top-level (i.e. non-nested) public class, but that is not a requirement. (A non-public class will work, and I think a static nested class will work too.)
How do you find the main method?
Use grep or similar to search the source code of your application.
Use your IDE's method search capability.
Read the application's user documentation or launch script.
Look for the main method in the index of the application's javadoc.
How does the java command find it?
It doesn't! You specify the fully qualified class name of the class containing the main method you want to use on the java command line. Alternatively, you can set the Main-Class attribute in a JAR file's manifest so that the user doesn't need to know the class name.
UPDATE - If you are looking for the code in the OpenJDK source tree that loads the entrypoint class, finds the main method and invokes it, it is all in "jdk8u/jdk/src/share/bin/java.c". Happy reading.
It's not defined anywhere as code (in the standard libraries).
The JVM expects to find it if you're running a class, and if it's not found you get an error. Therefore it's up to you to create a public static void main(String[] args) method if you want to run your class.
main method is the entry point of an application in java. All the java classes are packaged as libraries which will be used in any application. So class files are used as references instead of separte executable. You can't execute the java source code separately because there won't be any main method definition in java source code.

Having a common setup function for tests

So I have a script test.py
from nose.tools import with_setup
class Test:
#with_setup(setup_func, teardown_func)
def test(self):
print "Hello World"
Can I have setup_func() and teardown_func() defined in an init.py in the same directory as "test.py".
Basically, the objective is to have a common setup and teardown for a bunch of test cases.
There is nothing magical about setup_func and teardown_func. You can certainly do that by importing them from any module, if it called init.py or __init__. I would suggest you put it in some other module, say test_setup.py because you may not want to have those definitions at the package level of the test module, and "explicit is better than implicit" philosophy will be enforced.
ADDED: However, you have a bigger problem here: with_setup is useful only for test functions, not for test methods or inside of subclasses. As written, in your code, your setup function will not be called regardless where it is specified, see here.
If you intend to make your tests as class methods, you would have to rely on setUp and tearDown methods of the unittest. If you wish, you can inherit your test class from a special setup class and reuse this same special class again for different tests.

testNG : find which test classes will run before any of them are

I am working with testNG where I run an external test framework, receive the result data and assert it. To run the external test framework I need to set up a specification for which tests that should be run. To generate this specification I need to know which tests that are selected in the testNG .xml file.
The only way I could think of doing this is to parse the file manually. But I am hoping for a better solution than this.
Thanks for any answers!
//Flipbed
Edit:
My colleague found solutions to the problem.
In #Factory and #DataProvider annotated methods it is possible to add a parameter of the type ITestContext. Using the variable of that type, one can use the method .getAllTestMethods().
Create a new class that implements IMethodInterceptor. In this class one can override the method 'intercept'. The method takes a parameter of the type List which is a list of all methods that will be run by testNG.
If someone has any other suggestions feel free to add.
//Flipbed
The solution that we used was to number 2 in my edit. We implemented the IMethodInterceptor and used the methods list as well as the ITestContext to both view what tests will run and modify that list.

Does Matlab have something akin to a main method?

If I have a single Matlab source file (m-file) containing a class definition (classdef), is there any way to specify a particular set of code that is to be executed if I run the m-file? I mean the entire file, such as via the Run button in the IDE, from a shell, or from the Matlab command-line. I don't mean manually selecting the code to be executed.
Similar behaviour exists in Java with the static main method and in Python by having code outside the class definition (possibly inside a if __name__==__main__ block).
The short answer is "no"; MATLAB classdef M-files are just intended to define objects, not form complete programs.
The long answer is you might be able to get specific behavior out of your classdef function if, for example, you overload the constructor to take a flag specifying whether or not to "act like a variable" or "act like a program".
e.g.
classdef myClass
...
methods
function self = myClass(varargin)
if nargin == 1 && strcmpi(varargin{1},'run')
..... %run the program
else
..... %make the variable
OR you can make a static method called main:
methods (Static = true)
function main()
%enabes: myClass.main()
...
end
The IDE still won't know what to do with your M-file to "run it", but you can run it properly from the command line, or another M-file.
That last sentence is not 100% correct - as Egon pointed out below, you CAN make MATLAB's IDE run that code - use a "run configuration": http://www.mathworks.com/help/matlab/matlab_prog/run-functions-in-the-editor.html
There are a few ways you can do this:
You can create a "run configuration" (either as a script or a specific line of code). This will run whenever you click the run button (or press the run shortcut) from within your classdef file. The big drawback is that those run configurations are stored locally, so this is a nightmare when it comes to collaboration or working at multiple places. So personally, I'd recommend writing a script if you have a complicated run configuration. Mine are mostly called testMyClass where MyClass of course is the class you want to run.
If you don't require complicated code, you can also put everything in the Constructor of your object. If you check whether there are no arguments passed with if nargin == 0 ... end, that piece of code should be called whenever you 'run' the class file. However, you are somewhat limited in what you can do, as you could create infinite loops or an infinite chain of those objects being created if you are not careful. In the end, you will have just the object in your base workspace.
If you do require more complicated code or some code that makes some variables within the base workspace, it can be accomplished but it comes at great cost. Your code might end up a complete mess, so I advise against using this unless you have an extremely good reason otherwise. You can use the previous method and the evil functions evalin and assignin to evaluate and assign to variables in the base workspace.

How to enumerate all java classes in a directory in jython

I find it tedious to import a java class in jython because of the long package names. e.g. com.example.x.y.z.SomeClass. It's a lot of typing. I want to import by using only the simple name of the class (SomeClass in my example). Is there a way of achieving it? Current solution in my head is to enumerate all the classes in the classpath and prepare a map of simple class name to the complete package name like -
SomeClass -> com.example.x.y.z.SomeClass
SomeOtherClass -> com.example.p.q.r.SomeOtherClass
etc. Then call a function something like below-
def intelligent_import(simple_class_name):
#No error checking, simplified for clarity
package_name = dict[simle_class_name]
simple_class_name = __import__(package_name)
for each of the classes. The only problem is that I don't know a method to enumearte all classes in a directory. Is there's a better way to do this? If not a method to enumerate all java classes in a directory will do.
This is a bit of a hack, I'm sure there's better our there, but if everything else fails, remember that JAR files are just ZIPs, and that each class will have a .class file in the jar.
However, rather than hacking the import mechanism like this, I'd rather just use an IDE with proper autocompletion (i.e. Eclipse/PyDev, it's the only one that can do it reliably in my experience), and rock the ctrl-space.