Do not use a static String reference in length enforcer 96/96296/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 21 May 2021 11:13:11 +0000 (13:13 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 24 May 2021 20:40:55 +0000 (22:40 +0200)
LengthGenerator is making the mistake of having a static reference
to java.lang.String without FQCN. This breaks down if String is
overridden by definition context.

Fix this by using importedName(Type) to emit a properly-scoped
reference.

JIRA: MDSAL-664
Change-Id: I16b12865448bde807f0afd0d35e3145f8b6359c3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit b07c63ce93a839e806c8ce96b8ef659e658574e3)

binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/LengthGenerator.java
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/CompilationTest.java
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal664/foo.yang [new file with mode: 0644]

index 379f5f808acfbd6fb8f53f8810f7c19bc4e2c238..52169aab201745f23f99aa5edffeb9ed1ce7ad0c 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.model.util.Types;
 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -94,7 +95,8 @@ final class LengthGenerator {
         final StringBuilder sb = new StringBuilder();
         final Collection<String> expressions = createExpressions(constraint);
 
-        sb.append("private static void ").append(lengthCheckerName(member)).append("(final String value) {\n");
+        sb.append("private static void ").append(lengthCheckerName(member))
+            .append("(final ").append(template.importedName(Types.STRING)).append(" value) {\n");
 
         if (!expressions.isEmpty()) {
             sb.append("    final int length = value.codePointCount(0, value.length());\n");
index 2e49d7824e0b7b75e6d552cef8cf7d8729eef154..a286b41d36952a134d5e7eecf51046754c6c9987 100644 (file)
@@ -721,6 +721,15 @@ public class CompilationTest extends BaseCompilationTest {
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void testMdsal664() throws Exception {
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal664");
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal664");
+        generateTestSources("/compilation/mdsal664", sourcesOutputDir);
+        CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
+        CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
             final String returnTypeStr) throws NoSuchMethodException {
         Method method = clazz.getMethod(methodName);
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal664/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal664/foo.yang
new file mode 100644 (file)
index 0000000..435f55d
--- /dev/null
@@ -0,0 +1,14 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  list string {
+    key name;
+
+    leaf name {
+      type string {
+        length 1..32;
+      }
+    }
+  }
+}