forked from redhat-developer/quarkus-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Qute: Cannot locate hyphenated template name
Fixes redhat-developer#975 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
cf93107
commit 353f9be
Showing
13 changed files
with
551 additions
and
29 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
....qute.jdt.test/projects/maven/qute-record/src/main/java/org/acme/sample/ItemResource.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.acme.sample; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import io.quarkus.qute.CheckedTemplate; | ||
import io.quarkus.qute.TemplateExtension; | ||
import io.quarkus.qute.TemplateInstance; | ||
|
||
@Path("items") | ||
public class ItemResource { | ||
|
||
@CheckedTemplate(defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) | ||
static class Templates { | ||
static native TemplateInstance HelloWorld(String name); | ||
|
||
} | ||
|
||
@CheckedTemplate(defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) | ||
static class Templates2 { | ||
static native TemplateInstance HelloWorld(String name); | ||
|
||
} | ||
|
||
@CheckedTemplate(defaultName=CheckedTemplate.UNDERSCORED_ELEMENT_NAME)3 | ||
static class Templates3 { | ||
static native TemplateInstance HelloWorld(String name); | ||
} | ||
|
||
|
||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_BASE_PATH; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_HYPHENATED_ELEMENT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_UNDERSCORED_ELEMENT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.OLD_CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.TEMPLATE_CLASS; | ||
|
@@ -51,6 +54,7 @@ | |
import com.redhat.qute.jdt.utils.IJDTUtils; | ||
import com.redhat.qute.jdt.utils.JDTQuteProjectUtils; | ||
import com.redhat.qute.jdt.utils.JDTTypeUtils; | ||
import com.redhat.qute.jdt.utils.TemplateNameStrategy; | ||
import com.redhat.qute.jdt.utils.TemplatePathInfo; | ||
|
||
/** | ||
|
@@ -138,7 +142,8 @@ public boolean visit(FieldDeclaration node) { | |
.getLocationExpressionFromConstructorParameter(variable.getName().getIdentifier()); | ||
} | ||
String fieldName = variable.getName().getIdentifier(); | ||
collectTemplateLink(null, node, locationExpression, getTypeDeclaration(node), null, fieldName, false); | ||
collectTemplateLink(null, node, locationExpression, getTypeDeclaration(node), null, fieldName, false, | ||
TemplateNameStrategy.ELEMENT_NAME); | ||
} | ||
} | ||
return super.visit(node); | ||
|
@@ -186,10 +191,12 @@ public boolean visit(TypeDeclaration node) { | |
// public static native TemplateInstance book(Book book); | ||
boolean ignoreFragments = isIgnoreFragments(annotation); | ||
String basePath = getBasePath(annotation); | ||
TemplateNameStrategy templateNameStrategy = getDefaultName(annotation); | ||
List body = node.bodyDeclarations(); | ||
for (Object declaration : body) { | ||
if (declaration instanceof MethodDeclaration) { | ||
collectTemplateLink(basePath, (MethodDeclaration) declaration, node, ignoreFragments); | ||
collectTemplateLink(basePath, (MethodDeclaration) declaration, node, ignoreFragments, | ||
templateNameStrategy); | ||
} | ||
} | ||
} | ||
|
@@ -209,7 +216,7 @@ public boolean visit(TypeDeclaration node) { | |
public boolean visit(RecordDeclaration node) { | ||
if (isImplementTemplateInstance(node)) { | ||
String recordName = node.getName().getIdentifier(); | ||
collectTemplateLink(null, node, null, node, null, recordName, false); | ||
collectTemplateLink(null, node, null, node, null, recordName, false, TemplateNameStrategy.ELEMENT_NAME); | ||
} | ||
return super.visit(node); | ||
} | ||
|
@@ -275,18 +282,52 @@ private static boolean isIgnoreFragments(Annotation checkedTemplateAnnotation) { | |
* @return the <code>basePath</code> value declared in the @CheckedTemplate | ||
* annotation | ||
*/ | ||
public static String getBasePath(Annotation checkedTemplateAnnotation) { | ||
private static String getBasePath(Annotation checkedTemplateAnnotation) { | ||
String basePath = null; | ||
try { | ||
Expression ignoreFragmentExpr = AnnotationUtils.getAnnotationMemberValueExpression( | ||
checkedTemplateAnnotation, CHECKED_TEMPLATE_ANNOTATION_BASE_PATH); | ||
basePath = AnnotationUtils.getString(ignoreFragmentExpr); | ||
Expression basePathExpr = AnnotationUtils.getAnnotationMemberValueExpression(checkedTemplateAnnotation, | ||
CHECKED_TEMPLATE_ANNOTATION_BASE_PATH); | ||
basePath = AnnotationUtils.getString(basePathExpr); | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
return basePath; | ||
} | ||
|
||
/** | ||
* Returns the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation, relative to the templates root, to search the templates from. | ||
* <code> | ||
* @CheckedTemplate([email protected]_ELEMENT_NAME) | ||
*</code> | ||
* | ||
* @param checkedTemplateAnnotation the CheckedTemplate annotation. | ||
* @return the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation | ||
*/ | ||
private static TemplateNameStrategy getDefaultName(Annotation checkedTemplateAnnotation) { | ||
TemplateNameStrategy defaultName = TemplateNameStrategy.ELEMENT_NAME; | ||
try { | ||
Expression defaultNameExpr = AnnotationUtils.getAnnotationMemberValueExpression(checkedTemplateAnnotation, | ||
CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME); | ||
defaultName = getDefaultName(AnnotationUtils.getString(defaultNameExpr)); | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
return defaultName; | ||
} | ||
|
||
private static TemplateNameStrategy getDefaultName(String defaultName) { | ||
switch (defaultName) { | ||
case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_HYPHENATED_ELEMENT_NAME: | ||
return TemplateNameStrategy.HYPHENATED_ELEMENT_NAME; | ||
|
||
case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_UNDERSCORED_ELEMENT_NAME: | ||
return TemplateNameStrategy.UNDERSCORED_ELEMENT_NAME; | ||
} | ||
return TemplateNameStrategy.ELEMENT_NAME; | ||
} | ||
|
||
@Override | ||
public void endVisit(TypeDeclaration node) { | ||
levelTypeDecl--; | ||
|
@@ -302,24 +343,28 @@ private static TypeDeclaration getTypeDeclaration(ASTNode node) { | |
} | ||
|
||
private void collectTemplateLink(String basePath, MethodDeclaration methodDeclaration, TypeDeclaration type, | ||
boolean ignoreFragment) { | ||
boolean ignoreFragment, TemplateNameStrategy templateNameStrategy) { | ||
String className = null; | ||
boolean innerClass = levelTypeDecl > 1; | ||
if (innerClass) { | ||
className = JDTTypeUtils.getSimpleClassName(typeRoot.getElementName()); | ||
} | ||
String methodName = methodDeclaration.getName().getIdentifier(); | ||
collectTemplateLink(basePath, methodDeclaration, null, type, className, methodName, ignoreFragment); | ||
collectTemplateLink(basePath, methodDeclaration, null, type, className, methodName, ignoreFragment, | ||
templateNameStrategy); | ||
} | ||
|
||
private void collectTemplateLink(String basePath, ASTNode fieldOrMethod, StringLiteral locationAnnotation, | ||
AbstractTypeDeclaration type, String className, String fieldOrMethodName, boolean ignoreFragment) { | ||
AbstractTypeDeclaration type, String className, String fieldOrMethodName, boolean ignoreFragment, | ||
TemplateNameStrategy templateNameStrategy) { | ||
try { | ||
String location = locationAnnotation != null ? locationAnnotation.getLiteralValue() : null; | ||
IProject project = typeRoot.getJavaProject().getProject(); | ||
TemplatePathInfo templatePathInfo = location != null | ||
? JDTQuteProjectUtils.getTemplatePath(basePath, null, location, ignoreFragment) | ||
: JDTQuteProjectUtils.getTemplatePath(basePath, className, fieldOrMethodName, ignoreFragment); | ||
? JDTQuteProjectUtils.getTemplatePath(basePath, null, location, ignoreFragment, | ||
templateNameStrategy) | ||
: JDTQuteProjectUtils.getTemplatePath(basePath, className, fieldOrMethodName, ignoreFragment, | ||
templateNameStrategy); | ||
IFile templateFile = null; | ||
if (location == null) { | ||
templateFile = getTemplateFile(project, templatePathInfo.getTemplateUri()); | ||
|
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
Oops, something went wrong.