📄 constantpoolremapper.java
字号:
remapCpIndex(lineNumberTableAttrInfo.u2attrNameIndex); } public void visitLocalVariableTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTableAttrInfo localVariableTableAttrInfo) { localVariableTableAttrInfo.u2attrNameIndex = remapCpIndex(localVariableTableAttrInfo.u2attrNameIndex); // Remap the constant pool references of the local variables. localVariableTableAttrInfo.localVariablesAccept(classFile, methodInfo, codeAttrInfo, this); } public void visitLocalVariableTypeTableAttrInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTypeTableAttrInfo localVariableTypeTableAttrInfo) { localVariableTypeTableAttrInfo.u2attrNameIndex = remapCpIndex(localVariableTypeTableAttrInfo.u2attrNameIndex); // Remap the constant pool references of the local variables. localVariableTypeTableAttrInfo.localVariablesAccept(classFile, methodInfo, codeAttrInfo, this); } public void visitSourceFileAttrInfo(ClassFile classFile, SourceFileAttrInfo sourceFileAttrInfo) { sourceFileAttrInfo.u2attrNameIndex = remapCpIndex(sourceFileAttrInfo.u2attrNameIndex); sourceFileAttrInfo.u2sourceFileIndex = remapCpIndex(sourceFileAttrInfo.u2sourceFileIndex); } public void visitSourceDirAttrInfo(ClassFile classFile, SourceDirAttrInfo sourceDirAttrInfo) { sourceDirAttrInfo.u2attrNameIndex = remapCpIndex(sourceDirAttrInfo.u2attrNameIndex); sourceDirAttrInfo.u2sourceDirIndex = remapCpIndex(sourceDirAttrInfo.u2sourceDirIndex); } public void visitDeprecatedAttrInfo(ClassFile classFile, DeprecatedAttrInfo deprecatedAttrInfo) { deprecatedAttrInfo.u2attrNameIndex = remapCpIndex(deprecatedAttrInfo.u2attrNameIndex); } public void visitSyntheticAttrInfo(ClassFile classFile, SyntheticAttrInfo syntheticAttrInfo) { syntheticAttrInfo.u2attrNameIndex = remapCpIndex(syntheticAttrInfo.u2attrNameIndex); } public void visitSignatureAttrInfo(ClassFile classFile, SignatureAttrInfo signatureAttrInfo) { signatureAttrInfo.u2attrNameIndex = remapCpIndex(signatureAttrInfo.u2attrNameIndex); signatureAttrInfo.u2signatureIndex = remapCpIndex(signatureAttrInfo.u2signatureIndex); } public void visitRuntimeVisibleAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleAnnotationsAttrInfo runtimeVisibleAnnotationsAttrInfo) { runtimeVisibleAnnotationsAttrInfo.u2attrNameIndex = remapCpIndex(runtimeVisibleAnnotationsAttrInfo.u2attrNameIndex); // Remap the constant pool references of the annotations. runtimeVisibleAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeInvisibleAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleAnnotationsAttrInfo runtimeInvisibleAnnotationsAttrInfo) { runtimeInvisibleAnnotationsAttrInfo.u2attrNameIndex = remapCpIndex(runtimeInvisibleAnnotationsAttrInfo.u2attrNameIndex); // Remap the constant pool references of the annotations. runtimeInvisibleAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeVisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeVisibleParameterAnnotationsAttrInfo runtimeVisibleParameterAnnotationsAttrInfo) { runtimeVisibleParameterAnnotationsAttrInfo.u2attrNameIndex = remapCpIndex(runtimeVisibleParameterAnnotationsAttrInfo.u2attrNameIndex); // Remap the constant pool references of the annotations. runtimeVisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitRuntimeInvisibleParameterAnnotationAttrInfo(ClassFile classFile, RuntimeInvisibleParameterAnnotationsAttrInfo runtimeInvisibleParameterAnnotationsAttrInfo) { runtimeInvisibleParameterAnnotationsAttrInfo.u2attrNameIndex = remapCpIndex(runtimeInvisibleParameterAnnotationsAttrInfo.u2attrNameIndex); // Remap the constant pool references of the annotations. runtimeInvisibleParameterAnnotationsAttrInfo.annotationsAccept(classFile, this); } public void visitAnnotationDefaultAttrInfo(ClassFile classFile, AnnotationDefaultAttrInfo annotationDefaultAttrInfo) { annotationDefaultAttrInfo.u2attrNameIndex = remapCpIndex(annotationDefaultAttrInfo.u2attrNameIndex); // Remap the constant pool references of the annotations. annotationDefaultAttrInfo.defaultValueAccept(classFile, this); } // Implementations for InnerClassesInfoVisitor. public void visitInnerClassesInfo(ClassFile classFile, InnerClassesInfo innerClassesInfo) { if (innerClassesInfo.u2innerClassInfoIndex != 0) { innerClassesInfo.u2innerClassInfoIndex = remapCpIndex(innerClassesInfo.u2innerClassInfoIndex); } if (innerClassesInfo.u2outerClassInfoIndex != 0) { innerClassesInfo.u2outerClassInfoIndex = remapCpIndex(innerClassesInfo.u2outerClassInfoIndex); } if (innerClassesInfo.u2innerNameIndex != 0) { innerClassesInfo.u2innerNameIndex = remapCpIndex(innerClassesInfo.u2innerNameIndex); } } // Implementations for ExceptionInfoVisitor. public void visitExceptionInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, ExceptionInfo exceptionInfo) { if (exceptionInfo.u2catchType != 0) { exceptionInfo.u2catchType = remapCpIndex(exceptionInfo.u2catchType); } } // Implementations for LocalVariableInfoVisitor. public void visitLocalVariableInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableInfo localVariableInfo) { localVariableInfo.u2nameIndex = remapCpIndex(localVariableInfo.u2nameIndex); localVariableInfo.u2descriptorIndex = remapCpIndex(localVariableInfo.u2descriptorIndex); } // Implementations for LocalVariableTypeInfoVisitor. public void visitLocalVariableTypeInfo(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, LocalVariableTypeInfo localVariableTypeInfo) { localVariableTypeInfo.u2nameIndex = remapCpIndex(localVariableTypeInfo.u2nameIndex); localVariableTypeInfo.u2signatureIndex = remapCpIndex(localVariableTypeInfo.u2signatureIndex); } // Implementations for AnnotationVisitor. public void visitAnnotation(ClassFile classFile, Annotation annotation) { annotation.u2typeIndex = remapCpIndex(annotation.u2typeIndex); // Remap the constant pool references of the element values. annotation.elementValuesAccept(classFile, this); } // Implementations for ElementValueVisitor. public void visitConstantElementValue(ClassFile classFile, Annotation annotation, ConstantElementValue constantElementValue) { constantElementValue.u2elementName = remapCpIndex(constantElementValue.u2elementName); constantElementValue.u2constantValueIndex = remapCpIndex(constantElementValue.u2constantValueIndex); } public void visitEnumConstantElementValue(ClassFile classFile, Annotation annotation, EnumConstantElementValue enumConstantElementValue) { enumConstantElementValue.u2elementName = remapCpIndex(enumConstantElementValue.u2elementName); enumConstantElementValue.u2typeNameIndex = remapCpIndex(enumConstantElementValue.u2typeNameIndex); enumConstantElementValue.u2constantNameIndex = remapCpIndex(enumConstantElementValue.u2constantNameIndex); } public void visitClassElementValue(ClassFile classFile, Annotation annotation, ClassElementValue classElementValue) { classElementValue.u2elementName = remapCpIndex(classElementValue.u2elementName); classElementValue.u2classInfoIndex = remapCpIndex(classElementValue.u2classInfoIndex); } public void visitAnnotationElementValue(ClassFile classFile, Annotation annotation, AnnotationElementValue annotationElementValue) { annotationElementValue.u2elementName = remapCpIndex(annotationElementValue.u2elementName); // Remap the constant pool references of the annotation. annotationElementValue.annotationAccept(classFile, this); } public void visitArrayElementValue(ClassFile classFile, Annotation annotation, ArrayElementValue arrayElementValue) { arrayElementValue.u2elementName = remapCpIndex(arrayElementValue.u2elementName); // Remap the constant pool references of the element values. arrayElementValue.elementValuesAccept(classFile, annotation, this); } // Implementations for InstructionVisitor. public void visitSimpleInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, SimpleInstruction simpleInstruction) {} public void visitVariableInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, VariableInstruction variableInstruction) {} public void visitBranchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, BranchInstruction branchInstruction) {} public void visitTableSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, TableSwitchInstruction tableSwitchInstruction) {} public void visitLookUpSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, LookUpSwitchInstruction lookUpSwitchInstruction) {} public void visitCpInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, CpInstruction cpInstruction) { // Remap the instruction, and get the old and the new instruction length. int oldLength = cpInstruction.length(offset); cpInstruction.cpIndex = remapCpIndex(cpInstruction.cpIndex); cpInstruction.shrink(); int newLength = cpInstruction.length(offset); // Is the code length changing? if (newLength != oldLength || codeAttrInfoEditor.isModified()) { // We have to go through the code attribute editor. cpInstruction = new CpInstruction().copy(cpInstruction); codeAttrInfoEditor.replaceInstruction(offset, cpInstruction); } else { // We can write the instruction directly. cpInstruction.write(codeAttrInfo, offset); } } // Small utility methods. /** * Remaps all constant pool indices in the given array. */ private void remapCpIndexArray(int[] array, int length) { for (int index = 0; index < length; index++) { array[index] = remapCpIndex(array[index]); } } /** * Returns the new constant pool index of the entry at the * given index. */ private int remapCpIndex(int cpIndex) { return cpIndexMap[cpIndex]; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -