This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more exampes and helpful isX methods on types + fixed bug where…
… guards was not generated for arrays
- Loading branch information
Showing
19 changed files
with
285 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ples/src/main/java/com/fortyoneconcepts/valjogen/examples/AdvancedCustomDigestMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2014 41concepts Aps | ||
*/ | ||
package com.fortyoneconcepts.valjogen.examples; | ||
|
||
import com.fortyoneconcepts.valjogen.annotations.VALJOConfigure; | ||
import com.fortyoneconcepts.valjogen.annotations.VALJOGenerate; | ||
|
||
/** | ||
* Example that shows a non-trivial custom template implementing an advanced custom method that calculate a digest of object state. | ||
* The generated class is set to inherit from a base class that provide both contract and helper methods. | ||
* The example also shows how to make the generated mutable class thread safe. | ||
*/ | ||
@VALJOGenerate(comment="Example 13") | ||
@VALJOConfigure(customJavaTemplateFileName="advanced_custom_method.stg", baseClazzName="DigestBaseClass", synchronizedAccessEnabled=true) | ||
public interface AdvancedCustomDigestMethod | ||
{ | ||
public String getName(); | ||
public void setName(String name); | ||
|
||
public int getAge(); | ||
public void setAge(int age); | ||
|
||
public String[] getAddress(); | ||
public void setAddress(String[] address); | ||
|
||
public boolean isVerified(); | ||
public void setVerified(boolean verified); | ||
|
||
// Custom method calculateDigest declared in base class. | ||
} |
18 changes: 18 additions & 0 deletions
18
...mples/src/main/java/com/fortyoneconcepts/valjogen/examples/CustomLogginngWithRegions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (C) 2014 41concepts Aps | ||
*/ | ||
package com.fortyoneconcepts.valjogen.examples; | ||
|
||
import com.fortyoneconcepts.valjogen.annotations.VALJOConfigure; | ||
import com.fortyoneconcepts.valjogen.annotations.VALJOGenerate; | ||
|
||
/** | ||
* Example that shows how to apply a simple custom template to provide constructor logging by overriding existing regions. | ||
*/ | ||
@VALJOGenerate(comment="Example 10") | ||
@VALJOConfigure(customJavaTemplateFileName="custom_logging.stg", synchronizedAccessEnabled=true) | ||
public interface CustomLogginngWithRegions | ||
{ | ||
public String getName(); | ||
public void setName(String name); | ||
} |
38 changes: 38 additions & 0 deletions
38
valjogen-examples/src/main/java/com/fortyoneconcepts/valjogen/examples/DigestBaseClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.fortyoneconcepts.valjogen.examples; | ||
|
||
/** | ||
* Example of a preexisting abstract base class that a generated class can extend from. | ||
* | ||
* In this case the bease class both specifies an abstract method that the | ||
* generated class must implement (using custom templates) as well as | ||
* helper methods useful for calculating digests. Helper methods using overloading | ||
* like here can makes custom templates much simpler to write. | ||
* | ||
* Used by the "AdvancedCustomDigestMethod.java" example and its assoicated template "advanced_custom_method.stg". | ||
* | ||
* Note that the current set of getBytes methods only handle a subset of types such as bytes, integers, | ||
* strings and arrays of those types. For a real implementation, one should overload with additional | ||
* getBytes methods. | ||
*/ | ||
public abstract class DigestBaseClass | ||
{ | ||
// Will be implemented by a custom template specified in the interface for the example. | ||
public abstract byte[] calculateDigest(String algorithm) throws java.security.NoSuchAlgorithmException; | ||
|
||
protected static final byte getBytes(boolean v) { | ||
return v ? (byte)1 : (byte)0; | ||
} | ||
|
||
protected static final byte[] getBytes(int v) { | ||
return new byte[] { | ||
(byte)((v >> 24) & 0xff), | ||
(byte)((v >> 16) & 0xff), | ||
(byte)((v >> 8) & 0xff), | ||
(byte)((v >> 0) & 0xff), | ||
}; | ||
} | ||
|
||
protected static final byte[] getBytes(String v) { | ||
return v.getBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...-examples/src/main/java/com/fortyoneconcepts/valjogen/examples/advanced_custom_method.stg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2014 41concepts Aps | ||
*/ | ||
/** | ||
* This custom template shows how to implement a non-trivial custom method declared in the interface that iterate through all members of the class. | ||
* | ||
* The custom template for the new custom method must have a specific name of "method_"+specifier and the template must be configured by | ||
* the VALJOConfigure annotation. See javadoc on VALJOConfigure#customJavaTemplateFileName for details about this. Also refer to | ||
* javadocs in the com.fortyoneconcepts.valjogen.model packakge for template arguments clazz of type Clazz and method of type Method. | ||
* | ||
* The digest implementation iterates though all class members (fields) and calls update methods on a digester before returning the digest. Note, | ||
* how the abstract base class in this example supplies helper methods to get byte arrays from member values. | ||
*/ | ||
|
||
@class.imports() ::= << | ||
<! Add our log class manually - could also have been done easier with VALJOConfigure's importClasses option !> | ||
<@super.imports()> | ||
import java.security.MessageDigest; | ||
>> | ||
|
||
method_calculateDigest_String(clazz, method) ::= << | ||
/** | ||
* Returnes the message digest of the members in the class. | ||
*/ | ||
<declare_method(clazz, method)> | ||
{ | ||
<! Construct a digest using first algorithm specified in first argument of method (we could hardcode this but in the example we use the model to tell us the name of the argument) !> | ||
MessageDigest digester = MessageDigest.getInstance(<first(method.parameters).name>); | ||
|
||
<! Iterate through all members of the class and call one of the template methods below depending on the overall type of the member !> | ||
<clazz.members:{m | <(toByteTemplateNamesByTypeCategory.(m.type.typeCategory))(m.name, m.type)> }; wrap, anchor, separator="\n"> | ||
|
||
return digester.digest(); | ||
} | ||
>> | ||
|
||
/* A static ST map that works as a switch so types can be handled by different templates depending on category */ | ||
toByteTemplateNamesByTypeCategory ::= [ "PRIMITIVE": "toByte_primitive", "ARRAY": "toByte_array", default: "toByte_object"] | ||
|
||
/* Primitive members are handled here. Note how bytes are easier dealth with then other primitives */ | ||
toByte_primitive(var, type) ::= <% | ||
<if(type.primitiveByte)> | ||
digester.update(<var>); | ||
<else> | ||
digester.update(getBytes(<var>)); | ||
<endif> | ||
%> | ||
|
||
/* Arrays are handled here. Note how primitive arrays of bytes are easier dealth with then other arrays */ | ||
toByte_array(var, type) ::= << | ||
<if(type.arrayComponentType.primitiveByte)> | ||
digester.update(<var>);<\\> | ||
<else><\\> | ||
for (int i=0; i\<<var>.length; ++i) | ||
digester.update(getBytes(<var>[i]));<\\> | ||
<endif> | ||
>> | ||
|
||
/* All object types need help from our base class in this implementation. */ | ||
toByte_object(var, type) ::= <% | ||
digester.update(getBytes(<var>)); | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.