Do not use a static String reference in length enforcer 42/96342/1
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 19:59:00 +0000 (21:59 +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>
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 e0e127447739359c7a63928a8528f89f78b9299e..9b7bdaeee36fec1158f5d7df0b0e6a8450c55c0b 100644 (file)
@@ -725,6 +725,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;
+      }
+    }
+  }
+}