001 /*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006 package org.bridj.util;
007
008 import java.lang.reflect.Constructor;
009 import java.lang.reflect.ParameterizedType;
010 import java.lang.reflect.Type;
011 import java.lang.reflect.Method;
012 import java.io.*;
013 import java.lang.reflect.Array;
014 import java.lang.reflect.GenericArrayType;
015 import java.lang.reflect.TypeVariable;
016 import java.lang.reflect.WildcardType;
017 import java.nio.*;
018
019 /**
020 * Miscellaneous utility methods.
021 * @author ochafik
022 */
023 public class Utils {
024 public static int getEnclosedConstructorParametersOffset(Constructor c) {
025 Class<?> enclosingClass = c.getDeclaringClass().getEnclosingClass();
026 Class[] params = c.getParameterTypes();
027 int overrideOffset = params.length > 0 && enclosingClass != null && enclosingClass == params[0] ? 1 : 0;
028 return overrideOffset;
029 }
030 public static boolean isDirect(Buffer b) {
031 if (b instanceof ByteBuffer)
032 return ((ByteBuffer)b).isDirect();
033 if (b instanceof IntBuffer)
034 return ((IntBuffer)b).isDirect();
035 if (b instanceof LongBuffer)
036 return ((LongBuffer)b).isDirect();
037 if (b instanceof DoubleBuffer)
038 return ((DoubleBuffer)b).isDirect();
039 if (b instanceof FloatBuffer)
040 return ((FloatBuffer)b).isDirect();
041 if (b instanceof ShortBuffer)
042 return ((ShortBuffer)b).isDirect();
043 if (b instanceof CharBuffer)
044 return ((CharBuffer)b).isDirect();
045 return false;
046 }
047
048 public static boolean isSignedIntegral(Type tpe) {
049 return
050 tpe == int.class || tpe == Integer.class ||
051 tpe == long.class || tpe == Long.class ||
052 tpe == short.class || tpe == Short.class ||
053 tpe == byte.class || tpe == Byte.class;
054 }
055
056 public static String toString(Type t) {
057 if (t == null)
058 return "?";
059 if (t instanceof Class)
060 return ((Class)t).getName();
061 return t.toString();
062 }
063 public static String toString(Throwable th) {
064 StringWriter sw = new StringWriter();
065 PrintWriter pw = new PrintWriter(sw);
066 th.printStackTrace(pw);
067 return sw.toString();
068 }
069
070 public static boolean eq(Object a, Object b) {
071 if ((a == null) != (b == null))
072 return false;
073 return !(a != null && !a.equals(b));
074 }
075 public static <T> Class<T> getClass(Type type) {
076 if (type == null)
077 return null;
078 if (type instanceof Class<?>)
079 return (Class<T>)type;
080 if (type instanceof ParameterizedType)
081 return getClass(((ParameterizedType)type).getRawType());
082 if (type instanceof GenericArrayType)
083 return (Class)Array.newInstance(getClass(((GenericArrayType)type).getGenericComponentType()), 0).getClass();
084 if (type instanceof WildcardType)
085 return null;
086 if (type instanceof TypeVariable) {
087 Type[] bounds = ((TypeVariable)type).getBounds();
088 return getClass(bounds[0]);
089 }
090 throw new UnsupportedOperationException("Cannot infer class from type " + type);
091 }
092
093 public static Type getParent(Type type) {
094 if (type instanceof Class)
095 return ((Class)type).getSuperclass();
096 else
097 // TODO handle templates !!!
098 return getParent(getClass(type));
099 }
100
101 public static Class[] getClasses(Type[] types) {
102 int n = types.length;
103 Class[] ret = new Class[n];
104 for (int i = 0; i < n; i++)
105 ret[i] = getClass(types[i]);
106 return ret;
107 }
108
109 public static Type getUniqueParameterizedTypeParameter(Type type) {
110 return (type instanceof ParameterizedType) ? ((ParameterizedType)type).getActualTypeArguments()[0] : null;
111 }
112 }