|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.alipay.sofa.runtime.spring.bean; |
| 18 | + |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | +import org.springframework.asm.ClassReader; |
| 22 | +import org.springframework.asm.ClassVisitor; |
| 23 | +import org.springframework.asm.Label; |
| 24 | +import org.springframework.asm.MethodVisitor; |
| 25 | +import org.springframework.asm.Opcodes; |
| 26 | +import org.springframework.asm.SpringAsmInfo; |
| 27 | +import org.springframework.asm.Type; |
| 28 | +import org.springframework.core.BridgeMethodResolver; |
| 29 | +import org.springframework.core.ParameterNameDiscoverer; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | +import org.springframework.util.ClassUtils; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStream; |
| 35 | +import java.lang.reflect.Constructor; |
| 36 | +import java.lang.reflect.Executable; |
| 37 | +import java.lang.reflect.Method; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.concurrent.ConcurrentHashMap; |
| 41 | + |
| 42 | +/** |
| 43 | + * Deprecate in spring, fork codes for compatible |
| 44 | + */ |
| 45 | +@Deprecated |
| 46 | +public class LocalVariableTableParameterNameDiscoverer implements ParameterNameDiscoverer { |
| 47 | + |
| 48 | + private static final Log logger = LogFactory |
| 49 | + .getLog(LocalVariableTableParameterNameDiscoverer.class); |
| 50 | + |
| 51 | + // marker object for classes that do not have any debug info |
| 52 | + private static final Map<Executable, String[]> NO_DEBUG_INFO_MAP = Collections |
| 53 | + .emptyMap(); |
| 54 | + |
| 55 | + // the cache uses a nested index (value is a map) to keep the top level cache relatively small in size |
| 56 | + private final Map<Class<?>, Map<Executable, String[]>> parameterNamesCache = new ConcurrentHashMap<>( |
| 57 | + 32); |
| 58 | + |
| 59 | + @Override |
| 60 | + @Nullable |
| 61 | + public String[] getParameterNames(Method method) { |
| 62 | + Method originalMethod = BridgeMethodResolver.findBridgedMethod(method); |
| 63 | + return doGetParameterNames(originalMethod); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + @Nullable |
| 68 | + public String[] getParameterNames(Constructor<?> ctor) { |
| 69 | + return doGetParameterNames(ctor); |
| 70 | + } |
| 71 | + |
| 72 | + @Nullable |
| 73 | + private String[] doGetParameterNames(Executable executable) { |
| 74 | + Class<?> declaringClass = executable.getDeclaringClass(); |
| 75 | + Map<Executable, String[]> map = this.parameterNamesCache.computeIfAbsent(declaringClass, this::inspectClass); |
| 76 | + return (map != NO_DEBUG_INFO_MAP ? map.get(executable) : null); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Inspects the target class. |
| 81 | + * <p>Exceptions will be logged, and a marker map returned to indicate the |
| 82 | + * lack of debug information. |
| 83 | + */ |
| 84 | + private Map<Executable, String[]> inspectClass(Class<?> clazz) { |
| 85 | + InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz)); |
| 86 | + if (is == null) { |
| 87 | + // We couldn't load the class file, which is not fatal as it |
| 88 | + // simply means this method of discovering parameter names won't work. |
| 89 | + if (logger.isDebugEnabled()) { |
| 90 | + logger.debug("Cannot find '.class' file for class [" + clazz |
| 91 | + + "] - unable to determine constructor/method parameter names"); |
| 92 | + } |
| 93 | + return NO_DEBUG_INFO_MAP; |
| 94 | + } |
| 95 | + // We cannot use try-with-resources here for the InputStream, since we have |
| 96 | + // custom handling of the close() method in a finally-block. |
| 97 | + try { |
| 98 | + ClassReader classReader = new ClassReader(is); |
| 99 | + Map<Executable, String[]> map = new ConcurrentHashMap<>(32); |
| 100 | + classReader.accept(new ParameterNameDiscoveringVisitor(clazz, map), 0); |
| 101 | + if (logger.isWarnEnabled()) { |
| 102 | + logger |
| 103 | + .warn("Using deprecated '-debug' fallback for parameter name resolution. Compile the " |
| 104 | + + "affected code with '-parameters' instead or avoid its introspection: " |
| 105 | + + clazz.getName()); |
| 106 | + } |
| 107 | + return map; |
| 108 | + } catch (IOException ex) { |
| 109 | + if (logger.isDebugEnabled()) { |
| 110 | + logger.debug("Exception thrown while reading '.class' file for class [" + clazz |
| 111 | + + "] - unable to determine constructor/method parameter names", ex); |
| 112 | + } |
| 113 | + } catch (IllegalArgumentException ex) { |
| 114 | + if (logger.isDebugEnabled()) { |
| 115 | + logger |
| 116 | + .debug( |
| 117 | + "ASM ClassReader failed to parse class file [" |
| 118 | + + clazz |
| 119 | + + "], probably due to a new Java class file version that isn't supported yet " |
| 120 | + + "- unable to determine constructor/method parameter names", ex); |
| 121 | + } |
| 122 | + } finally { |
| 123 | + try { |
| 124 | + is.close(); |
| 125 | + } catch (IOException ex) { |
| 126 | + // ignore |
| 127 | + } |
| 128 | + } |
| 129 | + return NO_DEBUG_INFO_MAP; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Helper class that inspects all methods and constructors and then |
| 134 | + * attempts to find the parameter names for the given {@link Executable}. |
| 135 | + */ |
| 136 | + private static class ParameterNameDiscoveringVisitor extends ClassVisitor { |
| 137 | + |
| 138 | + private static final String STATIC_CLASS_INIT = "<clinit>"; |
| 139 | + |
| 140 | + private final Class<?> clazz; |
| 141 | + |
| 142 | + private final Map<Executable, String[]> executableMap; |
| 143 | + |
| 144 | + public ParameterNameDiscoveringVisitor(Class<?> clazz, |
| 145 | + Map<Executable, String[]> executableMap) { |
| 146 | + super(SpringAsmInfo.ASM_VERSION); |
| 147 | + this.clazz = clazz; |
| 148 | + this.executableMap = executableMap; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + @Nullable |
| 153 | + public MethodVisitor visitMethod(int access, String name, String desc, String signature, |
| 154 | + String[] exceptions) { |
| 155 | + // exclude synthetic + bridged && static class initialization |
| 156 | + if (!isSyntheticOrBridged(access) && !STATIC_CLASS_INIT.equals(name)) { |
| 157 | + return new LocalVariableTableVisitor(this.clazz, this.executableMap, name, desc, |
| 158 | + isStatic(access)); |
| 159 | + } |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + private static boolean isSyntheticOrBridged(int access) { |
| 164 | + return (((access & Opcodes.ACC_SYNTHETIC) | (access & Opcodes.ACC_BRIDGE)) > 0); |
| 165 | + } |
| 166 | + |
| 167 | + private static boolean isStatic(int access) { |
| 168 | + return ((access & Opcodes.ACC_STATIC) > 0); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static class LocalVariableTableVisitor extends MethodVisitor { |
| 173 | + |
| 174 | + private static final String CONSTRUCTOR = "<init>"; |
| 175 | + |
| 176 | + private final Class<?> clazz; |
| 177 | + |
| 178 | + private final Map<Executable, String[]> executableMap; |
| 179 | + |
| 180 | + private final String name; |
| 181 | + |
| 182 | + private final Type[] args; |
| 183 | + |
| 184 | + private final String[] parameterNames; |
| 185 | + |
| 186 | + private final boolean isStatic; |
| 187 | + |
| 188 | + private boolean hasLvtInfo = false; |
| 189 | + |
| 190 | + /* |
| 191 | + * The nth entry contains the slot index of the LVT table entry holding the |
| 192 | + * argument name for the nth parameter. |
| 193 | + */ |
| 194 | + private final int[] lvtSlotIndex; |
| 195 | + |
| 196 | + public LocalVariableTableVisitor(Class<?> clazz, Map<Executable, String[]> map, |
| 197 | + String name, String desc, boolean isStatic) { |
| 198 | + super(SpringAsmInfo.ASM_VERSION); |
| 199 | + this.clazz = clazz; |
| 200 | + this.executableMap = map; |
| 201 | + this.name = name; |
| 202 | + this.args = Type.getArgumentTypes(desc); |
| 203 | + this.parameterNames = new String[this.args.length]; |
| 204 | + this.isStatic = isStatic; |
| 205 | + this.lvtSlotIndex = computeLvtSlotIndices(isStatic, this.args); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void visitLocalVariable(String name, String description, String signature, |
| 210 | + Label start, Label end, int index) { |
| 211 | + this.hasLvtInfo = true; |
| 212 | + for (int i = 0; i < this.lvtSlotIndex.length; i++) { |
| 213 | + if (this.lvtSlotIndex[i] == index) { |
| 214 | + this.parameterNames[i] = name; |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void visitEnd() { |
| 221 | + if (this.hasLvtInfo || (this.isStatic && this.parameterNames.length == 0)) { |
| 222 | + // visitLocalVariable will never be called for static no args methods |
| 223 | + // which doesn't use any local variables. |
| 224 | + // This means that hasLvtInfo could be false for that kind of methods |
| 225 | + // even if the class has local variable info. |
| 226 | + this.executableMap.put(resolveExecutable(), this.parameterNames); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + private Executable resolveExecutable() { |
| 231 | + ClassLoader loader = this.clazz.getClassLoader(); |
| 232 | + Class<?>[] argTypes = new Class<?>[this.args.length]; |
| 233 | + for (int i = 0; i < this.args.length; i++) { |
| 234 | + argTypes[i] = ClassUtils.resolveClassName(this.args[i].getClassName(), loader); |
| 235 | + } |
| 236 | + try { |
| 237 | + if (CONSTRUCTOR.equals(this.name)) { |
| 238 | + return this.clazz.getDeclaredConstructor(argTypes); |
| 239 | + } |
| 240 | + return this.clazz.getDeclaredMethod(this.name, argTypes); |
| 241 | + } catch (NoSuchMethodException ex) { |
| 242 | + throw new IllegalStateException( |
| 243 | + "Method [" |
| 244 | + + this.name |
| 245 | + + "] was discovered in the .class file but cannot be resolved in the class object", |
| 246 | + ex); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + private static int[] computeLvtSlotIndices(boolean isStatic, Type[] paramTypes) { |
| 251 | + int[] lvtIndex = new int[paramTypes.length]; |
| 252 | + int nextIndex = (isStatic ? 0 : 1); |
| 253 | + for (int i = 0; i < paramTypes.length; i++) { |
| 254 | + lvtIndex[i] = nextIndex; |
| 255 | + if (isWideType(paramTypes[i])) { |
| 256 | + nextIndex += 2; |
| 257 | + } else { |
| 258 | + nextIndex++; |
| 259 | + } |
| 260 | + } |
| 261 | + return lvtIndex; |
| 262 | + } |
| 263 | + |
| 264 | + private static boolean isWideType(Type aType) { |
| 265 | + // float is not a wide type |
| 266 | + return (aType == Type.LONG_TYPE || aType == Type.DOUBLE_TYPE); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | +} |
0 commit comments