下面列出了java.lang.invoke.MethodType#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
/**
* Test of toString method, of class MethodType.
*/
@Test
public void testToString() {
System.out.println("toString");
MethodType[] instances = {mt_viS, mt_OO2, mt_vv, mt_Ov, mt_iSI, mt_ISi, mt_ISI, mt_iSi};
//String expResult = "void[int, class java.lang.String]";
String[] expResults = {
"(int,String)void",
"(Object,Object)Object",
"()void",
"()Object",
"(String,Integer)int",
"(String,int)Integer",
"(String,Integer)Integer",
"(String,int)int"
};
for (int i = 0; i < instances.length; i++) {
MethodType instance = instances[i];
String result = instance.toString();
System.out.println("#"+i+":"+result);
assertEquals("#"+i, expResults[i], result);
}
}
static String getMethodNameWithSignature(MethodType type, String methodName) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
return typeStr.substring(retTypeIndex) + " " + methodName + "(" + typeStr.substring(secondParamIndex, retTypeIndex);
}
static String getMethodNameWithSignature(final MethodType type, final String methodName, final boolean withReturnType) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
final StringBuilder b = new StringBuilder();
if (withReturnType) {
b.append(typeStr, retTypeIndex, typeStr.length()).append(' ');
}
return b.append(methodName).append('(').append(typeStr, secondParamIndex, retTypeIndex).toString();
}
static String getMethodNameWithSignature(final MethodType type, final String methodName) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
return typeStr.substring(retTypeIndex) + " " + methodName + "(" + typeStr.substring(secondParamIndex, retTypeIndex);
}
static String getMethodNameWithSignature(final MethodType type, final String methodName, final boolean withReturnType) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
final StringBuilder b = new StringBuilder();
if (withReturnType) {
b.append(typeStr, retTypeIndex, typeStr.length()).append(' ');
}
return b.append(methodName).append('(').append(typeStr, secondParamIndex, retTypeIndex).toString();
}
public static void check(MethodType ideal, MethodType actual) {
if (!ideal.equals(actual)) {
String idealString = (ideal==null)? "null" : ideal.toString();
String actualString = (actual==null)? "null" : actual.toString();
throw new Error("ERROR: Expected " + idealString + "; found " + actualString);
}
}
static String getMethodNameWithSignature(final MethodType type, final String methodName, final boolean withReturnType) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
final StringBuilder b = new StringBuilder();
if (withReturnType) {
b.append(typeStr, retTypeIndex, typeStr.length()).append(' ');
}
return b.append(methodName).append('(').append(typeStr, secondParamIndex, retTypeIndex).toString();
}
static String getMethodNameWithSignature(final MethodType type, final String methodName, final boolean withReturnType) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
final StringBuilder b = new StringBuilder();
if (withReturnType) {
b.append(typeStr, retTypeIndex, typeStr.length()).append(' ');
}
return b.append(methodName).append('(').append(typeStr, secondParamIndex, retTypeIndex).toString();
}
static String getMethodNameWithSignature(final MethodType type, final String methodName, final boolean withReturnType) {
final String typeStr = type.toString();
final int retTypeIndex = typeStr.lastIndexOf(')') + 1;
int secondParamIndex = typeStr.indexOf(',') + 1;
if(secondParamIndex == 0) {
secondParamIndex = retTypeIndex - 1;
}
final StringBuilder b = new StringBuilder();
if (withReturnType) {
b.append(typeStr, retTypeIndex, typeStr.length()).append(' ');
}
return b.append(methodName).append('(').append(typeStr, secondParamIndex, retTypeIndex).toString();
}
private static MethodHandle getStaticMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
MethodHandle handle = lookup.unreflect(method);
MethodType type = handle.type();
Class<?>[] params = type.parameterArray();
int p = 0, pcount = type.parameterCount();
boolean callerContext = false;
// First three parameters are (ExecutionContext, ExecutionContext?, Object=ThisValue)
if (!(p < pcount && ExecutionContext.class.equals(params[p++]))) {
throw new IllegalArgumentException(type.toString());
}
if (p < pcount && ExecutionContext.class.equals(params[p])) {
callerContext = true;
p++;
}
if (!(p < pcount && Object.class.equals(params[p++]))) {
throw new IllegalArgumentException(type.toString());
}
// Always required to return Object (for now at least)
if (!Object.class.equals(type.returnType())) {
throw new IllegalArgumentException(type.toString());
}
// Collect remaining arguments into Object[]
if (!(p + 1 == pcount && Object[].class.equals(params[p]))) {
final int fixedArguments = callerContext ? 3 : 2;
final boolean varargs = handle.isVarargsCollector();
// Otherwise all trailing arguments need to be of type Object or Object[]
for (; p < pcount; ++p) {
if (Object.class.equals(params[p])) {
continue;
}
if (p + 1 == pcount && Object[].class.equals(params[p]) && varargs) {
continue;
}
throw new IllegalArgumentException(type.toString());
}
// Trailing Object[] arguments are no longer spread in var-args methods (jdk8u40, jdk9).
if (varargs) {
handle = handle.asFixedArity();
}
// Convert to (ExecutionContext, Object, ...) -> Object handle
handle = toCanonical(handle, fixedArguments, varargs, method);
}
if (!callerContext) {
handle = MethodHandles.dropArguments(handle, 1, ExecutionContext.class);
}
// assert handle.type().parameterCount() == 4;
// assert handle.type().parameterType(0) == ExecutionContext.class;
// assert handle.type().parameterType(1) == ExecutionContext.class;
// assert handle.type().parameterType(2) == Object.class;
// assert handle.type().parameterType(3) == Object[].class;
// assert handle.type().returnType() == Object.class;
return handle;
}