Friday, August 21, 2009
Tuesday, August 11, 2009
eclipse booster plugin: Easy Hibernate mapping generation!
Eclipse Booster Plugin is an Open Source Hibernate mapping generator that supports mapping of relationships, inheritance hierarchies, and collection type attributes. Booster Plugin uses application model classes to obtain data and generate code, so generating an Hibernate mapping file it's really easy:
Step 1: Code your application's Java model classes
Step 2: Create a project configuration
Step 3: Select the class you want to map and attributes to persist
Step 4: That's all!
You can download it from google code: http://code.google.com/p/hexacta-booster/downloads/list
Enjoy!
Saturday, August 8, 2009
Is there a better way to programmatically retrieve a local variable IType from an EditorAction text selection in Eclipse?
public class MyEditorAction extends EditorAction {
/**
* @param unitEditor
* @return
*/
private IDocument getDocument(final CompilationUnitEditor unitEditor) {
IDocumentProvider provider = unitEditor.getDocumentProvider();
IEditorInput input = unitEditor.getEditorInput();
IDocument document = provider.getDocument(input);
return document;
}
public void runAction() {
if (getTargetEditor() instanceof CompilationUnitEditor) {
CompilationUnitEditor unitEditor = (CompilationUnitEditor) getTargetEditor();
ICompilationUnit varContainerUnit = (ICompilationUnit) unitEditor.getViewPartInput();
IDocument document = getDocument(unitEditor);
try {
JavaTextSelection javaSelection = new JavaTextSelection(varContainerUnit.getTypes()[0].getTypeRoot(),
document, getTextSelection().getOffset(), getTextSelection().getLength());
ILocalVariable localVar = (ILocalVariable) javaSelection.resolveElementAtOffset()[0];
String varQualifiedTypeName = Signature.getSignatureSimpleName(localVar.getTypeSignature());
//At this point with variable's container unit and qualified type name, use search engine to retrieve IType...
IType varType = SearchEngine.getVarType(varQualifiedTypeName, varContainerUnit);
}
}
Monday, August 3, 2009
How to write a DOM Document to an xml file in java 1.5
private void writeXMLDocument(final Document doc, final String fileName) throws IOException {
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos;
Transformer transformer;
fos = new FileOutputStream(xmlOutputFile);
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", new Integer(4));
try {
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Mapping DTD 3.0//EN");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
} catch (TransformerConfigurationException e) {
logger.error("Transformer configuration error: " + e.getMessage());
throw new IOException(e.getMessage());
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new OutputStreamWriter(fos, "utf-8"));
// transform source into result will do save
try {
transformer.transform(source, result);
} catch (TransformerException e) {
logger.error("Error transform: " + e.getMessage());
}
}
How to: Simple system configuration reading and writing with Castor XML
Step 1: Create a Java object representing a system configuration.
Step 2: Add Castor Maven dependency to project's pom.
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-xml</artifactId>
<version>1.3</version>
</dependency>
Step 3: Create castor.properties file with org.exolab.castor.indent=true.
Step 4: Read system configuration:
FileReader reader = new FileReader("system.cfg.xml");
SystemConfiguration systemConfiguration = (SystemConfiguration) Unmarshaller.unmarshal(SystemConfiguration.class, reader);
reader.close();
Step 5: Write system configuration:
FileWriter writer = new FileWriter("system.cfg.xml");
Marshaller.marshal(aSystemConfiguration, writer);
writer.close();
Sunday, August 2, 2009
Log4j Eclipse Code Template
If you are tired of manualy adding static logger instances in classes, try this template ...
Under Preferences > Java > Editor > Templates:
- Click new.
- Give it a description and name.
- Use this Pattern:
${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

To apply it, type the template name and press ctrl + space.
Commonclipse plugin: A recurrent method generator.
Commonclipse is an eclipse plugin for jakarta commons-lang users. It features automatic generation of the following methods using commons-lang builders:
- toString()
- hashcode()
- equals(Object)
- compareTo(Object)
Commonclipse supports customization of generated methods (custom ToStringStyle, use of appendSuper(), excluded fields) through user defined properties. Commonclipse actions are available both in java editor views and in package explorer.

WebSite:
Eclipse Booster Plugin: A private method testing tool.
When using reflection aproach to junit testing private methods it can become an error-prone, tedious and time consuming task. A private method call via Java Reflection API involves construction of formal and actual parameters as also method name specification as a string literal. Booster Plugin generates that code for you with just a few clicks.

Web Site:
Subscribe to:
Posts (Atom)




