📄 classfilereferencefixer.java
字号:
{ signatureAttrInfo.u2signatureIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newSignature); } } public void visitRuntimeVisibleAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleAnnotationsAttrInfo runtimeVisibleAnnotationsAttrInfo) { // Fix the annotations. runtimeVisibleAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeInvisibleAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleAnnotationsAttrInfo runtimeInvisibleAnnotationsAttrInfo) { // Fix the annotations. runtimeInvisibleAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeVisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleParameterAnnotationsAttrInfo runtimeVisibleParameterAnnotationsAttrInfo) { // Fix the annotations. runtimeVisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeInvisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleParameterAnnotationsAttrInfo runtimeInvisibleParameterAnnotationsAttrInfo) { // Fix the annotations. runtimeInvisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitAnnotationDefaultAttrInfo(ClassFile classFile, AnnotationDefaultAttrInfo annotationDefaultAttrInfo) { // Fix the annotation. annotationDefaultAttrInfo.defaultValueAccept(classFile, this); } // Implementations for LocalVariableInfoVisitor. public void visitLocalVariableInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableInfo localVariableInfo) { // Has the descriptor changed? String descriptor = classFile.getCpString(localVariableInfo.u2descriptorIndex); String newDescriptor = newDescriptor(descriptor, localVariableInfo.referencedClassFile); if (!descriptor.equals(newDescriptor)) { // Refer to a new Utf8 entry. localVariableInfo.u2descriptorIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newDescriptor); } } // Implementations for LocalVariableTypeInfoVisitor. public void visitLocalVariableTypeInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTypeInfo localVariableTypeInfo) { // Has the signature changed? String signature = classFile.getCpString(localVariableTypeInfo.u2signatureIndex); String newSignature = newDescriptor(signature, localVariableTypeInfo.referencedClassFiles); if (!signature.equals(newSignature)) { localVariableTypeInfo.u2signatureIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newSignature); } } // Implementations for AnnotationVisitor. public void visitAnnotation(ClassFile classFile, Annotation annotation) { // Compute the new type name. String typeName = classFile.getCpString(annotation.u2typeIndex); String newTypeName = newDescriptor(typeName, annotation.referencedClassFiles); if (!typeName.equals(newTypeName)) { // Refer to a new Utf8 entry. annotation.u2typeIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newTypeName); } // Fix the element values. annotation.elementValuesAccept(classFile, this); } // Implementations for ElementValueVisitor. public void visitConstantElementValue(ClassFile classFile, Annotation annotation, ConstantElementValue constantElementValue) { } public void visitEnumConstantElementValue(ClassFile classFile, Annotation annotation, EnumConstantElementValue enumConstantElementValue) { // Compute the new type name. String typeName = classFile.getCpString(enumConstantElementValue.u2typeNameIndex); String newTypeName = newDescriptor(typeName, enumConstantElementValue.referencedClassFiles); if (!typeName.equals(newTypeName)) { // Refer to a new Utf8 entry. enumConstantElementValue.u2typeNameIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newTypeName); } } public void visitClassElementValue(ClassFile classFile, Annotation annotation, ClassElementValue classElementValue) { // Compute the new class name. String className = classFile.getCpString(classElementValue.u2classInfoIndex); String newClassName = newDescriptor(className, classElementValue.referencedClassFiles); if (!className.equals(newClassName)) { // Refer to a new Utf8 entry. classElementValue.u2classInfoIndex = constantPoolEditor.addUtf8CpInfo((ProgramClassFile)classFile, newClassName); } } public void visitAnnotationElementValue(ClassFile classFile, Annotation annotation, AnnotationElementValue annotationElementValue) { // Fix the annotation. annotationElementValue.annotationAccept(classFile, this); } public void visitArrayElementValue(ClassFile classFile, Annotation annotation, ArrayElementValue arrayElementValue) { // Fix the element values. arrayElementValue.elementValuesAccept(classFile, annotation, this); } // Small utility methods. private static String newDescriptor(String descriptor, ClassFile referencedClassFile) { // If there is no referenced class, the descriptor won't change. if (referencedClassFile == null) { return descriptor; } // Unravel and reconstruct the class element of the descriptor. DescriptorClassEnumeration descriptorClassEnumeration = new DescriptorClassEnumeration(descriptor); StringBuffer newDescriptorBuffer = new StringBuffer(descriptor.length()); newDescriptorBuffer.append(descriptorClassEnumeration.nextFluff()); // Only if the descriptor contains a class name (e.g. with an array of // primitive types), the descriptor can change. if (descriptorClassEnumeration.hasMoreClassNames()) { String className = descriptorClassEnumeration.nextClassName(); String fluff = descriptorClassEnumeration.nextFluff(); String newClassName = newClassName(className, referencedClassFile); newDescriptorBuffer.append(newClassName); newDescriptorBuffer.append(fluff); } return newDescriptorBuffer.toString(); } private static String newDescriptor(String descriptor, ClassFile[] referencedClassFiles) { // If there are no referenced classes, the descriptor won't change. if (referencedClassFiles == null || referencedClassFiles.length == 0) { return descriptor; } // Unravel and reconstruct the class elements of the descriptor. DescriptorClassEnumeration descriptorClassEnumeration = new DescriptorClassEnumeration(descriptor); StringBuffer newDescriptorBuffer = new StringBuffer(descriptor.length()); newDescriptorBuffer.append(descriptorClassEnumeration.nextFluff()); int index = 0; while (descriptorClassEnumeration.hasMoreClassNames()) { String className = descriptorClassEnumeration.nextClassName(); String fluff = descriptorClassEnumeration.nextFluff(); String newClassName = newClassName(className, referencedClassFiles[index++]); newDescriptorBuffer.append(newClassName); newDescriptorBuffer.append(fluff); } return newDescriptorBuffer.toString(); } /** * Returns a new unique class member name, based on the given name and * descriptor. */ private String newUniqueMemberName(String name, String descriptor) { // TODO: Avoid duplicate constructors. return name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT) ? ClassConstants.INTERNAL_METHOD_NAME_INIT : name + '$' + Long.toHexString(Math.abs((descriptor).hashCode())); } /** * Returns the new class name based on the given class name and the new * name of the given referenced class file. Class names of array types * are handled properly. */ private static String newClassName(String className, ClassFile referencedClassFile) { // If there is no referenced class, the class name won't change. if (referencedClassFile == null) { return className; } // Reconstruct the class name. String newClassName = referencedClassFile.getName(); // Is it an array type? if (className.charAt(0) == ClassConstants.INTERNAL_TYPE_ARRAY) { // Add the array prefixes and suffix "[L...;". newClassName = className.substring(0, className.indexOf(ClassConstants.INTERNAL_TYPE_CLASS_START)+1) + newClassName + ClassConstants.INTERNAL_TYPE_CLASS_END; } return newClassName; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -