Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

I have created a project in eclipse with the google plugin that should run on the app engine. Therefore it uses the datanucleus framework and JDO. Because of eclipse everytime you change a class in the build path, the datanucleus Enhancer runs and enhances the classes that should be persistent (capable or award).

I wrote a test case to test some code using the annotations @RunWith and @Test, plus some more for the spring configuration (@ContextConfiguration and @Autowired). When I saved the first version of the test (it compiled successfully at last) I noticed that on the console there was a warning from datanucleus enhancher:

11 Ιουν 2009 12:49:02 μμ org.datanucleus.metadata.annotations.AnnotationManagerImpl getMetaDataForClass
WARNING: Class "the class name" has annotations but there is no registered AnnotationReader. Please check your CLASSPATH and the annotations in the class for validity.

It seems that JPOX (the base of datanucleus) supports plugins for annotations through an AnnotationReader interface. The desirable solution for my case were to tell datanucleus to ignore the test class through (for example) an annotation.

This post applies to eclipse IDE.

If you have an interface (say Test) with a method declared (say testMethod) and you create a new class that implements this interface, there will be an error, that the class you declared must implement the methods defined in the interface. There is also a quick fix available that implements this methods in respect to a code template. If you choose the quick fix the result will be like:


@Override
public String testMethod() {
// TODO Auto-generated method stub
return null;
}

Say, you are working in a team (this is the default situation for a company), and you forget to implement the method. The result will be in the best case will be a null pointer exception somewhere in the code, but surely not in the method above. The return null is not the best solution for the default implementation. Even worse, the method could return an int value, then the default return value from the template would be 0, so no error is visible at runtime...

It would be better if the code would show exactly where the problem is (that means in the method testMethod) and even better, who is responsibly for this error. Both tasks are covered with the following code:

@Override
public String testMethod() {
// TODO Auto-generated method stub
throw new IllegalStateException("User X didn't do his work!");
}

This piece of code cures both problems. An exception is thrown, so everyone knows where the error is, and even better, they know who is responsible for this.

To change the template in eclipse go to eclipse preferences Java > Code Style > Code Templates
There in the tree select Code > Method body.
Press Edit... and change the pattern in

@Override
// ${todo} Auto-generated method stub
throw new IllegalStateException("${user} didn't do his work!");

Press OK and OK again and test the pattern.

If you are using log4j then you probably are tired of the copy/pasting the logger from on class to an other. There is a simple way to automate this by using the templates in the java editor in eclipse.


Try this out
  • open the eclipse Preferences
  • in the search field type template or follow the path: Java > Editor > Templates
  • There you can find any predefined templates you can use while editing a java file
  • press New...
  • In the name field type logger (this is the keyword you should type in the editor so that the code assistance activates the current temaplate). You could type log if you wish....
  • As pattern you can try this (of course you can change this as you wish by using the predefined variables in eclipse): private static final Logger logger = Logger.getLogger(${enclosing_type}.class);
  • ${enclosing_type} is a variable and returns the class name of the file that is being currently edited.
  • press OK and then again OK
  • try it out in a java class. Just write logger under the class declaration and press ctrl + space. Select logger from the assistant
  • press alt + shift + O to automatically import the log4j Logger class
The result should be something like this:


import org.apache.log4j.Logger;

public class ExampleClass {
private static final Logger logger = Logger.getLogger(ExampleClass.class);
}

Some time now I had a problem with running a dynamic web app project in eclipse as a web application (right click on project name and then RUN AS > RUN ON SERVER).

I had mostly spring enabled applications and the problem was that the application-context.xml file was not deployed in tomcat (in the webapps directory of tomcat, not the workspace direcory). The reason was that the xml file, and all other xml files were not copied in the build (in most cases bin) directory by eclipse. I couldn't find the reason for that.

After searching in internet I found this thread . I don't have the TPTP project installed in my eclipse, so I don't think that this is the reason. Another thread here.

The solution is simple although I don't know if there are any side effects.

"Change the Java/Compiler/Building preference, and remove *.xml from the filtered resources."

And that worked... at last.