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

QuickStart

Template source code : TArray.java

The code that follows defines a dummy generic array class.

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

@Template
public abstract class TArray<T> implements _TArray<T> { // _TArray is autogenerated by the compiler
	
	final Array<T> array;
	public TArray(int size) {
		array = T(size);
		System.out.println("Created a new " + T().getName() + "[" + size + "]");
	}
	public T get(int pos) { 
		return array.get(pos); 
	}
	public void set(int pos, T value) { 
		array.set(pos, value); 
	}
	public int size() { 
		return array.length(); 
	}

	@Property
	String simpleProperty;
	
	// This annotation triggers the generation of TArray<int>, which will be known as TArray__int :  
	@Instantiate(template = TArray.class, params = { @ClassParam(Integer.TYPE.class) })
	public static void main(String[] args) {
		int size = 20;
		
		// Create a reified array of int[] :
		TArray<Integer> reified = TArray.template.newInstance(Integer.TYPE.class, size);
		reified.setSimpleProperty("OK");
		
		// Create an instantiated array of int[] (much faster than the reified one, but with no class dependance to TArray) :
		TArray__int instantiated = new TArray__int(size);
		instantiated.setSimpleProperty("OK");
	}
}

In this very simple example usage of Jeneral, you can see that there is a class _TArray that is not explicitely defined.

It is autogenerated by Apt (a standard tool bundled with the JDK).

This autogeneration is done one-the-fly in IDEs such as Eclipse and Netbeans (just declare jeneral.jar as a Processor Factory Jar), or can be triggered manually with the following command line :

	apt -factoryPath jeneral.jar -cp jeneral.jar TArray.java 

For more info on how to setup your building process to use Apt, please see the TODO Installation Page.

Generated instance code : TArray_int.java

Note that special replacements are done for the Jeneral class com.ochafik.lang.jeneral.Array<T> :

  • Array<T> becomes T[]
  • Given a variable myArray of type Array<T> :
    • myArray.get(i) becomes myArray[i]
    • myArray.set(i, value) becomes myArray[i] = value
    • myArray.length() becomes myArray.length

public class TArray_int {
	final int[] array;
	public TArray(int size) {
		array = new int[size];
	}
	public T get(int pos) { 
		return array[pos]; 
	}
	public void set(int pos, T value) { 
		array[pos] = value; 
	}
	public int size() { 
		return array.length; 
	}

	String simpleProperty;
	
	public void setProperty(String simpleProperty) {
		this.simpleProperty = simpleProperty;
	}
	public String getProperty() {
		return simpleProperty;
	}
	
	public static void main(String[] args) {
		// ...
	}
}
Powered by Google Project Hosting