public T create(final Class service) {
Utils.validateServiceInterface(service);
if (validateEagerly) {
eagerlyValidateMethods(service);
}
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class>[] { service },
new InvocationHandler() {
private final Platform platform = Platform.get();
@Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
throws Throwable {
// If the method is a method from Object then defer to normal invocation.
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
ServiceMethod
private static Class> getProxyClass0(ClassLoader loader,
Class>... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
return proxyClassCache.get(loader, interfaces);
}
这里可以看到proxyClassCache是一个WeakCache对象,其初始化代码为
private static final WeakCache[], Class>>
proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
@Override
public Class> apply(ClassLoader loader, Class>[] interfaces) {
Map, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class> interfaceClass = null;
try {
//加载传入的class类,由于initialize传入的是false,所里这里并没有初始化类,
//只能获取该类的相关信息
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
* 是否是接口,如果不是接口,则抛出异常
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
* 是否已经加载过该类,则抛出异常,去重
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
String proxyPkg = null; // package to define proxy class in
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
//因为上面已经将加载的类put进了interfaces,所以这里可以获取相应的方法,属性等。
for (Class> intf : interfaces) {
int flags = intf.getModifiers();
//必须是public类
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
//获取包名,如果两次包名不一样,则抛出一样,因为retrofit之传入一个service,所以这里不会抛出异常。
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) {
// if no non-public proxy interfaces, use the default package.
proxyPkg = "";
}
{
// Android-changed: Generate the proxy directly instead of calling
// through to ProxyGenerator.
//开始组织实际的类。
//首先添加方法,将接口数组中的所有方法都添加到methods以及equals、hashcode、toString方法
List methods = getMethods(interfaces);
Collections.sort(methods, ORDER_BY_SIGNATURE_AND_SUBTYPE);
判断是否存在方法名、参数相同,但是返回值不同的,因为如果存在这种方法,那么生成的实现类会出现编译错误(重载)
validateReturnTypes(methods);
//方法去重,并返回所有的异常信息
List[]> exceptions = deduplicateAndGetExceptions(methods);
Method[] methodsArray = methods.toArray(new Method[methods.size()]);
Class>[][] exceptionsArray = exceptions.toArray(new Class>[exceptions.size()][]);
/*
* Choose a name for the proxy class to generate.
*/
//继续构造了多少次。
long num = nextUniqueNumber.getAndIncrement();
//构造实现类的名称,包名+$Proxy+num
String proxyName = proxyPkg + proxyClassNamePrefix + num;
//交给native方法去构造
return generateProxy(proxyName, interfaces, loader, methodsArray,
exceptionsArray);
}
}
private final ReferenceQueue refQueue
= new ReferenceQueue<>();
// the key type is Object for supporting null key
private final ConcurrentMap>> map
= new ConcurrentHashMap<>();
private final ConcurrentMap, Boolean> reverseMap
= new ConcurrentHashMap<>();
private final BiFunction subKeyFactory;
private final BiFunction valueFactory;
1、java.lang.IllegalStateException: Class invariant violation
at org.apache.log4j.LogManager.getLoggerRepository(LogManager.java:199)at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
at o
hadoop jps出现process information unavailable提示解决办法
jps时出现如下信息:
3019 -- process information unavailable3053 -- process information unavailable2985 -- process information unavailable2917 --
<?php
class Image{
private $path;
function __construct($path='./'){
$this->path=rtrim($path,'/').'/';
}
//水印函数,参数:背景图,水印图,位置,前缀,TMD透明度
public function water($b,$l,$pos