Skip to content

Commit

Permalink
this parameter support
Browse files Browse the repository at this point in the history
Fixes redhat-developer#483

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 14, 2021
1 parent 278e2ed commit 9a922f6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HelloResource {
@GET
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@QueryParam("name") String name) {
hello.data(new Item(null, name)); // this parameter
hello.data("age", 12);
hello.data("height", 1.50, "weight", 50L);
return hello.data("name", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ private static void templateField(DataModelProject<DataModelTemplate<DataModelPa
List<DataModelParameter> parameters = helloTemplate.getParameters();
Assert.assertNotNull(parameters);

// hello.data(new Item(null, name));
// hello.data("age", 12);
// hello.data("height", 1.50, "weight", 50.5);
// return hello.data("name", name);

Assert.assertEquals(4, parameters.size());
assertParameter("age", "int", true, parameters, 0);
assertParameter("height", "double", true, parameters, 1);
assertParameter("weight", "long", true, parameters, 2);
assertParameter("name", "java.lang.String", true, parameters, 3);
Assert.assertEquals(5, parameters.size());
assertParameter("this", "org.acme.qute.Item", true, parameters, 0);
assertParameter("age", "int", true, parameters, 1);
assertParameter("height", "double", true, parameters, 2);
assertParameter("weight", "long", true, parameters, 3);
assertParameter("name", "java.lang.String", true, parameters, 4);

// Template goodbye;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
*/
public class TemplateDataCollector extends TemplateDataVisitor {

private static final String DATA_METHOD = "data";

private final DataModelTemplate<DataModelParameter> template;

public TemplateDataCollector(DataModelTemplate<DataModelParameter> template, IProgressMonitor monitor) {
Expand All @@ -60,6 +58,8 @@ protected boolean visitParameter(Object name, Object type) {
String paramName = null;
if (name instanceof StringLiteral) {
paramName = ((StringLiteral) name).getLiteralValue();
} else if (name instanceof String) {
paramName = ((String) name);
}
if (paramName != null) {
String paramType = "java.lang.Object";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
*******************************************************************************/
package com.redhat.qute.jdt.internal.template;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
Expand Down Expand Up @@ -48,6 +53,8 @@
*/
public class TemplateDataLocation extends TemplateDataVisitor {

private static final Logger LOGGER = Logger.getLogger(TemplateDataLocation.class.getName());

private final String parameterName;

private final IJDTUtils utils;
Expand All @@ -65,21 +72,28 @@ protected boolean visitParameter(Object paramName, Object paramType) {
StringLiteral literal = ((StringLiteral) paramName);
String paramNameString = literal.getLiteralValue();
if (parameterName.equals(paramNameString)) {
try {
Range range = utils.toRange(getMethod().getOpenable(), literal.getStartPosition(),
literal.getLength());
String uri = utils.toUri(getMethod().getTypeRoot());
this.location = new Location(uri, range);
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.location = createParameterLocation(literal);
return false;
}
} else if (THIS_PARAMETER_NAME.equals(paramName)) {
this.location = createParameterLocation((ASTNode) paramType);
}
return true;
}

public Location createParameterLocation(ASTNode arg0) {
try {
IMethod method = getMethod();
Range range = utils.toRange(method.getOpenable(), arg0.getStartPosition(), arg0.getLength());
String uri = utils.toUri(method.getTypeRoot());
return new Location(uri, range);
} catch (JavaModelException e) {
LOGGER.log(Level.SEVERE,
"Error while getting location of method template parameter of '" + parameterName + "'.", e);
return null;
}
}

public Location getLocation() {
return location;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ public abstract class TemplateDataVisitor extends ASTVisitor {

private static final String DATA_METHOD = "data";

protected static final String THIS_PARAMETER_NAME = "this";

private IMethod method;

@Override
public boolean visit(MethodInvocation node) {
String methodName = node.getName().getIdentifier();
if (DATA_METHOD.equals(methodName)) {
// .data("book", book)
@SuppressWarnings("rawtypes")
List arguments = node.arguments();
return visitDataMethodInvocation(node);
}
return super.visit(node);
}

private boolean visitDataMethodInvocation(MethodInvocation node) {
@SuppressWarnings("rawtypes")
List arguments = node.arguments();
if (arguments.size() == 1) {
// One parameter
Object paramType = arguments.get(0);
boolean result = visitParameter(THIS_PARAMETER_NAME, paramType);
if (!result) {
return false;
}
} else {
// Several parameters
Object paramName = null;
for (int i = 0; i < arguments.size(); i++) {
if (i % 2 == 0) {
Expand All @@ -35,7 +52,7 @@ public boolean visit(MethodInvocation node) {
}
}
}
return super.visit(node);
return true;
}

public void setMethod(IMethod method) {
Expand All @@ -45,7 +62,7 @@ public void setMethod(IMethod method) {
public IMethod getMethod() {
return method;
}

protected abstract boolean visitParameter(Object paramName, Object paramType);

}

0 comments on commit 9a922f6

Please sign in to comment.