|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectsugar4j.Sugar4j<T>
public abstract class Sugar4j<T extends Sugar4j<?>>
Abstract base class for all sugar generating needs.
The only method to implement is Creator#doCreate(), which creates the content of
the sugar.
The template argument T is generally the same as the name of the Sugar4j subclass.
Something like:
public class MySugar extends Sugar4j<MySugar> {
}
It is suggested that users of Sugar4j introduce a subclass and further derive from that subclass
instead of deriving from Sugar4j directly. Something like:
abstract public class MyCompanySugar extends Sugar4j<MyCompanySugar> {
public MyCompanySugar() {
}
public MyCompanySugar(Object classs) {
super(classs);
}
}
public class MyModelSugar extends MyCompanySugar {
public void doCreate() {
...
}
}
The generated java code is not formatted in any way. If needed, it is up to the programmer to
format the result in a subsequent step. Formatting also includes new lines so most java code will
be written on one line. This makes it hard to read for, in example, a debugging session. You may
set the pretty print setPrettyPrint(boolean) flag so that the resulting code gets new
lines/indent spaces. Javadoc is always set on new lines.
Creator.doCreate()| Constructor Summary | |
|---|---|
Sugar4j()
Default constructor. |
|
Sugar4j(Object classs)
Constructs with given class name. |
|
Sugar4j(Object packagee,
Object classSimpleName)
Constructs with given package and simple class name. |
|
| Method Summary | |
|---|---|
T |
_add(Object s)
Adds raw content to the output. |
Date |
_addDefaultFileHeader()
Adds the default file header. |
T |
abstractt()
Yields abstract. |
T |
add(Object left,
Object right)
Math add or string concatenation +. |
T |
and(Object left,
Object right)
Conditional and &&. |
T |
annotate(Object annotation,
Object... values)
Annotates a java element |
T |
annotation()
Creates an annotation type using this getClassSimpleName() as the name. |
T |
annotation(String name)
Creates an annotation type. |
static String |
arrayAt(Object array,
Object index)
Access an array element at a given index. |
static String |
arrayInit(Object... initializers)
Returns an array initializer for the given elements. |
static String |
arrayLength(Object array)
Access the length member of the given array. |
static String |
arrayNew(Object classss,
Object... sizes)
Creates a new array with the given sizes. |
static String |
arrayNeww(Object classss,
Object... initializers)
Creates a new array and initializes it with the given initializers. |
static String |
arrayOf(Object classss)
An array of the given classss. |
T |
assertt(Object expr,
Object... message)
Yields an assert statement. |
T |
assign(Object... leftOrLeftAndRight)
Yields a =. |
T |
assignBitAnd(Object left,
Object right)
Yields a &= expression. |
T |
assignBitOr(Object left,
Object right)
Yields a |= expression. |
T |
assignBitXor(Object left,
Object right)
Yields a ^= expression. |
T |
assignDivide(Object left,
Object right)
Yields a /= expression. |
T |
assignLeftShift(Object left,
Object right)
Yields a <<= expression. |
T |
assignMinus(Object left,
Object right)
Yields a -= expression. |
T |
assignPlus(Object left,
Object right)
Yields a += expression. |
T |
assignRemainder(Object left,
Object right)
Yields a %= expression. |
T |
assignRightShiftSigned(Object left,
Object right)
Yields a >>= expression. |
T |
assignRightShiftUnsigned(Object left,
Object right)
Yields a >>>= expression. |
T |
assignTimes(Object left,
Object right)
Yields a *= expression. |
T |
attribute(Object classss,
String name,
Object... defaultt)
Adds an annotation attribute inside an annotation type declaration. |
T |
begin()
Begins a block. |
T |
beginCompilationUnit()
Begins this compilation unit. |
T |
bitAnd(Object left,
Object right)
Bitwise and &. |
T |
bitOr(Object left,
Object right)
Bitwise or |. |
T |
bitXor(Object left,
Object right)
Bitwise xor ^. |
T |
breakk()
Yields a break; statement, including the eos() call. |
T |
call(Object object,
String method,
Object... params)
Calls a method on a given object. |
T |
calll(String method,
Object... params)
Calls a method on this. |
T |
casee(Object expr)
Yields a case statement, including the colon (:). |
T |
cast(Object classss,
Object... expr)
Yields a cast. |
T |
catchAndRethrowAs(Object catchedExceptionClass,
Object asExceptionClass)
Adds a catch block which catches all exceptions of type catchedExceptionClass
and re-throws (wrap) the exception in a new asExceptionClass. |
T |
catchh(Object exClass,
String exName)
Adds a catch handler. |
T |
classs()
Adds a class using this getClassSimpleName(). |
T |
classs(String name)
Adds a class using the given name used primarly for inner classes. |
T |
complement(Object expr)
|
T |
concat(Object... exprs)
|
T |
constructor(Object... params)
|
T |
constructorr(String name,
Object... params)
|
T |
continuee()
|
T |
create()
|
T |
decr(Object expr)
|
T |
decrPre(Object expr)
|
T |
defaultt()
|
T |
div(Object left,
Object right)
|
T |
doc(String text)
Adds a comment line. |
T |
docAuthor(String author,
String... comment)
Adds an @autor tag. |
T |
docBegin()
Begins comment mode. |
T |
docEnd()
Ends comment mode and adds add("/").nl() |
T |
docParam(String name,
String comment)
|
T |
docReturn(String comment)
|
T |
docSee(Object classss,
String method,
Object... paramTypes)
|
T |
docSingleLine(String comment)
|
T |
docThrows(Object exception,
String comment)
|
T |
doo()
|
T |
dot()
|
T |
elsee()
|
T |
elseif(Object expr)
|
T |
empty()
|
T |
end()
|
T |
enumItem(String name,
Object... params)
|
T |
enumm()
|
T |
enumm(String name)
|
T |
enummm(Object... items)
|
T |
enummmm(String name,
Object... items)
|
T |
eos()
|
T |
eq(Object left,
Object right)
|
ExprBase<? extends ExprBase<?>> |
expr()
|
static String |
expr(Object obj)
Encloses the given obj within parens ( () ). |
T |
extendss(Object name)
|
T |
finall()
|
T |
finallyy()
|
T |
forr(Object init,
Object condition,
Object updater)
|
T |
forrr(Object indexFrom,
Object indexTo)
|
T |
forrr(Object itemClass,
String itemName,
Object iterable)
|
T |
ge(Object left,
Object right)
|
static String |
get(Object... names)
Access a member. |
boolean |
getAutoImport()
Returns whether to automatically import where required. |
Object |
getClasss()
Returns the full qualified name of the class that this class creates. |
String |
getClassSimpleName()
|
static String |
getClassSimpleName(Object classss)
Returns the simple, local name of the given full qualified class name. |
String |
getPackage()
|
static String |
getPackage(Object classss)
Returns the package name of the given full qualified class name. |
boolean |
getPrettyPrint()
Returns whether to include new lines / indent spaces to make the resulting code human readable. |
T |
gt(Object left,
Object right)
|
T |
iff(Object expr)
|
T |
ifff(Object test,
Object trueExpr,
Object falseExpr)
|
T |
implementss(Object... itfs)
|
void |
importAll(Object packagee)
|
String |
importStatic(Object name)
|
String |
importt(Object name)
|
T |
incr(Object expr)
|
T |
incrPre(Object expr)
|
T |
instanceoff(Object left,
Object right)
|
T |
interfacee()
|
T |
interfacee(String name)
|
T |
le(Object left,
Object right)
|
T |
leftShift(Object left,
Object right)
|
T |
lt(Object left,
Object right)
|
T |
mainn(String... argsName)
|
T |
method(Object returnType,
String name,
Object... params)
|
T |
minus(Object expr)
|
T |
minus(Object left,
Object right)
|
T |
ne(Object left,
Object right)
|
T |
neww(Object classss,
Object... params)
|
T |
next()
|
T |
nl()
|
T |
not(Object expr)
|
T |
or(Object left,
Object right)
|
T |
Override()
Adds a Override annotation. |
T |
packagee(String name)
|
T |
plus(Object expr)
|
T |
plus(Object left,
Object right)
|
T |
privatee()
|
static String |
propertyGet(Object propertyName)
|
static String |
propertySet(Object propertyName)
|
T |
protectedd()
|
T |
publicc()
|
T |
question()
Adds a question mark ( ? |
static String |
quote(char c)
Quotes/escapes the given char. |
static String |
quote(Object s)
Quotes/escapes the given string. |
T |
remainder(Object left,
Object right)
|
T |
returnn()
|
T |
returnn(Object value)
|
T |
rightShiftSigned(Object left,
Object right)
|
T |
rightShiftUnsigned(Object left,
Object right)
|
void |
setAutoImport(boolean autoImport)
Sets whether to automatically import where required. |
void |
setClasss(Object classs)
Sets the full qualified name of the class that this class creates. |
void |
setPrettyPrint(boolean prettyPrint)
Sets whether to include new lines / indent spaces to make the resulting code human readable. |
T |
staticc()
Adds static. |
T |
superr(String method,
Object... params)
Calls the given method on super. |
T |
superrr(Object classss)
Super of the given class used for wildcard type arguments. |
T |
SuppressWarnings(Object firstValue,
Object... moreValues)
Adds a SuppressWarnings annotation. |
T |
switchh(Object expr)
Adds a switch. |
T |
synchronizedd(Object expr)
Adds a synchronized. |
T |
throwss(Object... exceptions)
Adds a throws declaration at the end of a ctor/method declaration. |
T |
throww()
Adds a throw statement. |
T |
times(Object left,
Object right)
Adds a * operation |
File |
toJavaFile(File srcFolder)
Returns the source file for this classs. |
String |
toString()
Returns the content added so far (tags may not be replaced yet). |
T |
tryy()
|
static String |
typeArg(Object classss,
Object firstTypeArg,
Object... moreTypeArgs)
Adds generic type parameters to the given classss. |
static String |
typeArgExtendss(Object typeArg,
Object extendsClass,
Object... extendsInterfaces)
Adds generic wildcard type parameter. |
T |
typeArgg(Object... args)
Used to add type arguments in front of a method declaration. |
static String |
typeArgMethod(Object... typeArgsAndMethodName)
Returns a parametrized method name used for later invocation with call(). |
T |
var(Object classss,
Object nameOrExpr,
Object... init)
Adds a variable declaration (local variable, member variable, parameter...). |
static String |
varargs(Object classss)
Used to declare a 'varargs' method argument. |
T |
whilee(Object expr)
Adds a while. |
| Constructor Detail |
|---|
public Sugar4j()
Note that methods like classs(), enumm() and
interfacee() throw Sugar4jException if the classs
member is not intialized.
setClasss(Object)public Sugar4j(Object classs)
classs - The full qualified name of the class that this class creates.
public Sugar4j(Object packagee,
Object classSimpleName)
packagee - The package nameclassSimpleName - The simple (local) name of the class that this class creates.| Method Detail |
|---|
public static String propertyGet(Object propertyName)
public static String propertySet(Object propertyName)
public File toJavaFile(File srcFolder)
srcFolder - The base source folder, parent/ancestor folder for the given class.
.java)
public static String arrayAt(Object array,
Object index)
arrayIndex("arr", 0) --> arr[0]
array - index -
public static String arrayInit(Object... initializers)
arrayInit(0, 1, 2) --> {0, 1, 2}
initializers - May be empty in which case the empty initializer ({}) is
returned
public static String arrayLength(Object array)
length member of the given array.
Equivalent of:
get(array, length)
array -
JavaLang.length
public static String arrayNew(Object classss,
Object... sizes)
sizes.
arrayNew(String, 2) --> new String[2]
classss - Classname of the arraysizes - Size of the array. If length == 0, the array's size is not
specified ([]).
public static String arrayNeww(Object classss,
Object... initializers)
initializers.
arrayNeww(Integer, 1, 2, 3) --> new Integer[] {1, 2, 3}
classss - initializers -
public static String arrayOf(Object classss)
classss.
arrayOf(String) --> String[]
classss -
public static String expr(Object obj)
obj within parens ( () ).
expr("hello") --> (hello)
obj -
public static String get(Object... names)
.) in between.
get(String, classs) --> String.class
get("a", "b", "c") --> a.b.c
names - Array of names
public static String getClassSimpleName(Object classss)
getClassSimpleName("java.lang.String") --> String
classss - Full qualified name or local name of the class
public static String getPackage(Object classss)
getPackageName("java.lang.String") --> java.lang
classss - Full qualified name or local name (no package) of the class
public static String varargs(Object classss)
method() method.
method(voidd, "myMethod", varargss("String"), "names").begin();
{
}
end();
classss -
method(Object, String, Object...)public static String quote(char c)
c -
public static String quote(Object s)
s -
public static String typeArgMethod(Object... typeArgsAndMethodName)
call().
Example:
call("Collections", typeArgMethod(String, "emptyList") -> Collections.<String>emptyList()
typeArgsAndMethodName -
public static String typeArg(Object classss,
Object firstTypeArg,
Object... moreTypeArgs)
classss.
typeArg("List", String) --> List<String>
typeArg("Map", String, Integer) --> Map<String, Integer>
classss - Class to enrich with type parametersfirstTypeArg - First type argumentmoreTypeArgs - More type argument
public static String typeArgExtendss(Object typeArg,
Object extendsClass,
Object... extendsInterfaces)
classs(typeArg("MyClass", typeArgExtendss("T", "Runnable"))).begin();
{
}
end();
yields
class MyClass<T extends Runnable> {
}
typeArg - Name of the type parameter (in example T)extendsClass - Name of the class that typeArg extendsextendsInterfaces - More interfaces that typeArg extends/implements
public T _add(Object s)
Note that this method is not necessarily required to create valid java code. Usage of this method should be avoided.
s - Content to add.
thispublic T abstractt()
abstract.
this
public T add(Object left,
Object right)
+.
left - right -
this
public T and(Object left,
Object right)
&&.
left - right -
this
public T annotate(Object annotation,
Object... values)
annotation - Name of the annotation without the leading @ character.values - If length == 0, no values are added (marker annotation like
@Override). If length == 1, its the default value
value. If length > 1, pairs of [name]=[value].
this
Sugar4jException - if values.length > 1 and values are not
pairspublic T annotation()
getClassSimpleName() as the name.
thisannotation(String),
getClassSimpleName()public T annotation(String name)
name - Name of the annotation without the leading @ character
this
public T assertt(Object expr,
Object... message)
assert statement.
expr - boolean expressionmessage - Optional. If length > 0, the first element designates the
message for the assert statement.
thispublic T assign(Object... leftOrLeftAndRight)
=.
var(String, "s", expr().calll("toString").eos());
One could write:
var(String, "s").assign().calll("toString").eos();
leftOrLeftAndRight - if length == 0 only the = is yield. If
length == 1, its the left side of the assignment. If
length == 2 the second index is the right side of the assignment.
this
public T assignBitAnd(Object left,
Object right)
&= expression.
left - right -
this
public T assignBitOr(Object left,
Object right)
|= expression.
left - right -
this
public T assignBitXor(Object left,
Object right)
^= expression.
left - right -
this
public T assignDivide(Object left,
Object right)
/= expression.
left - right -
this
public T assignLeftShift(Object left,
Object right)
<<= expression.
left - right -
this
public T assignMinus(Object left,
Object right)
-= expression.
left - right -
this
public T assignPlus(Object left,
Object right)
+= expression.
left - right -
this
public T assignRemainder(Object left,
Object right)
%= expression.
left - right -
this
public T assignRightShiftSigned(Object left,
Object right)
>>= expression.
left - right -
this
public T assignRightShiftUnsigned(Object left,
Object right)
>>>= expression.
left - right -
this
public T assignTimes(Object left,
Object right)
*= expression.
left - right -
this
public T attribute(Object classss,
String name,
Object... defaultt)
classss - Class of the attributename - Name of the attributedefaultt - Optional. If length > 0, the first element designates the
default value for the annotation attribute
thispublic T begin()
thispublic T beginCompilationUnit()
doCreate() method.ImportMananger's import list tag.
thisCreator.doCreate()
public T bitAnd(Object left,
Object right)
&.
left - right -
this
public T bitOr(Object left,
Object right)
|.
left - right -
this
public T bitXor(Object left,
Object right)
^.
left - right -
thispublic T breakk()
break; statement, including the eos() call.
this
public T call(Object object,
String method,
Object... params)
object - Object to call. If null, the method is called on
this.method - Name of the methodparams - Arguments for the method
this
public T calll(String method,
Object... params)
this.
method - params -
thiscall(Object, String, Object...)public T casee(Object expr)
case statement, including the colon (:).
expr -
this
public T cast(Object classss,
Object... expr)
classss - expr - Optional. If length == 0 the classss is enclosed
within parens ( ()). If length > 0, the expression
is casted using Class#cast()
thisClass.cast(Object)
public T catchAndRethrowAs(Object catchedExceptionClass,
Object asExceptionClass)
catchedExceptionClass
and re-throws (wrap) the exception in a new asExceptionClass.
Example:
try {
...
} catch ([catchedExceptionClass] e) {
throw new [asExceptionClass](e);
}
catchedExceptionClass - asExceptionClass -
public T catchh(Object exClass,
String exName)
catch handler.
exClass - Exception classexName - Exception variable
public T classs()
getClassSimpleName().
public T classs(String name)
name -
public T doc(String text)
docBegin() and docEnd() blocks.
text -
public T docAuthor(String author,
String... comment)
@autor tag.
author - comment - Optional. If length > 0, the first element contains further
comments for the author
public T docBegin()
public T docEnd()
add("/").nl()
public T docParam(String name,
String comment)
public T docReturn(String comment)
public T docSee(Object classss,
String method,
Object... paramTypes)
public T docSingleLine(String comment)
public T docThrows(Object exception,
String comment)
public T complement(Object expr)
public T concat(Object... exprs)
public T constructor(Object... params)
public T constructorr(String name,
Object... params)
public T continuee()
public T create()
public T decrPre(Object expr)
public T decr(Object expr)
public T defaultt()
public T div(Object left,
Object right)
public T doo()
public T dot()
public T elsee()
public T elseif(Object expr)
public T empty()
public T end()
public T enumItem(String name,
Object... params)
public T enumm()
public T enumm(String name)
public T enummm(Object... items)
public T enummmm(String name,
Object... items)
public T eos()
public T eq(Object left,
Object right)
public ExprBase<? extends ExprBase<?>> expr()
public T extendss(Object name)
public T finall()
public T finallyy()
public T forr(Object init,
Object condition,
Object updater)
public T forrr(Object indexFrom,
Object indexTo)
public T forrr(Object itemClass,
String itemName,
Object iterable)
public T ge(Object left,
Object right)
public Object getClasss()
public String getPackage()
public String getClassSimpleName()
public T gt(Object left,
Object right)
public T iff(Object expr)
public T ifff(Object test,
Object trueExpr,
Object falseExpr)
public T implementss(Object... itfs)
public void importAll(Object packagee)
public String importStatic(Object name)
public String importt(Object name)
public T incrPre(Object expr)
public T incr(Object expr)
public T instanceoff(Object left,
Object right)
public T interfacee()
public T interfacee(String name)
public T le(Object left,
Object right)
public T leftShift(Object left,
Object right)
public T lt(Object left,
Object right)
public T mainn(String... argsName)
public T method(Object returnType,
String name,
Object... params)
public T minus(Object expr)
public T minus(Object left,
Object right)
public T ne(Object left,
Object right)
public T neww(Object classss,
Object... params)
public T next()
public T nl()
public T not(Object expr)
public T or(Object left,
Object right)
public T Override()
Override annotation.
public T packagee(String name)
public T plus(Object expr)
public T plus(Object left,
Object right)
public T privatee()
public T protectedd()
public T publicc()
public T remainder(Object left,
Object right)
public T returnn()
public T returnn(Object value)
public T rightShiftSigned(Object left,
Object right)
public T rightShiftUnsigned(Object left,
Object right)
public void setClasss(Object classs)
classs - public T staticc()
static.
public T superr(String method,
Object... params)
super.
method - params -
public T SuppressWarnings(Object firstValue,
Object... moreValues)
SuppressWarnings annotation.
firstValue - First value to suppress.moreValues - If length == 0, adds a single member annotation, else adds a
normal annotation given all values in a string array.
public T switchh(Object expr)
switch.
expr -
public T synchronizedd(Object expr)
synchronized.
expr -
public T throwss(Object... exceptions)
throws declaration at the end of a ctor/method declaration.
exceptions - Exceptions that may be throws from the method. Must not be empty
public T throww()
public T times(Object left,
Object right)
* operation
left - right -
public String toString()
toString in class Objectpublic T tryy()
public T var(Object classss,
Object nameOrExpr,
Object... init)
Example using an expression:
var(doublee, expr().assign("d", null).next().assign("devil", 666)).eos();
-> double d, devil = 666;
Example using simple names and initializers:
var(doublee, "d").eos(); -> double d; var(doublee, "d", 2.2).eos(); -> double d = 2.2; var(doublee, "d", 2.2, "pi", 3.1415).eos(); -> double d = 2.2, pi = 3.1415;
classss - Type of the variablenameOrExpr - May be an instance of ExprBase in which case the expression is added
directly and the init parameter is ignored. If not an instance of
ExprBase, designates the simple name of the variable.init - If length > 0, the first element is the variable initializer.
Further elements (index > 0) must consist of [name] = [initializer] pairs.
public T whilee(Object expr)
while.
expr -
public T question()
?) used for wildcard type arguments.
public T superrr(Object classss)
Example: Class<? super MyClass>
classss -
public T typeArgg(Object... args)
args -
public boolean getAutoImport()
public void setAutoImport(boolean autoImport)
autoImport - public Date _addDefaultFileHeader()
May be overriden by subclasses to add their own header.
public boolean getPrettyPrint()
This flag does not affect the javadoc methods doc*. Javadoc is always set on
new lines.
public void setPrettyPrint(boolean prettyPrint)
This flag does not affect the javadoc methods doc*. Javadoc is always set on
new lines.
prettyPrint -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||