Skip to content

Commit

Permalink
fix pmd
Browse files Browse the repository at this point in the history
  • Loading branch information
acraciun committed Dec 15, 2015
1 parent fb0aec0 commit b775b13
Showing 1 changed file with 18 additions and 6 deletions.
Expand Up @@ -55,7 +55,11 @@
*/
public class PathGetterMemberSelectTemplate<JS> implements WriterContributor<MemberSelectTree, JS> {

private JS getTarget(WriterVisitor<JS> visitor, ExpressionTree tree, GenerationContext<JS> context) {
private JS getTarget(WriterVisitor<JS> visitor, TreeWrapper<ExpressionTree, JS> currentWrapper, ExpressionTree tree,
GenerationContext<JS> context, boolean targetIsPartOfPath) {
if (!targetIsPartOfPath) {
return visitor.scan(currentWrapper.getTree(), context);
}
if (tree instanceof MemberSelectTree) {
MemberSelectTree ms = (MemberSelectTree) tree;
if (JavaNodes.isSuper(ms.getExpression())) {
Expand Down Expand Up @@ -87,6 +91,15 @@ private String ident(Tree tree) {
throw new STJSRuntimeException("Unexpected node type while building path:" + tree);
}

private boolean checkTemplateParams(String[] params, GenerationContext<JS> context, TreeWrapper<ExpressionTree, JS> currentWrapper) {
if (params == null || params.length == 0) {
context.addError(currentWrapper.getTree(),
"The 'path' template needs a parameter designating the method to be called, like this @Template(\"path(methodName)\")");
return false;
}
return true;
}

private JS getPath(WriterVisitor<JS> visitor, TreeWrapper<ExpressionTree, JS> tree, GenerationContext<JS> context) {
String path = "";
TreeWrapper<ExpressionTree, JS> currentWrapper = tree;
Expand All @@ -99,6 +112,7 @@ private JS getPath(WriterVisitor<JS> visitor, TreeWrapper<ExpressionTree, JS> tr
while (true) {
Tree currentTree = currentWrapper.getTree();
String template = currentWrapper.getFieldTemplate();

if (!"path".equals(template)) {
targetIsPartOfPath = false;
break;
Expand All @@ -113,14 +127,12 @@ private JS getPath(WriterVisitor<JS> visitor, TreeWrapper<ExpressionTree, JS> tr
currentWrapper = currentWrapper.child(((MemberSelectTree) currentTree).getExpression());
}

JS target = targetIsPartOfPath ? getTarget(visitor, currentWrapper.getTree(), context) : visitor.scan(currentWrapper.getTree(), context);

if (params == null || params.length == 0) {
context.addError(currentWrapper.getTree(),
"The 'path' template needs a parameter designating the method to be called, like this @Template(\"path(methodName)\")");
if (!checkTemplateParams(params, context, currentWrapper)) {
return null;
}

JS target = getTarget(visitor, currentWrapper, currentWrapper.getTree(), context, targetIsPartOfPath);

String methodName = params[0];

JS pathString = context.js().string(path);
Expand Down

0 comments on commit b775b13

Please sign in to comment.