vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

java_method_references_signature.java (4654B)


      1 // VIM_TEST_SETUP let g:java_highlight_functions = 'style'
      2 // VIM_TEST_SETUP let g:java_highlight_signature = 1
      3 // VIM_TEST_SETUP let g:java_highlight_generics = 1
      4 
      5 import java.lang.invoke.MethodHandle;
      6 import java.util.function.BiPredicate;
      7 import java.util.function.Consumer;
      8 import java.util.function.Function;
      9 import java.util.function.IntFunction;
     10 import java.util.function.IntSupplier;
     11 import java.util.function.Predicate;
     12 import java.util.function.Supplier;
     13 import java.util.function.ToIntFunction;
     14 import java.util.function.UnaryOperator;
     15 
     16 class MethodReferences$Tests
     17 {
     18 	static {
     19 		// Primary :: [TypeArguments] Identifier
     20 		try {
     21 			Runnable r1 = ((Runtime) null)::gc;
     22 		} catch (NullPointerException expected) {
     23 		}
     24 
     25 		Supplier<Integer> s1 = ((Number) 0)::hashCode;
     26 		Supplier<Integer> s2 = ((Comparable<?>) '\0')::hashCode;
     27 		Supplier<Integer> s3 = ((Comparable<?>) false)::hashCode;
     28 		Supplier<Integer> s4 = "::"::hashCode;
     29 		Supplier<Class<?>> s5 = int[].class::arrayType;
     30 		Supplier<Integer> s6 = new MethodReferences$Tests() ::
     31 			hashCode;
     32 		Supplier<Integer> s7 = ((Number)
     33 			(new MethodReferences$Tests().xy)[0])::intValue;
     34 		Supplier<int[]> s8 = new MethodReferences$Tests().xy::
     35 			clone;
     36 		Consumer<Object> c1 = System.out :: println;
     37 		Supplier<byte[]> s9 = ((Supplier<String>) ()->"()").get()
     38 			::getBytes;
     39 		Supplier<String> sa = ((Supplier<String>)
     40 			((Supplier<String>) ((Supplier<String>)
     41 			((Supplier<String>) ((Supplier<String>)
     42 			() -> "() -> ()")
     43 			::toString)
     44 			::toString)
     45 			::get)
     46 			::toString)
     47 			::toString;
     48 
     49 		// ExpressionName :: [TypeArguments] Identifier
     50 		// ReferenceType :: [TypeArguments] Identifier
     51 		Function<String, IntSupplier> f1 = s ->
     52 						s :: length;
     53 		Function<int[][], Supplier<int[]>> f2 = ii ->
     54 			((int[]) (ii.length > 0 ? ii[0] : ii))
     55 							:: clone;
     56 		UnaryOperator<String> uo1 = String::valueOf;
     57 		ToIntFunction<String> tif1 = s -> s.transform(
     58 						String :: length);
     59 
     60 		// ClassType :: [TypeArguments] new
     61 		// ArrayType :: new
     62 		Function<Object, C2> f3 = C2::<Object>new;
     63 		Function<C2, C2.C21> f4 = pci -> pci.new
     64 					<String>C21(null); // Cf. "d".
     65 		Supplier<C1<?>> sb = C1::new;
     66 		Function<Byte, C1<?>> f5 = C1<Void> :: <Byte> new;
     67 		IntFunction<C1<?>[]> if1 = C1<?>[] :: new;
     68 		IntFunction<byte[]> if2 = byte[] :: new;
     69 	}
     70 
     71 	final int[] xy = { 0, 1 };
     72 
     73 	// super :: [TypeArguments] Identifier
     74 	// TypeName . super :: [TypeArguments] Identifier
     75 	<T> MethodReferences$Tests()
     76 	{
     77 		Predicate<T> p1 = MethodReferences$Tests.super::equals;
     78 		Predicate<T> p2 = MethodReferences$Tests.this::equals;
     79 	}
     80 
     81 	interface I4<T> extends I3<T>
     82 	{
     83 		default Predicate<T> superEqualist()
     84 		{
     85 			return I3
     86 				.super::equals;	/* "a" */
     87 		}
     88 	}
     89 
     90 	interface I3<T> extends I2<T>
     91 	{
     92 		default Predicate<T> superEqualist()
     93 		{
     94 			return I2.
     95 				super::equals;	/* "b" */
     96 		}
     97 	}
     98 
     99 	interface I2<T> extends I1<T>
    100 	{
    101 		default Predicate<T> superEqualist()
    102 		{	/* Non-capturing gymnastics for super::equals. */
    103 			return Function.<Function<MethodHandle,
    104 							Predicate<T>>>
    105 								identity()
    106 				.apply(mh -> o -> MethodReferences$Tests
    107 						.invokePredicate(mh, o))
    108 				.apply(EQUALS.bindTo(this));
    109 		}
    110 	}
    111 
    112 	interface I1<T>
    113 	{
    114 		default Predicate<T> equalist()
    115 		{	/* Non-capturing gymnastics for this::equals. */
    116 			return Function.<Function<I1<T>, Predicate<T>>>
    117 								identity()
    118 				.apply(that -> o -> Function
    119 						.<BiPredicate<I1<T>, T>>
    120 								identity()
    121 					.apply(I1<T>::	/* "c" */
    122 						equals)
    123 					.test(that, o))
    124 				.apply(I1.this);
    125 		}
    126 	}
    127 
    128 	static <T> boolean invokePredicate(MethodHandle mh, T o)
    129 	{
    130 		try {
    131 			return (boolean) mh.invokeExact(o);
    132 		} catch (Throwable th) {
    133 			throw new RuntimeException(th);
    134 		}
    135 	}
    136 
    137 	private static final MethodHandle EQUALS;
    138 
    139 	static {
    140 		try {
    141 			EQUALS = java.lang.invoke.MethodHandles.lookup()
    142 							.findSpecial(
    143 				I1.class,
    144 				"equals",
    145 				java.lang.invoke.MethodType.methodType(
    146 							boolean.class,
    147 							Object.class),
    148 				I2.class);
    149 		} catch (ReflectiveOperationException e) {
    150 			throw new Error(e);
    151 		}
    152 	}
    153 
    154 	static class C1<T>
    155 	{
    156 		C1() { }
    157 		<A> C1(A dummy) { }
    158 	}
    159 
    160 	static class C2
    161 	{
    162 		C2() { <String> this(""); }
    163 
    164 		<A> C2(A dummy)
    165 		{
    166 			C2.stringer().apply(((Function<C2, C2.C21>)
    167 						C2.C21::new)	/* "d" */
    168 					.apply(C2.this));
    169 		}
    170 
    171 		class C21
    172 		{
    173 			C21() { <String> this(""); }
    174 
    175 			<B> C21(B dummy)
    176 			{
    177 				C2.stringer().apply(C2.this);
    178 			}
    179 		}
    180 
    181 		static <T extends Object> Function<T, String> stringer()
    182 		{
    183 			return T::toString;	/* "e" */
    184 		}
    185 	}
    186 
    187 	static {
    188 		((Supplier<Class<?>>)((Supplier<C2.C21>)()->new
    189 			<Object>C2().new <Object> C21(new<Object>
    190 				C1< Object >((C1<?>)null)))::getClass).get();
    191 	}
    192 }