org.testifyproject.runtime.Desc Maven / Gradle / Ivy
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later,
* or the Apache License Version 2.0.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package org.testifyproject.testifyproject.runtime;
/**
* A support class for implementing $sig
and
* $type
.
* This support class is required at runtime
* only if $sig
or $type
is used.
*/
public class Desc {
/**
* Specifies how a java.lang.Class
object is loaded.
*
* If true, it is loaded by:
*
Thread.currentThread().getContextClassLoader().loadClass()
* If false, it is loaded by Class.forName()
.
* The org.testifyproject.testifyprojectfault value is false.
*/
public static boolean useContextClassLoader = false;
private static Class getClassObject(String name)
throws ClassNotFoundException
{
if (useContextClassLoader)
return Class.forName(name, true, Thread.currentThread().getContextClassLoader());
else
return Class.forName(name);
}
/**
* Interprets the given class name.
* It is used for implementing $class
.
*/
public static Class getClazz(String name) {
try {
return getClassObject(name);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(
"$class: internal error, could not find class '" + name
+ "' (Desc.useContextClassLoader: "
+ Boolean.toString(useContextClassLoader) + ")", e);
}
}
/**
* Interprets the given type org.testifyproject.testifyprojectscriptor representing a method
* signature. It is used for implementing $sig
.
*/
public static Class[] getParams(String org.testifyproject.testifyprojectsc) {
if (org.testifyproject.testifyprojectsc.charAt(0) != '(')
throw new RuntimeException("$sig: internal error");
return getType(org.testifyproject.testifyprojectsc, org.testifyproject.testifyprojectsc.length(), 1, 0);
}
/**
* Interprets the given type org.testifyproject.testifyprojectscriptor.
* It is used for implementing $type
.
*/
public static Class getType(String org.testifyproject.testifyprojectsc) {
Class[] result = getType(org.testifyproject.testifyprojectsc, org.testifyproject.testifyprojectsc.length(), 0, 0);
if (result == null || result.length != 1)
throw new RuntimeException("$type: internal error");
return result[0];
}
private static Class[] getType(String org.testifyproject.testifyprojectsc, int org.testifyproject.testifyprojectscLen,
int start, int num) {
Class clazz;
if (start >= org.testifyproject.testifyprojectscLen)
return new Class[num];
char c = org.testifyproject.testifyprojectsc.charAt(start);
switch (c) {
case 'Z' :
clazz = Boolean.TYPE;
break;
case 'C' :
clazz = Character.TYPE;
break;
case 'B' :
clazz = Byte.TYPE;
break;
case 'S' :
clazz = Short.TYPE;
break;
case 'I' :
clazz = Integer.TYPE;
break;
case 'J' :
clazz = Long.TYPE;
break;
case 'F' :
clazz = Float.TYPE;
break;
case 'D' :
clazz = Double.TYPE;
break;
case 'V' :
clazz = Void.TYPE;
break;
case 'L' :
case '[' :
return getClassType(org.testifyproject.testifyprojectsc, org.testifyproject.testifyprojectscLen, start, num);
org.testifyproject.testifyprojectfault :
return new Class[num];
}
Class[] result = getType(org.testifyproject.testifyprojectsc, org.testifyproject.testifyprojectscLen, start + 1, num + 1);
result[num] = clazz;
return result;
}
private static Class[] getClassType(String org.testifyproject.testifyprojectsc, int org.testifyproject.testifyprojectscLen,
int start, int num) {
int end = start;
while (org.testifyproject.testifyprojectsc.charAt(end) == '[')
++end;
if (org.testifyproject.testifyprojectsc.charAt(end) == 'L') {
end = org.testifyproject.testifyprojectsc.indexOf(';', end);
if (end < 0)
throw new IndexOutOfBoundsException("bad org.testifyproject.testifyprojectscriptor");
}
String cname;
if (org.testifyproject.testifyprojectsc.charAt(start) == 'L')
cname = org.testifyproject.testifyprojectsc.substring(start + 1, end);
else
cname = org.testifyproject.testifyprojectsc.substring(start, end + 1);
Class[] result = getType(org.testifyproject.testifyprojectsc, org.testifyproject.testifyprojectscLen, end + 1, num + 1);
try {
result[num] = getClassObject(cname.replace('/', '.'));
}
catch (ClassNotFoundException e) {
// "new RuntimeException(e)" is not available in JDK 1.3.
throw new RuntimeException(e.getMessage());
}
return result;
}
}