在学习JDK的源码过程当中我遇到了一些有趣有用的方法,在此以前若是要使用这些工具方法,我首先会想到的是commons-lang
和guava
这样的语言扩展包,但如今若是是写一些demo,使用原生便可达到目的。固然咱们也不可否认它们的做用,在平时的工做项目中几乎都会引入这些语言扩展包,直接使用他们也使得编程风格统一,并且还可以对低版本的JDK提供支持。
如下收集的代码片断可能会逐渐增长,也可能不会。html
java.util.Objects
工具类,我以为好用的几个方法java
public static boolean equals(Object var0, Object var1) { return var0 == var1 || var0 != null && var0.equals(var1); } public static int hashCode(Object var0) { return var0 != null ? var0.hashCode() : 0; } public static <T> T requireNonNull(T var0) { if (var0 == null) { throw new NullPointerException(); } else { return var0; } } public static <T> T requireNonNull(T var0, String var1) { if (var0 == null) { throw new NullPointerException(var1); } else { return var0; } }
除此以外还应该从Objects
学习到编写工具类的正确的规范,sql
这个最先应该是在Hello World程序中见到的,推荐它的一个方法编程
/** * Returns the same hash code for the given object as * would be returned by the default method hashCode(), * whether or not the given object's class overrides * hashCode(). * The hash code for the null reference is zero. * * @param x object for which the hashCode is to be calculated * @return the hashCode * @since JDK1.1 */ public static native int identityHashCode(Object x);
注释写得很明白了,无论一个对象实例的class有没有覆盖Object的hashCode方法,都能使用这个方法得到hash值。segmentfault
咱们能够从如下代码得到提示,代码来自HashMap
,app
/** * Returns x's Class if it is of the form "class C implements * Comparable<C>", else null. */ static Class<?> comparableClassFor(Object x) { if (x instanceof Comparable) { Class<?> c; Type[] ts, as; Type t; ParameterizedType p; if ((c = x.getClass()) == String.class) // bypass checks return c; if ((ts = c.getGenericInterfaces()) != null) { for (int i = 0; i < ts.length; ++i) { if (((t = ts[i]) instanceof ParameterizedType) && ((p = (ParameterizedType)t).getRawType() == Comparable.class) && (as = p.getActualTypeArguments()) != null && as.length == 1 && as[0] == c) // type arg is c return c; } } } return null; }
这里的逻辑是得到类C
,而后获取它实现的接口Comparable<C>
,而后从这个Comparable<C>
中得到类型参数C
,而后比较这两个类型是否相等。虽然咱们一直据说Java的泛型是类型擦除式,可是在这里咱们是能够得到泛型的参数类型的。照例用一段demo测试一下,ide
public class ParameterApp { public static void main(String[] args) { StringList list = new StringList(); Class<?> clazz = getTypeArgument(list); System.out.println(clazz.getName()); } static Class<?> getTypeArgument(Object x) { if (x instanceof Collection) { Class<?> c = x.getClass(); Type[] ts, as; Type t; ParameterizedType p; if ((ts = c.getGenericInterfaces()) != null) { for (int i = 0; i < ts.length; ++i) { if (((t = ts[i]) instanceof ParameterizedType) && ((as = ((ParameterizedType)t).getActualTypeArguments()) != null) && as.length == 1) // type arg is c return (Class<?>) as[0]; } } } return null; } static class StringList extends AbstractList<String> implements List<String> { @Override public String get(int i) { return null; } @Override public int size() { return 0; } } }
这个工具类是和反射相关的,让你们知道有这么一个方法函数
@CallerSensitive public static native Class<?> getCallerClass();
我第一次见到这个方法是在java.sql.DriverManager
中的getConnection
方法中见到的工具
@CallerSensitive public static Connection getConnection(String url, String user, String password) throws SQLException { java.util.Properties info = new java.util.Properties(); if (user != null) { info.put("user", user); } if (password != null) { info.put("password", password); } return (getConnection(url, info, Reflection.getCallerClass())); }
Reflection.getCallerClass()
是一个native
方法,返回的是Class<?>
类型,在DriverManager
中使用它的目的是为了得到相应的ClassLoader
,上面的代码是在Java 8中见到的。其中在Java 7中为得到ClassLoader
,DriverManager
就直接提供了native
的方法学习
/* Returns the caller's class loader, or null if none */ private static native ClassLoader getCallerClassLoader();
咱们用一段代码尝试调用这个方法
public class CalleeApp { public void call() { Class<?> clazz = Reflection.getCallerClass(); System.out.println("Hello " + clazz); } }
public class CallerApp { public static void main(String[] args) { CalleeApp app = new CalleeApp(); Caller1 c1 = new Caller1(); c1.run(app); } static class Caller1 { void run(CalleeApp calleeApp) { if (calleeApp == null) { throw new IllegalArgumentException("callee can not be null"); } calleeApp.call(); } } }
执行main方法会抛出异常
Exception in thread "main" java.lang.InternalError: CallerSensitive annotation expected at frame 1
这个错误信息说的是咱们缺乏在函数调用栈开始位置添加CallerSensitive
注解,观察DriverManager
的getConnection
方法确实是有这么个注解的。
那若是给CalleeApp
的call
加上注解,那结果又会怎样呢?
这个方法是来卖萌,它的本义在注释是这样子写的,
/* * <p> * This method is similar to the {@code wait} method of one * argument, but it allows finer control over the amount of time to * wait for a notification before giving up. The amount of real time, * measured in nanoseconds, is given by: * <blockquote> * <pre> * 1000000*timeout+nanos</pre></blockquote> * <p> */
意思是提供精细化的时间衡量,nano
但是纳秒单位啊!!!
而它的实现倒是这样的,
public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos > 0) { timeout++; } wait(timeout); }
除了对传入参数的数值范围校验外,对nano
的使用牢牢是判断这个变量是否大于0,是则给timeout
加1,这只是增长了1毫秒的时间,并无体现出了精细化的地方。