SLING-7581 - Using data-sly-use with two classes with same name will generate uncompilable code
* made the compiler generate FQCN in the Use-API context
* removed import declarations since they're redundant now
diff --git a/pom.xml b/pom.xml
index 12c9449..8fadc0d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -167,7 +167,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.scripting.sightly.compiler</artifactId>
- <version>1.0.23-1.4.0-SNAPSHOT</version>
+ <version>1.0.22-1.4.0</version>
<scope>provided</scope>
</dependency>
diff --git a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/JavaClassBackendCompiler.java b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/JavaClassBackendCompiler.java
index 8069cb0..01a8c62 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/JavaClassBackendCompiler.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/JavaClassBackendCompiler.java
@@ -85,13 +85,6 @@
JavaClassTemplate mainTemplate = newMainTemplate();
mainTemplate.setPackageName(classInfo.getPackageName());
mainTemplate.setClassName(classInfo.getSimpleClassName());
- StringBuilder imports = new StringBuilder();
- for (String importClass : unitBuilder.getImports()) {
- if (JAVA_IMPORTS_ANALYZER.allowImport(importClass)) {
- imports.append("import ").append(importClass).append(";").append(System.lineSeparator());
- }
- }
- mainTemplate.setImports(imports.toString());
processCompilationResult(compilationOutput, mainTemplate);
return mainTemplate.toString();
}
diff --git a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/ExpressionTranslator.java b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/ExpressionTranslator.java
index f65eefe..599a50b 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/ExpressionTranslator.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/ExpressionTranslator.java
@@ -144,15 +144,14 @@
String runtimeCallName = runtimeCall.getFunctionName();
source.startMethodCall(SourceGenConstants.RENDER_CONTEXT_INSTANCE, SourceGenConstants.RUNTIME_CALL_METHOD)
.stringLiteral(runtimeCallName);
- int index = 0;
for (ExpressionNode arg : runtimeCall.getArguments()) {
source.separateArgument();
- if (RuntimeFunction.USE.equals(runtimeCallName) && index == 0) {
+ if (RuntimeFunction.USE.equals(runtimeCallName)) {
if (arg instanceof StringConstant) {
StringConstant constant = (StringConstant) arg;
String className = constant.getText();
if (imports.contains(className)) {
- source.className(className.substring(className.lastIndexOf('.') + 1));
+ source.className(className);
} else {
visit(arg);
}
diff --git a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/JavaClassTemplate.java b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/JavaClassTemplate.java
index 85a0236..66b43d5 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/JavaClassTemplate.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/java/compiler/impl/JavaClassTemplate.java
@@ -24,7 +24,6 @@
private String classTemplate;
private static final String PACKAGE_NAME = "PackageName";
- private static final String IMPORTS = "Imports";
private static final String CLASS_NAME = "ClassName";
private static final String MAIN_BODY = "MainBody";
private static final String TEMPLATE_INIT = "SubTemplateMapInit";
@@ -52,10 +51,6 @@
setPart(PACKAGE_NAME, name);
}
- public void setImports(String imports) {
- setPart(IMPORTS, imports);
- }
-
@Override
public String toString() {
return insertPart(TEMPLATE_INIT, classTemplate, templateInitBuilder.toString());
diff --git a/src/main/resources/templates/compiled_unit_template.txt b/src/main/resources/templates/compiled_unit_template.txt
index 3ce6585..a327d6a 100644
--- a/src/main/resources/templates/compiled_unit_template.txt
+++ b/src/main/resources/templates/compiled_unit_template.txt
@@ -25,8 +25,6 @@
import org.apache.sling.scripting.sightly.java.compiler.RenderUnit;
import org.apache.sling.scripting.sightly.render.RenderContext;
-##Imports##
-
public final class ##ClassName## extends RenderUnit {
@Override
diff --git a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
index 8bac5aa..3867eea 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/JavaClassBackendCompilerTest.java
@@ -95,7 +95,7 @@
}
@Test
- public void generateImportStatements() throws Exception {
+ public void testJavaUseApiDependencies() throws Exception {
CompilationUnit compilationUnit = TestUtils.readScriptFromClasspath("/imports.html");
JavaClassBackendCompiler backendCompiler = new JavaClassBackendCompiler();
SightlyCompiler sightlyCompiler = new SightlyCompiler();
diff --git a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/CharSequenceJavaCompiler.java b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/CharSequenceJavaCompiler.java
index f3b1d3a..2ddc681 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/CharSequenceJavaCompiler.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/CharSequenceJavaCompiler.java
@@ -182,7 +182,7 @@
.keySet(), diagnostics);
}
try {
- // For each class name in the inpput map, get its compiled
+ // For each class name in the input map, get its compiled
// class and put it in the output map
Map<String, Class<T>> compiled = new HashMap<>();
for (String qualifiedClassName : classes.keySet()) {
diff --git a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/a/TestPojo.java b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/a/TestPojo.java
new file mode 100644
index 0000000..830bf03
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/a/TestPojo.java
@@ -0,0 +1,22 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.sightly.compiler.java.utils.fixture.a;
+
+public class TestPojo {
+}
diff --git a/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/b/TestPojo.java b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/b/TestPojo.java
new file mode 100644
index 0000000..db79e5c
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/sightly/compiler/java/utils/fixture/b/TestPojo.java
@@ -0,0 +1,22 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.sightly.compiler.java.utils.fixture.b;
+
+public class TestPojo {
+}
diff --git a/src/test/resources/imports.html b/src/test/resources/imports.html
index 639c635..cf936ab 100644
--- a/src/test/resources/imports.html
+++ b/src/test/resources/imports.html
@@ -17,6 +17,8 @@
~ under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<div data-sly-use.record="org.apache.sling.scripting.sightly.Record"
+ data-sly-use.pojoA="org.apache.sling.scripting.sightly.compiler.java.utils.fixture.a.TestPojo"
+ data-sly-use.pojoB="org.apache.sling.scripting.sightly.compiler.java.utils.fixture.b.TestPojo"
data-sly-use.js="script.js"
data-sly-use.pojo="Pojo"
></div>
diff --git a/src/test/resources/imports.html.java b/src/test/resources/imports.html.java
index bdbcaf1..0ce9c47 100644
--- a/src/test/resources/imports.html.java
+++ b/src/test/resources/imports.html.java
@@ -25,9 +25,6 @@
import org.apache.sling.scripting.sightly.java.compiler.RenderUnit;
import org.apache.sling.scripting.sightly.render.RenderContext;
-import org.apache.sling.scripting.sightly.Record;
-
-
public final class Test_imports extends RenderUnit {
@Override
@@ -38,10 +35,14 @@
// Main Template Body -----------------------------------------------------------------------------
Object _global_record = null;
+Object _global_pojoa = null;
+Object _global_pojob = null;
Object _global_js = null;
Object _global_pojo = null;
out.write("\n");
-_global_record = renderContext.call("use", Record.class.getName(), obj());
+_global_record = renderContext.call("use", org.apache.sling.scripting.sightly.Record.class.getName(), obj());
+_global_pojoa = renderContext.call("use", org.apache.sling.scripting.sightly.compiler.java.utils.fixture.a.TestPojo.class.getName(), obj());
+_global_pojob = renderContext.call("use", org.apache.sling.scripting.sightly.compiler.java.utils.fixture.b.TestPojo.class.getName(), obj());
_global_js = renderContext.call("use", "script.js", obj());
_global_pojo = renderContext.call("use", "Pojo", obj());
out.write("<div></div>\n");