Last active
October 15, 2019 14:50
-
-
Save edefazio/bf47f86143d78df36382c68d9fdff6f5 to your computer and use it in GitHub Desktop.
jdraft metaprogramming example following the JavaParser Example presented by Luca Molteni
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
package test.othertools; | |
import junit.framework.TestCase; | |
import org.jdraft._method; | |
import org.jdraft.pattern.$typeRef; | |
/** | |
* Luca Molteni presented an example at the Voxxed Days Ticino 2019 | |
* https://youtu.be/1pSj6OQsq9k?list=PLRsbF2sD7JVorYibvQrrG34UQ6YE3PaVP&t=2214 | |
* replaced using jdraft API | |
*/ | |
public class JavaParserLucaAccumulateTest extends TestCase { | |
public void testAcc() { | |
System.out.println ( generateAccumulate(int.class) ); | |
System.out.println ( generateAccumulate(float.class) ); | |
System.out.println ( generateAccumulate(double.class) ); | |
System.out.println ( generateAccumulate(String.class) ); | |
} | |
public _method generateAccumulate( Class primitiveType ){ | |
_method _m = _method.of(new Object(){ | |
public int accumulate( int[] nums){ | |
int acc = 0; | |
for( int n : nums ) { | |
acc = acc + n; | |
} | |
return acc; | |
} | |
}); | |
$typeRef.as(int[].class).replaceIn(_m, primitiveType.getCanonicalName()+"[]"); | |
$typeRef.as(int.class).replaceIn(_m, primitiveType); | |
return _m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment