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 2016/12/22 10:53:02 UTC
svn commit: r1775619 - in /felix/trunk/gogo/jline/src:
main/java/org/apache/felix/gogo/jline/ test/java/org/apache/felix/gogo/jline/
Author: gnodet
Date: Thu Dec 22 10:53:01 2016
New Revision: 1775619
URL: http://svn.apache.org/viewvc?rev=1775619&view=rev
Log:
[FELIX-5463] [gogo][jline] The Main class should add a default Function->FunctionInterface converter
Also, make sure default methods are supported
Added:
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/BaseConverters.java
felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/BaseConvertersTest.java
Modified:
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java
felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java
Added: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/BaseConverters.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/BaseConverters.java?rev=1775619&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/BaseConverters.java (added)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/BaseConverters.java Thu Dec 22 10:53:01 2016
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.jline;
+
+import org.apache.felix.service.command.Converter;
+import org.apache.felix.service.command.Function;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+
+public class BaseConverters implements Converter {
+
+ public Object convert(Class<?> desiredType, final Object in) throws Exception {
+ if (desiredType == Class.class) {
+ try {
+ return Class.forName(in.toString());
+ } catch (ClassNotFoundException e) {
+ return null;
+ }
+ }
+
+ if (desiredType.isAssignableFrom(String.class) && in instanceof InputStream) {
+ return read(((InputStream) in));
+ }
+
+ if (in instanceof Function && isFunctional(desiredType)) {
+ return Proxy.newProxyInstance(desiredType.getClassLoader(),
+ new Class[]{desiredType}, new InvocationHandler() {
+ Function command = ((Function) in);
+
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+ if (isObjectMethod(method)) {
+ return method.invoke(command, args);
+ } else if (method.isDefault()) {
+ final Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
+ field.setAccessible(true);
+ final MethodHandles.Lookup lookup = (MethodHandles.Lookup) field.get(null);
+ return lookup
+ .unreflectSpecial(method, method.getDeclaringClass())
+ .bindTo(proxy)
+ .invokeWithArguments(args);
+ } else {
+ return command.execute(null, Arrays.asList(args));
+ }
+ }
+ });
+ }
+
+ return null;
+ }
+
+ public CharSequence format(Object target, int level, Converter converter)
+ throws IOException {
+ if (level == INSPECT && target instanceof InputStream) {
+ return read(((InputStream) target));
+ }
+ return null;
+ }
+
+ private CharSequence read(InputStream in) throws IOException {
+ int c;
+ StringBuffer sb = new StringBuffer();
+ while ((c = in.read()) > 0) {
+ if (c >= 32 && c <= 0x7F || c == '\n' || c == '\r') {
+ sb.append((char) c);
+ } else {
+ String s = Integer.toHexString(c).toUpperCase();
+ sb.append("\\");
+ if (s.length() < 1) {
+ sb.append(0);
+ }
+ sb.append(s);
+ }
+ }
+ return sb;
+ }
+
+ public static boolean isFunctional(Class<?> clazz) {
+ if (!clazz.isInterface()) {
+ return false;
+ }
+ int nb = 0;
+ for (Method method : clazz.getMethods()) {
+ if (method.isDefault() || isObjectMethod(method)) {
+ continue;
+ }
+ nb++;
+ }
+ return nb == 1;
+ }
+
+ public static boolean isObjectMethod(Method method) {
+ switch (method.getName()) {
+ case "toString":
+ if (method.getParameterCount() == 0 && method.getReturnType() == String.class) {
+ return true;
+ }
+ break;
+ case "equals":
+ if (method.getParameterCount() == 1
+ && method.getParameterTypes()[0] == Object.class
+ && method.getReturnType() == boolean.class) {
+ return true;
+ }
+ break;
+ case "hashCode":
+ if (method.getParameterCount() == 0 && method.getReturnType() == int.class) {
+ return true;
+ }
+ break;
+ }
+ return false;
+ }
+
+}
Modified: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java?rev=1775619&r1=1775618&r2=1775619&view=diff
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java (original)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Converters.java Thu Dec 22 10:53:01 2016
@@ -19,22 +19,16 @@
package org.apache.felix.gogo.jline;
import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.Arrays;
import java.util.Formatter;
import org.apache.felix.service.command.Converter;
-import org.apache.felix.service.command.Function;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.startlevel.StartLevel;
-public class Converters implements Converter {
+public class Converters extends BaseConverters {
private final BundleContext context;
public Converters(BundleContext context) {
@@ -133,32 +127,7 @@ public class Converters implements Conve
return convertServiceReference(in);
}
- if (desiredType == Class.class) {
- try {
- return Class.forName(in.toString());
- } catch (ClassNotFoundException e) {
- return null;
- }
- }
-
- if (desiredType.isAssignableFrom(String.class) && in instanceof InputStream) {
- return read(((InputStream) in));
- }
-
- if (in instanceof Function && desiredType.isInterface()
- && desiredType.getDeclaredMethods().length == 1) {
- return Proxy.newProxyInstance(desiredType.getClassLoader(),
- new Class[]{desiredType}, new InvocationHandler() {
- Function command = ((Function) in);
-
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- return command.execute(null, Arrays.asList(args));
- }
- });
- }
-
- return null;
+ return super.convert(desiredType, in);
}
private Object convertServiceReference(Object in) throws InvalidSyntaxException {
@@ -204,9 +173,6 @@ public class Converters implements Conve
public CharSequence format(Object target, int level, Converter converter)
throws IOException {
- if (level == INSPECT && target instanceof InputStream) {
- return read(((InputStream) target));
- }
if (level == LINE && target instanceof Bundle) {
return print((Bundle) target);
}
@@ -219,25 +185,7 @@ public class Converters implements Conve
if (level == PART && target instanceof ServiceReference) {
return getShortNames((String[]) ((ServiceReference) target).getProperty("objectclass"));
}
- return null;
- }
-
- private CharSequence read(InputStream in) throws IOException {
- int c;
- StringBuffer sb = new StringBuffer();
- while ((c = in.read()) > 0) {
- if (c >= 32 && c <= 0x7F || c == '\n' || c == '\r') {
- sb.append((char) c);
- } else {
- String s = Integer.toHexString(c).toUpperCase();
- sb.append("\\");
- if (s.length() < 1) {
- sb.append(0);
- }
- sb.append(s);
- }
- }
- return sb;
+ return super.format(target, level, converter);
}
}
Modified: felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java?rev=1775619&r1=1775618&r2=1775619&view=diff
==============================================================================
--- felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java (original)
+++ felix/trunk/gogo/jline/src/main/java/org/apache/felix/gogo/jline/Main.java Thu Dec 22 10:53:01 2016
@@ -53,6 +53,7 @@ public class Main {
processor.addCommand("gogo", processor, "addCommand");
processor.addCommand("gogo", processor, "removeCommand");
processor.addCommand("gogo", processor, "eval");
+ processor.addConverter(new BaseConverters());
register(processor, new Builtin(), Builtin.functions);
register(processor, new Procedural(), Procedural.functions);
register(processor, new Posix(processor), Posix.functions);
Added: felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/BaseConvertersTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/BaseConvertersTest.java?rev=1775619&view=auto
==============================================================================
--- felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/BaseConvertersTest.java (added)
+++ felix/trunk/gogo/jline/src/test/java/org/apache/felix/gogo/jline/BaseConvertersTest.java Thu Dec 22 10:53:01 2016
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.gogo.jline;
+
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.command.Function;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class BaseConvertersTest {
+
+ @Test
+ public void testFunctionProxy() throws Exception {
+ Function function = new Function() {
+ @Override
+ public Object execute(CommandSession session, List<Object> arguments) throws Exception {
+ return "Hello ";
+ }
+ public String toString() {
+ return "MyFunction";
+ }
+ };
+ MyType myType = (MyType) new BaseConverters().convert(MyType.class, function);
+ assertEquals("MyFunction", myType.toString());
+ assertEquals("Hello ", myType.run(null));
+ assertEquals("World !", myType.hello());
+ }
+
+ @FunctionalInterface
+ public interface MyType {
+
+ String toString();
+
+ Object run(List<Object> args);
+
+ default String hello() {
+ return "World !";
+ }
+
+ }
+
+}