if (logSet.size() < LOG_MAX_BUFFER) { // accessLog 所对应的 AccessLogData 不应该超过 5000 条 logSet.add(accessLogData); } else { //TODO we needs use force writing to file so that buffer gets clear and new log can be written. logger.warn("AccessLog buffer is full skipping buffer "); } }
Set<AccessLogData> logSet = LOG_ENTRIES.computeIfAbsent(accessLog, k -> new ConcurrentHashSet<>()); 这条代码的逻辑很简单,但是它利用了 java 的新特性,通过传入函数参数,简化了操作。computeIfAbsent 这个函数,先判断 map 中是否有 accessLog 这条记录,如果没有就新建一条记录,以 accessLog 为键进行存储,等价的就是一个 if-else 操作。
publicstatic String getVersion(Class<?> cls, String defaultVersion) { try { // find version info from MANIFEST.MF first // 根据 cls 查找 version 信息,存储在 MANIFEST.MF 文件中 Packagepkg= cls.getPackage(); Stringversion=null; if (pkg != null) { version = pkg.getImplementationVersion(); // 此为获取 MANIFEST.MF 文件中版本信息的函数,比如 Implementation-Version: if (!StringUtils.isEmpty(version)) { return version; }
version = pkg.getSpecificationVersion(); // 此为获取 MANIFEST.MF 文件中版本信息的函数,比如 Specification-Version if (!StringUtils.isEmpty(version)) { return version; } }
// guess version fro jar file name if nothing's found from MANIFEST.MF // 没能从 MANIFEST.MF 文件中获取到 version 信息 CodeSourcecodeSource= cls.getProtectionDomain().getCodeSource(); // 估计是由于某种原因,没能获取到 codeSource,那就使用参数传递的 defaultVersion 信息 if (codeSource == null) { logger.info("No codeSource for class " + cls.getName() + " when getVersion, use default version " + defaultVersion); return defaultVersion; } // 那就根据 jar 包推测版本信息 Stringfile= codeSource.getLocation().getFile(); if (!StringUtils.isEmpty(file) && file.endsWith(".jar")) { version = getFromFile(file); }
// return default version if no version info is found return StringUtils.isEmpty(version) ? defaultVersion : version; } catch (Throwable e) { // return default version when any exception is thrown logger.error("return default version, ignore exception " + e.getMessage(), e); return defaultVersion; } }
// get all public field. for (Field f : c.getFields()) { Stringfn= f.getName(); Class<?> ft = f.getType(); // 避开 static 和 transient 修饰的字段 if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())) { continue; }
// 根据 field 进行构建 // public void setPropertyValue (Object o, String n, Object v){ // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // if( $2.equals("serviceName")){ // w.serviceName=(org.apache.dubbo.demo.DemoService) $3; // return; // } c1.append(" if( $2.equals(\"").append(fn).append("\") ){ w.").append(fn).append("=").append(arg(ft, "$3")).append("; return; }"); // public Object getPropertyValue (Object o, String n){ // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // if( $2.equals("serviceName")){ // return ($w)w.serviceName; // } c2.append(" if( $2.equals(\"").append(fn).append("\") ){ return ($w)w.").append(fn).append("; }"); // 保存字段的 name 和 type pts.put(fn, ft); }
Method[] methods = c.getMethods(); // get all public method. // methods 方法不为空,且不全为 Object 声明的方法 booleanhasMethod= hasMethods(methods); if (hasMethod) { // 通过 methods 对 c3 进行构造 c3.append(" try{"); for (Method m : methods) { //ignore Object's method. if (m.getDeclaringClass() == Object.class) { continue; }
// 拼接完返回值后 // public Object invokeMethod (Object o, String n, Class[]p, Object[]v) throws java.lang.reflect.InvocationTargetException { // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // try { // if ("sayHello".equals($2) && $3.length == 1) { // return ($w) w.sayHello((java.lang.String) $4[0]); // } c3.append(" }");
// 保存拼接的方法名 mns.add(mn); if (m.getDeclaringClass() == c) { // 保存声明方法的类名 dmns.add(mn); } // 保存方法的签名和方法实例 // get method desc. // int do(int arg1) => "do(I)I" // void do(String arg1,boolean arg2) => "do(Ljava/lang/String;Z)V" ms.put(ReflectUtils.getDesc(m), m); } c3.append(" } catch(Throwable e) { "); c3.append(" throw new java.lang.reflect.InvocationTargetException(e); "); c3.append(" }"); }
// 补全括号即剩余信息 // o 方法调用的目标对象,n 方法名,p 方法的参数类型,v 方法的参数 // public Object invokeMethod (Object o, String n, Class[]p, Object[]v) throws java.lang.reflect.InvocationTargetException { // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // try { // if ("sayHello".equals($2) && $3.length == 1) { // return ($w) w.sayHello((java.lang.String) $4[0]); // } // } catch (Throwable e) { // throw new java.lang.reflect.InvocationTargetException(e); // } // throw new org.apache.dubbo.common.bytecode.NoSuchMethodException("Not found method \"" + $2 + "\" in class org.apache.dubbo.demo.DemoService."); // } c3.append(" throw new " + NoSuchMethodException.class.getName() + "(\"Not found method \\\"\"+$2+\"\\\" in class " + c.getName() + ".\"); }");
// deal with get/set method. Matcher matcher; // 这里 ms 保存的是接口中非 Object 类方法的签名和方法实例 for (Map.Entry<String, Method> entry : ms.entrySet()) { // 方法签名 Stringmd= entry.getKey(); // 方法实例 Methodmethod= entry.getValue(); // 匹配 getter 方法 if ((matcher = ReflectUtils.GETTER_METHOD_DESC_PATTERN.matcher(md)).matches()) { Stringpn= propertyName(matcher.group(1)); c2.append(" if( $2.equals(\"").append(pn).append("\") ){ return ($w)w.").append(method.getName()).append("(); }"); pts.put(pn, method.getReturnType()); } elseif ((matcher = ReflectUtils.IS_HAS_CAN_METHOD_DESC_PATTERN.matcher(md)).matches()) { // 匹配 is、has、can 方法 Stringpn= propertyName(matcher.group(1)); c2.append(" if( $2.equals(\"").append(pn).append("\") ){ return ($w)w.").append(method.getName()).append("(); }"); pts.put(pn, method.getReturnType()); } elseif ((matcher = ReflectUtils.SETTER_METHOD_DESC_PATTERN.matcher(md)).matches()) { // 匹配 setter 方法 Class<?> pt = method.getParameterTypes()[0]; Stringpn= propertyName(matcher.group(1)); c1.append(" if( $2.equals(\"").append(pn).append("\") ){ w.").append(method.getName()).append("(").append(arg(pt, "$3")).append("); return; }"); pts.put(pn, pt); } } // c1 的拼接结果 // public void setPropertyValue (Object o, String n, Object v){ // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // throw new org.apache.dubbo.common.bytecode.NoSuchPropertyException("Not found property \"" + $2 + "\" field or setter method in class org.apache.dubbo.demo.DemoService."); // } c1.append(" throw new " + NoSuchPropertyException.class.getName() + "(\"Not found property \\\"\"+$2+\"\\\" field or setter method in class " + c.getName() + ".\"); }");
// c2 的拼接结果 // public Object getPropertyValue (Object o, String n){ // org.apache.dubbo.demo.DemoService w; // try { // w = ((org.apache.dubbo.demo.DemoService) $1); // } catch (Throwable e) { // throw new IllegalArgumentException(e); // } // throw new org.apache.dubbo.common.bytecode.NoSuchPropertyException("Not found property \"" + $2 + "\" field or setter method in class org.apache.dubbo.demo.DemoService."); // } c2.append(" throw new " + NoSuchPropertyException.class.getName() + "(\"Not found property \\\"\"+$2+\"\\\" field or setter method in class " + c.getName() + ".\"); }");