My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Home  
Updated Feb 4, 2010 by olivier.chafik@gmail.com

Presentation

Jeneral is a pure-Java class templates framework, that uses annotations processors.

The project is still in its early research phase. Feel free to contribute any comment / suggestion through the issues tab.

It aims at bridging the gap between Java's poor type-erasure generics and more powerful abstraction syntaxes found in other languages, such as C#'s generics or C++'s templates.

Jeneral may be used to create :

  • reified generics (that is, generics that "remember" the class they were created with during runtime)
  • generics that operate seamlessly on primitive and non-primitive types (including when it comes to creating arrays)
  • compile-time java templates with both class and constant parameters (in the C++ template style), letting programmers go much beyond GNU Trove-style classes generation
  • arbitrary code generation through inline velocity scripts

All this is done through an unified syntax with the same template class. A template class is a real pure-Java class, hence easily refactorable and with full support from IDEs such as Eclipse and Netbeans.

It also features some goodies such as a @Property annotation (generates getters and / or setters and / or initialization in constructors) or @ImplementMissingMembers (to avoid having to write all those dummy methods some interfaces feature when you only need to implement a single one).

Why this name ?

Jeneral lets Java programmers write generic abstractions that generate files (makes code genera(liza)tion easier :-D)

Simple Meaningful Example

import com.ochafik.lang.jeneral.*;
import com.ochafik.lang.jeneral.annotations.*;

import javax.swing.JLabel;

// Declare that ElementsBuild is a template.
// It has to be abstract and to implement ElementsBuilder_Template, which is autogenerated on the fly in Eclipse, NetBeans or with the apt tool in Sun's JDK
@Template
public abstract class ElementsBuilder<T> implements ElementsBuilder_Template<T> {
	
	// Trigger generation of getters and setters for the 'arg' property, and append it to all ElementBuilder's factory methods
	@Property(fromConstructor = true)
	String arg;
	
	// Declare that T must have a constructor T(String) that throws no checked exception, and that it should be accessible as a new_T method :
	@ParamConstructor
	abstract T new_T(String arg);
	
	public T buildElement() {
		return new_T(getArg());
	}
	
	public static void main(String[] args) {
		// There is one newInstance method for each public constructor of the template class
		// Each newInstance method has the following arguments :
		//   - classes of generic template parameter 
		//   - constant template parameters, if any (here Test has no constant template parameter)
		//   - arguments of the constructor that corresponds to this newInstance method (here String arg, which was added implicitely to the unique Test() constructor by the @Property(fromConstructor = true) annotation)
		ElementsBuilder<JLabel> labelBuilder = ElementsBuilder_Template.Factory.newInstance(JLabel.class, "Default Label Text");
		System.out.println(labelBuilder.buildElement());
	}
}

Credits

Jeneral would be nothing without the amazing Spoon library, which it needs to work. Make sure you read the License page for more information.

Powered by Google Project Hosting