You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2017/01/11 11:08:21 UTC
svn commit: r1778273 - in /felix/trunk/gogo/runtime/src:
main/java/org/apache/felix/gogo/runtime/
test/java/org/apache/felix/gogo/runtime/
Author: gnodet
Date: Wed Jan 11 11:08:21 2017
New Revision: 1778273
URL: http://svn.apache.org/viewvc?rev=1778273&view=rev
Log:
[FELIX-5486] [gogo][runtime] Avoid unnecessary conversions from strings to booleans/integers
Modified:
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProxy.java
felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java
Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java?rev=1778273&r1=1778272&r2=1778273&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Closure.java Wed Jan 11 11:08:21 2017
@@ -293,7 +293,7 @@ public class Closure implements Function
return last == null ? null : last.result;
}
- private Object eval(Object v)
+ static Object eval(Object v)
{
String s = v.toString();
if ("null".equals(s))
@@ -326,6 +326,11 @@ public class Closure implements Function
public Object eval(final Token t) throws Exception
{
+ return eval(t, true);
+ }
+
+ public Object eval(final Token t, boolean convertNumeric) throws Exception
+ {
if (t instanceof Parser.Closure)
{
return new Closure(session, this, ((Parser.Closure) t).program());
@@ -354,7 +359,10 @@ public class Closure implements Function
Object v = Expander.expand(t, this);
if (t == v)
{
- v = eval(v);
+ if (convertNumeric)
+ {
+ v = eval(v);
+ }
}
return v;
}
@@ -412,7 +420,7 @@ public class Closure implements Function
for (Token t : tokens)
{
- Object v = eval(t);
+ Object v = eval(t, values.isEmpty());
// if ((Token.Type.EXECUTION == t.type) && (tokens.size() == 1)) {
// return v;
Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProxy.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProxy.java?rev=1778273&r1=1778272&r2=1778273&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProxy.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/CommandProxy.java Wed Jan 11 11:08:21 2017
@@ -75,6 +75,15 @@ public class CommandProxy implements Fun
{
if (tgt instanceof Function)
{
+ for (int i = 0; i < arguments.size(); i++)
+ {
+ Object obj = arguments.get(i);
+ if (obj instanceof Token)
+ {
+ arguments.set(i, obj.toString());
+ }
+ }
+
return ((Function) tgt).execute(session, arguments);
}
else
Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java?rev=1778273&r1=1778272&r2=1778273&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java Wed Jan 11 11:08:21 2017
@@ -244,21 +244,77 @@ public final class Reflective
private static int coerce(CommandSession session, Object target, Method m,
Class<?> types[], Object out[], List<Object> in)
{
- in = transformParameters(m, in);
- if (in == null)
+ List<Object> cnvIn = new ArrayList<>();
+ List<Object> cnvIn2 = new ArrayList<>();
+ int different = 0;
+ for (Object obj : in)
+ {
+ if (obj instanceof Token)
+ {
+ Object s1 = Closure.eval(obj);
+ Object s2 = obj.toString();
+ cnvIn.add(s1);
+ cnvIn2.add(s2);
+ different += s2.equals(s1) ? 0 : 1;
+ } else
+ {
+ cnvIn.add(obj);
+ cnvIn2.add(obj);
+ }
+ }
+
+ cnvIn = transformParameters(m, cnvIn);
+ if (different != 0)
+ {
+ cnvIn2 = transformParameters(m, cnvIn2);
+ }
+ if (cnvIn == null || cnvIn2 == null)
{
// missing parameter argument?
return -1;
}
int res;
- res = docoerce(session, target, m, types, out, in);
+
+ res = docoerce(session, target, m, types, out, cnvIn);
+ // Without conversion
+ if (different != 0 && res < 0)
+ {
+ res = docoerce(session, target, m, types, out, cnvIn2);
+ }
+ else if (different != 0 && res > 0)
+ {
+ int res2;
+ Object[] out2 = out.clone();
+ res2 = docoerce(session, target, m, types, out2, cnvIn2) + different * 2;
+ if (res >= 0 && res2 <= res)
+ {
+ res = res2;
+ System.arraycopy(out2, 0, out, 0, out.length);
+ }
+ }
// Check if the command takes a session
if (res < 0 && (types.length > 0) && types[0].isInterface()
&& types[0].isAssignableFrom(session.getClass()))
{
- in.add(0, session);
- res = docoerce(session, target, m, types, out, in);
+ cnvIn.add(0, session);
+ res = docoerce(session, target, m, types, out, cnvIn);
+ if (different != 0 && res < 0)
+ {
+ cnvIn2.add(0, session);
+ res = docoerce(session, target, m, types, out, cnvIn2);
+ }
+ else if (different != 0 && res > 0)
+ {
+ int res2;
+ Object[] out2 = out.clone();
+ res2 = docoerce(session, target, m, types, out2, cnvIn2) + different * 2;
+ if (res >= 0 && res2 <= res)
+ {
+ res = res2;
+ System.arraycopy(out2, 0, out, 0, out.length);
+ }
+ }
}
return res;
}
Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java?rev=1778273&r1=1778272&r2=1778273&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java (original)
+++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestCoercion.java Wed Jan 11 11:08:21 2017
@@ -124,6 +124,11 @@ public class TestCoercion extends Abstra
return "string";
}
+ public String mymethod(String loc)
+ {
+ return loc;
+ }
+
@Test
public void testBestCoercion() throws Exception
{
@@ -135,6 +140,16 @@ public class TestCoercion extends Abstra
assertEquals("bundles '1'", "string", c.execute("bundles '1'"));
}
+ @Test
+ public void testNoCoercionToString() throws Exception
+ {
+ Context c = new Context();
+ c.addCommand("mymethod", this);
+
+ assertEquals("mymethod '1.10'", "1.10", c.execute("mymethod '1.10'"));
+ assertEquals("mymethod 1.10", "1.10", c.execute("mymethod 1.10"));
+ }
+
@Descriptor("list all installed bundles")
public String p0(
@Descriptor("show location") @Parameter(names = { "-l", "--location" }, presentValue = "true", absentValue = "false") boolean showLoc,