Skip to content

Commit

Permalink
fixed lookup of HashMap key before method
Browse files Browse the repository at this point in the history
  • Loading branch information
synapticloop committed Mar 12, 2017
1 parent 71c5575 commit 019a96e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,19 @@ repositories {

```
dependencies {
runtime(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar')
runtime(group: 'synapticloop', name: 'templar', version: '1.4.3', ext: 'jar')
compile(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar')
compile(group: 'synapticloop', name: 'templar', version: '1.4.3', ext: 'jar')
}
```

or, more simply for versions of gradle greater than 2.1

```
dependencies {
runtime 'synapticloop:templar:1.4.2'
runtime 'synapticloop:templar:1.4.3'
compile 'synapticloop:templar:1.4.2'
compile 'synapticloop:templar:1.4.3'
}
```

Expand All @@ -238,7 +238,7 @@ dependencies {
<dependency>
<groupId>synapticloop</groupId>
<artifactId>templar</artifactId>
<version>1.4.2</version>
<version>1.4.3</version>
<type>jar</type>
</dependency>
```
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ plugins {
group = 'synapticloop'
archivesBaseName = 'templar'
description = """Templar templating engine"""
version = '1.4.2'
version = '1.4.4'

sourceCompatibility = 1.7
targetCompatibility = 1.7

// tasks.withType(Javadoc).all { enabled = false }

cobertura {
coverageFormats = [ 'html', 'xml']
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/synapticloop/templar/helper/ObjectHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Object evaluateObject(String commandLine, TemplarContext templarCo
Method invokeMethod = findMethod(object, nextToken);
if(null != invokeMethod) {
foundMethod = true;
int parameterCount = invokeMethod.getParameterCount();
int parameterCount = invokeMethod.getParameterTypes().length;
switch (parameterCount) {
case 0:
object = invokeObjectMethod(object, invokeMethod);
Expand Down Expand Up @@ -282,6 +282,14 @@ public static Object parseAndExecuteCommandLine(TemplarContext templarContext, S
private static Method findMethod(Object object, String reference) throws RenderException {
Method returnMethod = null;
if(object instanceof Map<?, ?>) {
if(((Map<?,?>)object).containsKey(reference)) {
try {
return(object.getClass().getMethod("get", Object.class));
} catch (NoSuchMethodException nsmex) {
} catch (SecurityException sex) {
}
}

try {
returnMethod = object.getClass().getMethod(reference);
if(null != returnMethod) {
Expand All @@ -293,6 +301,7 @@ private static Method findMethod(Object object, String reference) throws RenderE
// fall through
}


try {
// it could be that we are looking up a direct call on the object

Expand Down Expand Up @@ -339,7 +348,7 @@ private static Method getMethod(Object object, String methodReference) throws Re
} catch (NullPointerException npex) {
throw new RenderException("Got Null Pointer for method '" + methodReference + "' on object '" + object + "'.", npex);
}


return(null);
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/synapticloop/templar/token/LoopToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

import synapticloop.templar.exception.ParseException;
Expand Down Expand Up @@ -126,6 +127,21 @@ public String render(TemplarContext templarContext) throws RenderException {
first = false;
offset++;
}
} else if(object instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>)object;
Iterator<?> iterator = map.keySet().iterator();
boolean first = true;
int offset = 0;
while (iterator.hasNext()) {
Object nextObject = (Object) iterator.next();
templarContext.add(contextAs, nextObject);
templarContext.add(contextAs + STATUS, new LoopStatusBean(first, !iterator.hasNext(), offset + 1, offset));
for (Token token : this.childTokens) {
stringBuilder.append(token.render(templarContext));
}
first = false;
offset++;
}
} else if (object instanceof Iterable<?>) {
Iterable<?> iterable = (Iterable<?>)object;
boolean first = true;
Expand Down

0 comments on commit 019a96e

Please sign in to comment.