📄 constantpoolremapper.java
字号:
remapConstantIndexArray(exceptionsAttribute.u2exceptionIndexTable, exceptionsAttribute.u2exceptionIndexTableLength); } public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) { codeAttribute.u2attributeNameIndex = remapConstantIndex(codeAttribute.u2attributeNameIndex); // Initially, the code attribute editor doesn't contain any changes. codeAttributeEditor.reset(codeAttribute.u4codeLength); // Remap the constant pool references of the instructions. codeAttribute.instructionsAccept(clazz, method, this); // Apply the code atribute editor. It will only contain any changes if // the code length is changing at any point. codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute); // Remap the constant pool references of the exceptions and attributes. codeAttribute.exceptionsAccept(clazz, method, this); codeAttribute.attributesAccept(clazz, method, this); } public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute) { stackMapAttribute.u2attributeNameIndex = remapConstantIndex(stackMapAttribute.u2attributeNameIndex); // Remap the constant pool references of the stack map frames. stackMapAttribute.stackMapFramesAccept(clazz, method, codeAttribute, this); } public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute) { stackMapTableAttribute.u2attributeNameIndex = remapConstantIndex(stackMapTableAttribute.u2attributeNameIndex); // Remap the constant pool references of the stack map frames. stackMapTableAttribute.stackMapFramesAccept(clazz, method, codeAttribute, this); } public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute) { lineNumberTableAttribute.u2attributeNameIndex = remapConstantIndex(lineNumberTableAttribute.u2attributeNameIndex); } public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute) { localVariableTableAttribute.u2attributeNameIndex = remapConstantIndex(localVariableTableAttribute.u2attributeNameIndex); // Remap the constant pool references of the local variables. localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); } public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute) { localVariableTypeTableAttribute.u2attributeNameIndex = remapConstantIndex(localVariableTypeTableAttribute.u2attributeNameIndex); // Remap the constant pool references of the local variables. localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); } public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute) { annotationsAttribute.u2attributeNameIndex = remapConstantIndex(annotationsAttribute.u2attributeNameIndex); // Remap the constant pool references of the annotations. annotationsAttribute.annotationsAccept(clazz, this); } public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute) { parameterAnnotationsAttribute.u2attributeNameIndex = remapConstantIndex(parameterAnnotationsAttribute.u2attributeNameIndex); // Remap the constant pool references of the annotations. parameterAnnotationsAttribute.annotationsAccept(clazz, method, this); } public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute) { annotationDefaultAttribute.u2attributeNameIndex = remapConstantIndex(annotationDefaultAttribute.u2attributeNameIndex); // Remap the constant pool references of the annotations. annotationDefaultAttribute.defaultValueAccept(clazz, this); } // Implementations for InnerClassesInfoVisitor. public void visitInnerClassesInfo(Clazz clazz, InnerClassesInfo innerClassesInfo) { if (innerClassesInfo.u2innerClassIndex != 0) { innerClassesInfo.u2innerClassIndex = remapConstantIndex(innerClassesInfo.u2innerClassIndex); } if (innerClassesInfo.u2outerClassIndex != 0) { innerClassesInfo.u2outerClassIndex = remapConstantIndex(innerClassesInfo.u2outerClassIndex); } if (innerClassesInfo.u2innerNameIndex != 0) { innerClassesInfo.u2innerNameIndex = remapConstantIndex(innerClassesInfo.u2innerNameIndex); } } // Implementations for ExceptionInfoVisitor. public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo) { if (exceptionInfo.u2catchType != 0) { exceptionInfo.u2catchType = remapConstantIndex(exceptionInfo.u2catchType); } } // Implementations for InstructionVisitor. public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {} public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction) { // Is the new constant pool index different from the original one? int newConstantIndex = remapConstantIndex(constantInstruction.constantIndex); if (newConstantIndex != constantInstruction.constantIndex) { // Replace the instruction. constantInstruction = new ConstantInstruction().copy(constantInstruction); constantInstruction.constantIndex = newConstantIndex; constantInstruction.shrink(); codeAttributeEditor.replaceInstruction(offset, constantInstruction); } } // Implementations for StackMapFrameVisitor. public void visitAnyStackMapFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, StackMapFrame stackMapFrame) {} public void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame) { // Remap the constant pool references of the verification types. sameOneFrame.stackItemAccept(clazz, method, codeAttribute, offset, this); } public void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame) { // Remap the constant pool references of the verification types. moreZeroFrame.additionalVariablesAccept(clazz, method, codeAttribute, offset, this); } public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame) { // Remap the constant pool references of the verification types. fullFrame.variablesAccept(clazz, method, codeAttribute, offset, this); fullFrame.stackAccept(clazz, method, codeAttribute, offset, this); } // Implementations for VerificationTypeVisitor. public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType) {} public void visitObjectType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ObjectType objectType) { objectType.u2classIndex = remapConstantIndex(objectType.u2classIndex); } // Implementations for LocalVariableInfoVisitor. public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo) { localVariableInfo.u2nameIndex = remapConstantIndex(localVariableInfo.u2nameIndex); localVariableInfo.u2descriptorIndex = remapConstantIndex(localVariableInfo.u2descriptorIndex); } // Implementations for LocalVariableTypeInfoVisitor. public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo) { localVariableTypeInfo.u2nameIndex = remapConstantIndex(localVariableTypeInfo.u2nameIndex); localVariableTypeInfo.u2signatureIndex = remapConstantIndex(localVariableTypeInfo.u2signatureIndex); } // Implementations for AnnotationVisitor. public void visitAnnotation(Clazz clazz, Annotation annotation) { annotation.u2typeIndex = remapConstantIndex(annotation.u2typeIndex); // Remap the constant pool references of the element values. annotation.elementValuesAccept(clazz, this); } // Implementations for ElementValueVisitor. public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue) { constantElementValue.u2elementNameIndex = remapConstantIndex(constantElementValue.u2elementNameIndex); constantElementValue.u2constantValueIndex = remapConstantIndex(constantElementValue.u2constantValueIndex); } public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue) { enumConstantElementValue.u2elementNameIndex = remapConstantIndex(enumConstantElementValue.u2elementNameIndex); enumConstantElementValue.u2typeNameIndex = remapConstantIndex(enumConstantElementValue.u2typeNameIndex); enumConstantElementValue.u2constantNameIndex = remapConstantIndex(enumConstantElementValue.u2constantNameIndex); } public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue) { classElementValue.u2elementNameIndex = remapConstantIndex(classElementValue.u2elementNameIndex); classElementValue.u2classInfoIndex = remapConstantIndex(classElementValue.u2classInfoIndex); } public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue) { annotationElementValue.u2elementNameIndex = remapConstantIndex(annotationElementValue.u2elementNameIndex); // Remap the constant pool references of the annotation. annotationElementValue.annotationAccept(clazz, this); } public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue) { arrayElementValue.u2elementNameIndex = remapConstantIndex(arrayElementValue.u2elementNameIndex); // Remap the constant pool references of the element values. arrayElementValue.elementValuesAccept(clazz, annotation, this); } // Small utility methods. /** * Remaps all constant pool indices in the given array. */ private void remapConstantIndexArray(int[] array, int length) { for (int index = 0; index < length; index++) { array[index] = remapConstantIndex(array[index]); } } /** * Returns the new constant pool index of the entry at the * given index. */ private int remapConstantIndex(int constantIndex) { return constantIndexMap[constantIndex]; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -