Ver código fonte

yea: parameter types for lambdas :)

Ranides Atterwim 10 anos atrás
pai
commit
d055c9f35f

Diferenças do arquivo suprimidas por serem muito extensas
+ 9 - 0
assira.drafts/nb_jr_remoting.cfg


+ 106 - 0
assira.drafts/src/main/java/net/ranides/assira/reflection/LambdaParams.java

@@ -0,0 +1,106 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.drafts
+ */
+
+package net.ranides.assira.reflection;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import net.ranides.asm.Type;
+import net.ranides.assira.text.StringUtils;
+import sun.reflect.ConstantPool;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class LambdaParams {
+
+	public static void main(String[] args) throws Exception {
+		LambdaParams that = new LambdaParams();
+		System.out.printf("(Integer a) -> String %n");
+		that.runme( (Integer a) -> "A" );
+		System.out.printf("%n%n");
+		
+		System.out.printf("(int[] a) -> Float %n");
+		that.runme( (int[] a) -> 11.0f );
+		System.out.printf("%n%n");
+		
+		System.out.printf("(String a) -> Character %n");
+		that.runme( OtherLambda::flow );
+		System.out.printf("%n%n");
+		
+		System.out.printf("(Params2 a) -> Integer %n");
+		that.runme( (Param2 v) -> 3*OtherLambda.flow("s") );
+		System.out.printf("%n%n");
+
+		System.out.printf("(Param2, Float) -> Integer %n");
+		that.runme( (Param2 v, Float z) -> z * OtherLambda.flow("s") );
+		System.out.printf("%n%n");
+	}
+	
+	public <T,R> void runme(Function<T,R> function) throws Exception {
+		dump(function);
+	}
+	
+	public <T,U,R> void runme(BiFunction<T,U,R> function) throws Exception {
+		dump(function);
+	}
+	
+	private static void dump(Object function) throws Exception {
+		Method GET_CONSTANT_POOL = Class.class.getDeclaredMethod("getConstantPool");
+		GET_CONSTANT_POOL.setAccessible(true);
+		ConstantPool cp = (ConstantPool) GET_CONSTANT_POOL.invoke(function.getClass());
+		
+		for(int i=0; i<cp.getSize(); i++) {
+			String[] info = null;
+			try {
+				info = cp.getMemberRefInfoAt(i);
+			} catch(Exception ex) {
+				continue;
+			}
+			
+			String parent = info[0];
+			
+			if("java/lang/Object".equals(parent) || "java/lang/Class".equals(parent)) {
+				continue;
+			}
+			if(parent.startsWith("com/zeroturnaround/")) {
+				continue;
+			}
+			try {
+				String name = info[1];
+				Type type = Type.getReturnType(info[2]);
+				List<Type> tparams = Arrays.asList(Type.getArgumentTypes(info[2]));
+				List<String> sparams = tparams.stream().map(Type::getClassName).collect(Collectors.toList());
+
+				System.out.printf("[%d] (%s) -> %s   @ %s # %s%n", 
+					i, 
+					StringUtils.join(sparams, ", "),
+					type.getClassName(),
+					parent, name
+				);
+			} catch(Exception ex) {
+				System.out.printf("[%d] ERROR = %s # %s : %s%n", i, info[0], info[1], ex);
+			}
+//			break;
+			
+		}
+		
+//		System.out.printf("%s%n", Arrays.asList(constantPool.getMemberRefInfoAt(2)));
+		
+//		String[] methodRefInfo = constantPool.getMemberRefInfoAt(constantPool.getSize() - 2);
+//
+//		int argumentIndex = 0;
+//		String argumentType = jdk.internal.org.objectweb.asm.Type.getArgumentTypes(methodRefInfo[2])[argumentIndex].getClassName();
+//		Class<?> type = (Class<?>) Class.forName(argumentType);
+	}
+	
+}

+ 20 - 0
assira.drafts/src/main/java/net/ranides/assira/reflection/OtherLambda.java

@@ -0,0 +1,20 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.drafts
+ */
+
+package net.ranides.assira.reflection;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class OtherLambda {
+
+	public static Character flow(String v) {
+		return 'a';
+	}
+	
+}

+ 28 - 0
assira.drafts/src/main/java/net/ranides/assira/reflection/Param2.java

@@ -0,0 +1,28 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.drafts
+ */
+
+package net.ranides.assira.reflection;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class Param2 {
+
+	public int apply() {
+		return 0;
+	}
+	
+	public float print(String message) {
+		return 0;
+	}
+	
+	protected String append(char[] data) {
+		return "";
+	}
+	
+}