import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
+import com.google.common.base.Preconditions
/**
* Template for generating JAVA class.
* List of generated types which are enclosed inside <code>genType</code>
*/
protected val List<GeneratedType> enclosedGeneratedTypes;
-
+
protected val GeneratedTransferObject genTO;
/**
* Creates instance of this class with concrete <code>genType</code>.
- *
+ *
* @param genType generated transfer object which will be transformed to JAVA class source code
*/
new(GeneratedTransferObject genType) {
/**
* Generates JAVA class source code (class body only).
- *
+ *
* @return string with JAVA class body source code
*/
def CharSequence generateAsInnerClass() {
/**
* Template method which generates class body.
- *
+ *
* @param isInnerClass boolean value which specify if generated class is|isn't inner
* @return string with class source code in JAVA format
*/
«enumDeclarations»
«constantsDeclarations»
«generateFields»
-
- «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty ||
+
+ «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty ||
!restrictions.lengthConstraints.nullOrEmpty)»
«generateConstraints»
-
+
«ENDIF»
«constructors»
/**
* Template method which generates inner classes inside this interface.
- *
+ *
* @return string with the source code for inner classes in JAVA format
*/
def protected innerClassesDeclarations() '''
«IF false == parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
- «FOR p : allProperties»
+ «FOR p : allProperties»
«generateRestrictions(type, p.fieldName.toString, p.returnType)»
«ENDFOR»
- «FOR p : properties»
+
+ «/*
+ * If we have patterns, we need to apply them to the value field. This is a sad
+ * consequence of how this code is structured.
+ */
+ IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+
+ «Preconditions.importedName».checkNotNull(_value, "Supplied value may not be null");
+
+ «FOR c : consts»
+ «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME && c.value instanceof List<?>»
+ boolean valid = false;
+ for (Pattern p : patterns) {
+ if (p.matcher(_value).matches()) {
+ valid = true;
+ break;
+ }
+ }
+
+ «Preconditions.importedName».checkArgument(valid, "Supplied value \"%s\" does not match any of the permitted patterns %s", _value, «TypeConstants.PATTERN_CONSTANT_NAME»);
+ «ENDIF»
+ «ENDFOR»
+ «ENDIF»
+
+ «FOR p : properties»
this.«p.fieldName» = «p.fieldName»;
«ENDFOR»
}
-
+
'''
def protected genUnionConstructor() '''
«IF false == parentProperties.empty»
super(«parentProperties.asArguments»);
«ENDIF»
- «generateRestrictions(type, property.fieldName.toString, property.returnType)»
- this.«property.fieldName» = «property.name»;
- «FOR p : other»
+
+ «generateRestrictions(type, property.fieldName.toString, property.returnType)»
+
+ this.«property.fieldName» = «property.name»;
+ «FOR p : other»
this.«p.fieldName» = null;
- «ENDFOR»
+ «ENDFOR»
}
'''
«IF false == parentProperties.empty»
super(source);
«ENDIF»
- «FOR p : properties»
+ «FOR p : properties»
this.«p.fieldName» = source.«p.fieldName»;
«ENDFOR»
}
«IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
public static «genTO.name» getDefaultInstance(String defaultValue) {
«IF "byte[]".equals(prop.returnType.name)»
- «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64();
+ «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64();
return new «genTO.name»(baseEncoding.decode(defaultValue));
«ELSEIF "java.lang.String".equals(prop.returnType.fullyQualifiedName)»
return new «genTO.name»(defaultValue);
/**
* Template method which generates JAVA class declaration.
- *
+ *
* @param isInnerClass boolean value which specify if generated class is|isn't inner
* @return string with class declaration in JAVA format
*/
/**
* Template method which generates JAVA enum type.
- *
+ *
* @return string with inner enum source code in JAVA format
*/
def protected enumDeclarations() '''
def protected suidDeclaration() '''
«IF genTO.SUID != null»
- private static final long serialVersionUID = «genTO.SUID.value»L;
+ private static final long serialVersionUID = «genTO.SUID.value»L;
«ENDIF»
'''
/**
- * Template method wich generates JAVA constants.
- *
- * @return string with constants in JAVA format
+ * Template method which generates JAVA constants.
+ *
+ * @return string with constants in JAVA format
*/
def protected constantsDeclarations() '''
«IF !consts.empty»
«val cValue = c.value»
«IF cValue instanceof List<?>»
«val cValues = cValue as List<?>»
- private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST» = new «ArrayList.importedName»<«Pattern.importedName»>();
- public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «Arrays.importedName».asList(«
+ private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST»;
+ public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
FOR v : cValues SEPARATOR ", "»«
IF v instanceof String»"«
v as String»"«
*/
def protected generateStaticInicializationBlock() '''
static {
+ final «List.importedName»<«Pattern.importedName»> l = new «ArrayList.importedName»<«Pattern.importedName»>();
for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
- «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
+ l.add(Pattern.compile(regEx));
}
+
+ «Constants.MEMBER_PATTERN_LIST» = «ImmutableList.importedName».copyOf(l);
}
'''
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.util.ArrayList;
* instead of marking it with @After annotation to prevent removing
* generated code if test fails.
*/
- static void cleanUp(File... resourceDirs) {
+ static void cleanUp(final File... resourceDirs) {
for (File resourceDir : resourceDirs) {
if (resourceDir.exists()) {
deleteTestDir(resourceDir);
* field type
* @return field with given name if present in class
*/
- static Field assertContainsField(Class<?> clazz, String name, Class<?> type) {
+ static Field assertContainsField(final Class<?> clazz, final String name, final Class<?> type) {
try {
Field f = clazz.getDeclaredField(name);
assertEquals(type, f.getType());
* @param constructorArgs
* constructor arguments of class to test
*/
- static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
- Class<?>... constructorArgs) {
+ static void assertContainsFieldWithValue(final Class<?> clazz, final String name, final Class<?> returnType, final Object expectedValue,
+ final Class<?>... constructorArgs) {
Object[] initargs = null;
if (constructorArgs != null && constructorArgs.length > 0) {
initargs = new Object[constructorArgs.length];
* @param initargs
* array of constructor values
*/
- static void assertContainsFieldWithValue(Class<?> clazz, String name, Class<?> returnType, Object expectedValue,
- Class<?>[] constructorArgs, Object... initargs) {
+ static void assertContainsFieldWithValue(final Class<?> clazz, final String name, final Class<?> returnType, final Object expectedValue,
+ final Class<?>[] constructorArgs, final Object... initargs) {
Field f = assertContainsField(clazz, name, returnType);
f.setAccessible(true);
+
+ final Object obj;
+ if ((f.getModifiers() & Modifier.STATIC) == 0) {
+ try {
+ Constructor<?> c = clazz.getDeclaredConstructor(constructorArgs);
+ obj = c.newInstance(initargs);
+ } catch (Exception e) {
+ throw new AssertionError("Failed to instantiate object for " + clazz, e);
+ }
+ } else {
+ obj = null;
+ }
+
try {
- Constructor<?> c = clazz.getDeclaredConstructor(constructorArgs);
- Object o = c.newInstance(initargs);
- assertEquals(expectedValue, f.get(o));
- } catch (Exception e) {
- throw new AssertionError("Failed to perform " + name + " field test", e);
+ assertEquals(expectedValue, f.get(obj));
+ } catch (IllegalArgumentException | IllegalAccessException e) {
+ throw new AssertionError("Failed to get field " + name + " of class " + clazz, e);
}
}
* @param args
* array of argument classes
*/
- static Constructor<?> assertContainsConstructor(Class<?> clazz, Class<?>... args) {
+ static Constructor<?> assertContainsConstructor(final Class<?> clazz, final Class<?>... args) {
try {
return clazz.getDeclaredConstructor(args);
} catch (NoSuchMethodException e) {
* array of parameter type classes
* @return method with given name, return type and parameter types
*/
- static Method assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
+ static Method assertContainsMethod(final Class<?> clazz, final Class<?> returnType, final String name, final Class<?>... args) {
try {
Method m = clazz.getDeclaredMethod(name, args);
assertEquals(returnType, m.getReturnType());
* @param loader
* current class loader
*/
- static void assertContainsMethod(Class<?> clazz, String returnTypeStr, String name, ClassLoader loader) {
+ static void assertContainsMethod(final Class<?> clazz, final String returnTypeStr, final String name, final ClassLoader loader) {
Class<?> returnType;
try {
returnType = Class.forName(returnTypeStr, true, loader);
* @param clazz
* class to test
*/
- static void assertContainsDefaultMethods(Class<?> clazz) {
+ static void assertContainsDefaultMethods(final Class<?> clazz) {
assertContainsMethod(clazz, Integer.TYPE, "hashCode");
assertContainsMethod(clazz, Boolean.TYPE, "equals", Object.class);
assertContainsMethod(clazz, String.class, "toString");
* constructor arguments
* @throws Exception
*/
- static void assertContainsRestrictionCheck(Constructor<?> constructor, String errorMsg, Object... args)
+ static void assertContainsRestrictionCheck(final Constructor<?> constructor, final String errorMsg, final Object... args)
throws Exception {
try {
constructor.newInstance(args);
* constructor arguments
* @throws Exception
*/
- static void assertContainsRestrictionCheck(Object obj, Method method, String errorMsg, Object... args)
+ static void assertContainsRestrictionCheck(final Object obj, final Method method, final String errorMsg, final Object... args)
throws Exception {
try {
method.invoke(obj, args);
* @param clazz
* class to test
*/
- static void assertContainsGetLengthOrRange(Class<?> clazz, boolean isLength) {
+ static void assertContainsGetLengthOrRange(final Class<?> clazz, final boolean isLength) {
try {
Method m = clazz.getDeclaredMethod(isLength ? "length" : "range");
java.lang.reflect.Type returnType = m.getGenericReturnType();
* @param ifc
* expected interface
*/
- static void assertImplementsIfc(Class<?> clazz, Class<?> ifc) {
+ static void assertImplementsIfc(final Class<?> clazz, final Class<?> ifc) {
Class<?>[] interfaces = clazz.getInterfaces();
List<Class<?>> ifcsList = Arrays.asList(interfaces);
if (!ifcsList.contains(ifc)) {
* @param genericTypeName
* fully qualified name of expected parameter type
*/
- static void testAugmentation(Class<?> clazz, String genericTypeName) {
+ static void testAugmentation(final Class<?> clazz, final String genericTypeName) {
assertImplementsParameterizedIfc(clazz, AUGMENTATION, genericTypeName);
}
* @param genericTypeName
* name of generic type
*/
- static void assertImplementsParameterizedIfc(Class<?> clazz, String ifcName, String genericTypeName) {
+ static void assertImplementsParameterizedIfc(final Class<?> clazz, final String ifcName, final String genericTypeName) {
ParameterizedType ifcType = null;
for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) {
if (ifc instanceof ParameterizedType) {
* @param compiledOutputDir
* compiler output directory
*/
- static void testCompilation(File sourcesOutputDir, File compiledOutputDir) {
+ static void testCompilation(final File sourcesOutputDir, final File compiledOutputDir) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<File> filesList = getJavaFiles(sourcesOutputDir);
* @param count
* expected count of files in directory
*/
- static void assertFilesCount(File dir, int count) {
+ static void assertFilesCount(final File dir, final int count) {
File[] dirContent = dir.listFiles();
if (dirContent == null) {
throw new AssertionError("File " + dir + " doesn't exists or it's not a directory");
* directory to search
* @return List of java files found
*/
- private static List<File> getJavaFiles(File directory) {
+ private static List<File> getJavaFiles(final File directory) {
List<File> result = new ArrayList<>();
File[] filesToRead = directory.listFiles();
if (filesToRead != null) {
return result;
}
- static List<File> getSourceFiles(String path) throws Exception {
+ static List<File> getSourceFiles(final String path) throws Exception {
final URI resPath = BaseCompilationTest.class.getResource(path).toURI();
final File sourcesDir = new File(resPath);
if (sourcesDir.exists()) {
}
}
- static void deleteTestDir(File file) {
+ static void deleteTestDir(final File file) {
if (file.isDirectory()) {
File[] filesToDelete = file.listFiles();
if (filesToDelete != null) {
import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
-
import com.google.common.collect.Range;
import java.io.File;
import java.lang.reflect.Constructor;
arg = "abcd";
expectedMsg = String.format("Invalid length: %s, expected: %s.", arg, lengthConstraints);
assertContainsRestrictionCheck(expectedConstructor, expectedMsg, arg);
- obj = expectedConstructor.newInstance("hello world");
- assertEquals(obj, defInst.invoke(null, "hello world"));
+
+ obj = expectedConstructor.newInstance("abcde");
+ assertEquals(obj, defInst.invoke(null, "abcde"));
// typedef string-ext2
assertFalse(stringExt2Class.isInterface());
arg = "abcde";
expectedMsg = String.format("Invalid length: %s, expected: %s.", arg, lengthConstraints);
assertContainsRestrictionCheck(expectedConstructor, expectedMsg, arg);
- obj = expectedConstructor.newInstance("helloWorld");
- assertEquals(obj, defInst.invoke(null, "helloWorld"));
+ obj = expectedConstructor.newInstance("abcdef");
+ assertEquals(obj, defInst.invoke(null, "abcdef"));
// typedef string-ext3
assertFalse(stringExt3Class.isInterface());
defInst = assertContainsMethod(stringExt3Class, stringExt3Class, "getDefaultInstance", String.class);
// assertEquals(1, stringExt3Class.getDeclaredMethods().length);
- obj = expectedConstructor.newInstance("helloWorld");
- assertEquals(obj, defInst.invoke(null, "helloWorld"));
+ obj = expectedConstructor.newInstance("bbbbbb");
+ assertEquals(obj, defInst.invoke(null, "bbbbbb"));
// typedef my-decimal-type
assertFalse(myDecimalTypeClass.isInterface());