diff options
Diffstat (limited to 'src/proguard/classfile/io')
-rw-r--r-- | src/proguard/classfile/io/LibraryClassReader.java | 383 | ||||
-rw-r--r-- | src/proguard/classfile/io/ProgramClassReader.java | 1118 | ||||
-rw-r--r-- | src/proguard/classfile/io/ProgramClassWriter.java | 873 | ||||
-rw-r--r-- | src/proguard/classfile/io/RuntimeDataInput.java | 223 | ||||
-rw-r--r-- | src/proguard/classfile/io/RuntimeDataOutput.java | 224 | ||||
-rw-r--r-- | src/proguard/classfile/io/package.html | 3 |
6 files changed, 0 insertions, 2824 deletions
diff --git a/src/proguard/classfile/io/LibraryClassReader.java b/src/proguard/classfile/io/LibraryClassReader.java deleted file mode 100644 index a9a4e8e..0000000 --- a/src/proguard/classfile/io/LibraryClassReader.java +++ /dev/null @@ -1,383 +0,0 @@ -/* - * ProGuard -- shrinking, optimization, obfuscation, and preverification - * of Java bytecode. - * - * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package proguard.classfile.io; - -import proguard.classfile.*; -import proguard.classfile.constant.*; -import proguard.classfile.constant.visitor.ConstantVisitor; -import proguard.classfile.util.*; -import proguard.classfile.visitor.*; - -import java.io.DataInput; - -/** - * This ClassVisitor fills out the LibraryClass objects that it visits with data - * from the given DataInput object. - * - * @author Eric Lafortune - */ -public class LibraryClassReader -extends SimplifiedVisitor -implements ClassVisitor, - MemberVisitor, - ConstantVisitor -{ - private static final LibraryField[] EMPTY_LIBRARY_FIELDS = new LibraryField[0]; - private static final LibraryMethod[] EMPTY_LIBRARY_METHODS = new LibraryMethod[0]; - - - private final RuntimeDataInput dataInput; - private final boolean skipNonPublicClasses; - private final boolean skipNonPublicClassMembers; - - // A global array that acts as a parameter for the visitor methods. - private Constant[] constantPool; - - - /** - * Creates a new ProgramClassReader for reading from the given DataInput. - */ - public LibraryClassReader(DataInput dataInput, - boolean skipNonPublicClasses, - boolean skipNonPublicClassMembers) - { - this.dataInput = new RuntimeDataInput(dataInput); - this.skipNonPublicClasses = skipNonPublicClasses; - this.skipNonPublicClassMembers = skipNonPublicClassMembers; - } - - - // Implementations for ClassVisitor. - - public void visitProgramClass(ProgramClass libraryClass) - { - } - - - public void visitLibraryClass(LibraryClass libraryClass) - { - // Read and check the magic number. - int u4magic = dataInput.readInt(); - - ClassUtil.checkMagicNumber(u4magic); - - // Read and check the version numbers. - int u2minorVersion = dataInput.readUnsignedShort(); - int u2majorVersion = dataInput.readUnsignedShort(); - - int u4version = ClassUtil.internalClassVersion(u2majorVersion, - u2minorVersion); - - ClassUtil.checkVersionNumbers(u4version); - - // Read the constant pool. Note that the first entry is not used. - int u2constantPoolCount = dataInput.readUnsignedShort(); - - // Create the constant pool array. - constantPool = new Constant[u2constantPoolCount]; - - for (int index = 1; index < u2constantPoolCount; index++) - { - Constant constant = createConstant(); - constant.accept(libraryClass, this); - - int tag = constant.getTag(); - if (tag == ClassConstants.CONSTANT_Class || - tag == ClassConstants.CONSTANT_Utf8) - { - constantPool[index] = constant; - } - - // Long constants and double constants take up two entries in the - // constant pool. - if (tag == ClassConstants.CONSTANT_Long || - tag == ClassConstants.CONSTANT_Double) - { - index++; - } - } - - // Read the general class information. - libraryClass.u2accessFlags = dataInput.readUnsignedShort(); - - // We may stop parsing this library class if it's not public anyway. - // E.g. only about 60% of all rt.jar classes need to be parsed. - if (skipNonPublicClasses && - AccessUtil.accessLevel(libraryClass.getAccessFlags()) < AccessUtil.PUBLIC) - { - return; - } - - // Read the class and super class indices. - int u2thisClass = dataInput.readUnsignedShort(); - int u2superClass = dataInput.readUnsignedShort(); - - // Store their actual names. - libraryClass.thisClassName = getClassName(u2thisClass); - libraryClass.superClassName = (u2superClass == 0) ? null : - getClassName(u2superClass); - - // Read the interfaces - int u2interfacesCount = dataInput.readUnsignedShort(); - - libraryClass.interfaceNames = new String[u2interfacesCount]; - for (int index = 0; index < u2interfacesCount; index++) - { - // Store the actual interface name. - int u2interface = dataInput.readUnsignedShort(); - libraryClass.interfaceNames[index] = getClassName(u2interface); - } - - // Read the fields. - int u2fieldsCount = dataInput.readUnsignedShort(); - - // Create the fields array. - LibraryField[] reusableFields = new LibraryField[u2fieldsCount]; - - int visibleFieldsCount = 0; - for (int index = 0; index < u2fieldsCount; index++) - { - LibraryField field = new LibraryField(); - this.visitLibraryMember(libraryClass, field); - - // Only store fields that are visible. - if (AccessUtil.accessLevel(field.getAccessFlags()) >= - (skipNonPublicClassMembers ? AccessUtil.PROTECTED : - AccessUtil.PACKAGE_VISIBLE)) - { - reusableFields[visibleFieldsCount++] = field; - } - } - - // Copy the visible fields (if any) into a fields array of the right size. - if (visibleFieldsCount == 0) - { - libraryClass.fields = EMPTY_LIBRARY_FIELDS; - } - else - { - libraryClass.fields = new LibraryField[visibleFieldsCount]; - System.arraycopy(reusableFields, 0, libraryClass.fields, 0, visibleFieldsCount); - } - - // Read the methods. - int u2methodsCount = dataInput.readUnsignedShort(); - - // Create the methods array. - LibraryMethod[] reusableMethods = new LibraryMethod[u2methodsCount]; - - int visibleMethodsCount = 0; - for (int index = 0; index < u2methodsCount; index++) - { - LibraryMethod method = new LibraryMethod(); - this.visitLibraryMember(libraryClass, method); - - // Only store methods that are visible. - if (AccessUtil.accessLevel(method.getAccessFlags()) >= - (skipNonPublicClassMembers ? AccessUtil.PROTECTED : - AccessUtil.PACKAGE_VISIBLE)) - { - reusableMethods[visibleMethodsCount++] = method; - } - } - - // Copy the visible methods (if any) into a methods array of the right size. - if (visibleMethodsCount == 0) - { - libraryClass.methods = EMPTY_LIBRARY_METHODS; - } - else - { - libraryClass.methods = new LibraryMethod[visibleMethodsCount]; - System.arraycopy(reusableMethods, 0, libraryClass.methods, 0, visibleMethodsCount); - } - - // Skip the class attributes. - skipAttributes(); - } - - - // Implementations for MemberVisitor. - - public void visitProgramMember(ProgramClass libraryClass, ProgramMember libraryMember) - { - } - - - public void visitLibraryMember(LibraryClass libraryClass, LibraryMember libraryMember) - { - // Read the general field information. - libraryMember.u2accessFlags = dataInput.readUnsignedShort(); - libraryMember.name = getString(dataInput.readUnsignedShort()); - libraryMember.descriptor = getString(dataInput.readUnsignedShort()); - - // Skip the field attributes. - skipAttributes(); - } - - - // Implementations for ConstantVisitor. - - public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant) - { - dataInput.skipBytes(4); - } - - - public void visitLongConstant(Clazz clazz, LongConstant longConstant) - { - dataInput.skipBytes(8); - } - - - public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant) - { - dataInput.skipBytes(4); - } - - - public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant) - { - dataInput.skipBytes(8); - } - - - public void visitStringConstant(Clazz clazz, StringConstant stringConstant) - { - dataInput.skipBytes(2); - } - - - public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) - { - int u2length = dataInput.readUnsignedShort(); - - // Read the UTF-8 bytes. - byte[] bytes = new byte[u2length]; - dataInput.readFully(bytes); - utf8Constant.setBytes(bytes); - } - - - public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant) - { - dataInput.skipBytes(4); - } - - - public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant) - { - dataInput.skipBytes(3); - } - - - public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant) - { - dataInput.skipBytes(4); - } - - - public void visitClassConstant(Clazz clazz, ClassConstant classConstant) - { - classConstant.u2nameIndex = dataInput.readUnsignedShort(); - } - - - public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant) - { - dataInput.skipBytes(2); - } - - - public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant) - { - dataInput.skipBytes(4); - } - - - // Small utility methods. - - /** - * Returns the class name of the ClassConstant at the specified index in the - * reusable constant pool. - */ - private String getClassName(int constantIndex) - { - ClassConstant classEntry = (ClassConstant)constantPool[constantIndex]; - - return getString(classEntry.u2nameIndex); - } - - - /** - * Returns the string of the Utf8Constant at the specified index in the - * reusable constant pool. - */ - private String getString(int constantIndex) - { - return ((Utf8Constant)constantPool[constantIndex]).getString(); - } - - - private Constant createConstant() - { - int u1tag = dataInput.readUnsignedByte(); - - switch (u1tag) - { - case ClassConstants.CONSTANT_Integer: return new IntegerConstant(); - case ClassConstants.CONSTANT_Float: return new FloatConstant(); - case ClassConstants.CONSTANT_Long: return new LongConstant(); - case ClassConstants.CONSTANT_Double: return new DoubleConstant(); - case ClassConstants.CONSTANT_String: return new StringConstant(); - case ClassConstants.CONSTANT_Utf8: return new Utf8Constant(); - case ClassConstants.CONSTANT_InvokeDynamic: return new InvokeDynamicConstant(); - case ClassConstants.CONSTANT_MethodHandle: return new MethodHandleConstant(); - case ClassConstants.CONSTANT_Fieldref: return new FieldrefConstant(); - case ClassConstants.CONSTANT_Methodref: return new MethodrefConstant(); - case ClassConstants.CONSTANT_InterfaceMethodref: return new InterfaceMethodrefConstant(); - case ClassConstants.CONSTANT_Class: return new ClassConstant(); - case ClassConstants.CONSTANT_MethodType: return new MethodTypeConstant(); - case ClassConstants.CONSTANT_NameAndType: return new NameAndTypeConstant(); - - default: throw new RuntimeException("Unknown constant type ["+u1tag+"] in constant pool"); - } - } - - - private void skipAttributes() - { - int u2attributesCount = dataInput.readUnsignedShort(); - - for (int index = 0; index < u2attributesCount; index++) - { - skipAttribute(); - } - } - - - private void skipAttribute() - { - dataInput.skipBytes(2); - int u4attributeLength = dataInput.readInt(); - dataInput.skipBytes(u4attributeLength); - } -} diff --git a/src/proguard/classfile/io/ProgramClassReader.java b/src/proguard/classfile/io/ProgramClassReader.java deleted file mode 100644 index d6a8c36..0000000 --- a/src/proguard/classfile/io/ProgramClassReader.java +++ /dev/null @@ -1,1118 +0,0 @@ -/* - * ProGuard -- shrinking, optimization, obfuscation, and preverification - * of Java bytecode. - * - * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package proguard.classfile.io; - -import proguard.classfile.*; -import proguard.classfile.attribute.*; -import proguard.classfile.attribute.annotation.*; -import proguard.classfile.attribute.annotation.target.*; -import proguard.classfile.attribute.annotation.target.visitor.*; -import proguard.classfile.attribute.annotation.visitor.*; -import proguard.classfile.attribute.preverification.*; -import proguard.classfile.attribute.preverification.visitor.*; -import proguard.classfile.attribute.visitor.*; -import proguard.classfile.constant.*; -import proguard.classfile.constant.visitor.ConstantVisitor; -import proguard.classfile.util.*; -import proguard.classfile.visitor.*; - -import java.io.DataInput; - -/** - * This ClassVisitor fills out the ProgramClass objects that it visits with data - * from the given DataInput object. - * - * @author Eric Lafortune - */ -public class ProgramClassReader -extends SimplifiedVisitor -implements ClassVisitor, - MemberVisitor, - ConstantVisitor, - AttributeVisitor, - BootstrapMethodInfoVisitor, - InnerClassesInfoVisitor, - ExceptionInfoVisitor, - StackMapFrameVisitor, - VerificationTypeVisitor, - LineNumberInfoVisitor, - ParameterInfoVisitor, - LocalVariableInfoVisitor, - LocalVariableTypeInfoVisitor, - AnnotationVisitor, - TypeAnnotationVisitor, - TargetInfoVisitor, - TypePathInfoVisitor, - LocalVariableTargetElementVisitor, - ElementValueVisitor -{ - private final RuntimeDataInput dataInput; - - - /** - * Creates a new ProgramClassReader for reading from the given DataInput. - */ - public ProgramClassReader(DataInput dataInput) - { - this.dataInput = new RuntimeDataInput(dataInput); - } - - - // Implementations for ClassVisitor. - - public void visitProgramClass(ProgramClass programClass) - { - // Read and check the magic number. - programClass.u4magic = dataInput.readInt(); - - ClassUtil.checkMagicNumber(programClass.u4magic); - - // Read and check the version numbers. - int u2minorVersion = dataInput.readUnsignedShort(); - int u2majorVersion = dataInput.readUnsignedShort(); - - programClass.u4version = ClassUtil.internalClassVersion(u2majorVersion, - u2minorVersion); - - ClassUtil.checkVersionNumbers(programClass.u4version); - - // Read the constant pool. Note that the first entry is not used. - programClass.u2constantPoolCount = dataInput.readUnsignedShort(); - - programClass.constantPool = new Constant[programClass.u2constantPoolCount]; - for (int index = 1; index < programClass.u2constantPoolCount; index++) - { - Constant constant = createConstant(); - constant.accept(programClass, this); - programClass.constantPool[index] = constant; - - // Long constants and double constants take up two entries in the - // constant pool. - int tag = constant.getTag(); - if (tag == ClassConstants.CONSTANT_Long || - tag == ClassConstants.CONSTANT_Double) - { - programClass.constantPool[++index] = null; - } - } - - // Read the general class information. - programClass.u2accessFlags = dataInput.readUnsignedShort(); - programClass.u2thisClass = dataInput.readUnsignedShort(); - programClass.u2superClass = dataInput.readUnsignedShort(); - - // Read the interfaces. - programClass.u2interfacesCount = dataInput.readUnsignedShort(); - - programClass.u2interfaces = new int[programClass.u2interfacesCount]; - for (int index = 0; index < programClass.u2interfacesCount; index++) - { - programClass.u2interfaces[index] = dataInput.readUnsignedShort(); - } - - // Read the fields. - programClass.u2fieldsCount = dataInput.readUnsignedShort(); - - programClass.fields = new ProgramField[programClass.u2fieldsCount]; - for (int index = 0; index < programClass.u2fieldsCount; index++) - { - ProgramField programField = new ProgramField(); - this.visitProgramField(programClass, programField); - programClass.fields[index] = programField; - } - - // Read the methods. - programClass.u2methodsCount = dataInput.readUnsignedShort(); - - programClass.methods = new ProgramMethod[programClass.u2methodsCount]; - for (int index = 0; index < programClass.u2methodsCount; index++) - { - ProgramMethod programMethod = new ProgramMethod(); - this.visitProgramMethod(programClass, programMethod); - programClass.methods[index] = programMethod; - } - - // Read the class attributes. - programClass.u2attributesCount = dataInput.readUnsignedShort(); - - programClass.attributes = new Attribute[programClass.u2attributesCount]; - for (int index = 0; index < programClass.u2attributesCount; index++) - { - Attribute attribute = createAttribute(programClass); - attribute.accept(programClass, this); - programClass.attributes[index] = attribute; - } - } - - - public void visitLibraryClass(LibraryClass libraryClass) - { - } - - - // Implementations for MemberVisitor. - - public void visitProgramField(ProgramClass programClass, ProgramField programField) - { - // Read the general field information. - programField.u2accessFlags = dataInput.readUnsignedShort(); - programField.u2nameIndex = dataInput.readUnsignedShort(); - programField.u2descriptorIndex = dataInput.readUnsignedShort(); - - // Read the field attributes. - programField.u2attributesCount = dataInput.readUnsignedShort(); - - programField.attributes = new Attribute[programField.u2attributesCount]; - for (int index = 0; index < programField.u2attributesCount; index++) - { - Attribute attribute = createAttribute(programClass); - attribute.accept(programClass, programField, this); - programField.attributes[index] = attribute; - } - } - - - public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) - { - // Read the general method information. - programMethod.u2accessFlags = dataInput.readUnsignedShort(); - programMethod.u2nameIndex = dataInput.readUnsignedShort(); - programMethod.u2descriptorIndex = dataInput.readUnsignedShort(); - - // Read the method attributes. - programMethod.u2attributesCount = dataInput.readUnsignedShort(); - - programMethod.attributes = new Attribute[programMethod.u2attributesCount]; - for (int index = 0; index < programMethod.u2attributesCount; index++) - { - Attribute attribute = createAttribute(programClass); - attribute.accept(programClass, programMethod, this); - programMethod.attributes[index] = attribute; - } - } - - - public void visitLibraryMember(LibraryClass libraryClass, LibraryMember libraryMember) - { - } - - - // Implementations for ConstantVisitor. - - public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant) - { - integerConstant.u4value = dataInput.readInt(); - } - - - public void visitLongConstant(Clazz clazz, LongConstant longConstant) - { - longConstant.u8value = dataInput.readLong(); - } - - - public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant) - { - floatConstant.f4value = dataInput.readFloat(); - } - - - public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant) - { - doubleConstant.f8value = dataInput.readDouble(); - } - - - public void visitStringConstant(Clazz clazz, StringConstant stringConstant) - { - stringConstant.u2stringIndex = dataInput.readUnsignedShort(); - } - - - public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) - { - int u2length = dataInput.readUnsignedShort(); - - // Read the UTF-8 bytes. - byte[] bytes = new byte[u2length]; - dataInput.readFully(bytes); - utf8Constant.setBytes(bytes); - } - - - public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant) - { - invokeDynamicConstant.u2bootstrapMethodAttributeIndex = dataInput.readUnsignedShort(); - invokeDynamicConstant.u2nameAndTypeIndex = dataInput.readUnsignedShort(); - } - - - public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant) - { - methodHandleConstant.u1referenceKind = dataInput.readUnsignedByte(); - methodHandleConstant.u2referenceIndex = dataInput.readUnsignedShort(); - } - - - public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant) - { - refConstant.u2classIndex = dataInput.readUnsignedShort(); - refConstant.u2nameAndTypeIndex = dataInput.readUnsignedShort(); - } - - - public void visitClassConstant(Clazz clazz, ClassConstant classConstant) - { - classConstant.u2nameIndex = dataInput.readUnsignedShort(); - } - - - public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant) - { - methodTypeConstant.u2descriptorIndex = dataInput.readUnsignedShort(); - } - - - public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant) - { - nameAndTypeConstant.u2nameIndex = dataInput.readUnsignedShort(); - nameAndTypeConstant.u2descriptorIndex = dataInput.readUnsignedShort(); - } - - - // Implementations for AttributeVisitor. - - public void visitUnknownAttribute(Clazz clazz, UnknownAttribute unknownAttribute) - { - // Read the unknown information. - byte[] info = new byte[unknownAttribute.u4attributeLength]; - dataInput.readFully(info); - unknownAttribute.info = info; - } - - - public void visitBootstrapMethodsAttribute(Clazz clazz, BootstrapMethodsAttribute bootstrapMethodsAttribute) - { - // Read the bootstrap methods. - bootstrapMethodsAttribute.u2bootstrapMethodsCount = dataInput.readUnsignedShort(); - - bootstrapMethodsAttribute.bootstrapMethods = new BootstrapMethodInfo[bootstrapMethodsAttribute.u2bootstrapMethodsCount]; - for (int index = 0; index < bootstrapMethodsAttribute.u2bootstrapMethodsCount; index++) - { - BootstrapMethodInfo bootstrapMethodInfo = new BootstrapMethodInfo(); - visitBootstrapMethodInfo(clazz, bootstrapMethodInfo); - bootstrapMethodsAttribute.bootstrapMethods[index] = bootstrapMethodInfo; - } - } - - - public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute) - { - sourceFileAttribute.u2sourceFileIndex = dataInput.readUnsignedShort(); - } - - - public void visitSourceDirAttribute(Clazz clazz, SourceDirAttribute sourceDirAttribute) - { - sourceDirAttribute.u2sourceDirIndex = dataInput.readUnsignedShort(); - } - - - public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute) - { - // Read the inner classes. - innerClassesAttribute.u2classesCount = dataInput.readUnsignedShort(); - - innerClassesAttribute.classes = new InnerClassesInfo[innerClassesAttribute.u2classesCount]; - for (int index = 0; index < innerClassesAttribute.u2classesCount; index++) - { - InnerClassesInfo innerClassesInfo = new InnerClassesInfo(); - visitInnerClassesInfo(clazz, innerClassesInfo); - innerClassesAttribute.classes[index] = innerClassesInfo; - } - } - - - public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute) - { - enclosingMethodAttribute.u2classIndex = dataInput.readUnsignedShort(); - enclosingMethodAttribute.u2nameAndTypeIndex = dataInput.readUnsignedShort(); - } - - - public void visitDeprecatedAttribute(Clazz clazz, DeprecatedAttribute deprecatedAttribute) - { - // This attribute does not contain any additional information. - } - - - public void visitSyntheticAttribute(Clazz clazz, SyntheticAttribute syntheticAttribute) - { - // This attribute does not contain any additional information. - } - - - public void visitSignatureAttribute(Clazz clazz, SignatureAttribute signatureAttribute) - { - signatureAttribute.u2signatureIndex = dataInput.readUnsignedShort(); - } - - - public void visitConstantValueAttribute(Clazz clazz, Field field, ConstantValueAttribute constantValueAttribute) - { - constantValueAttribute.u2constantValueIndex = dataInput.readUnsignedShort(); - } - - - public void visitMethodParametersAttribute(Clazz clazz, Method method, MethodParametersAttribute methodParametersAttribute) - { - // Read the parameter information. - methodParametersAttribute.u1parametersCount = dataInput.readUnsignedByte(); - - methodParametersAttribute.parameters = new ParameterInfo[methodParametersAttribute.u1parametersCount]; - for (int index = 0; index < methodParametersAttribute.u1parametersCount; index++) - { - ParameterInfo parameterInfo = new ParameterInfo(); - visitParameterInfo(clazz, method, index, parameterInfo); - methodParametersAttribute.parameters[index] = parameterInfo; - } - } - - - public void visitExceptionsAttribute(Clazz clazz, Method method, ExceptionsAttribute exceptionsAttribute) - { - // Read the exceptions. - exceptionsAttribute.u2exceptionIndexTableLength = dataInput.readUnsignedShort(); - - exceptionsAttribute.u2exceptionIndexTable = new int[exceptionsAttribute.u2exceptionIndexTableLength]; - for (int index = 0; index < exceptionsAttribute.u2exceptionIndexTableLength; index++) - { - exceptionsAttribute.u2exceptionIndexTable[index] = dataInput.readUnsignedShort(); - } - } - - - public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) - { - // Read the stack size and local variable frame size. - codeAttribute.u2maxStack = dataInput.readUnsignedShort(); - codeAttribute.u2maxLocals = dataInput.readUnsignedShort(); - - // Read the byte code. - codeAttribute.u4codeLength = dataInput.readInt(); - - byte[] code = new byte[codeAttribute.u4codeLength]; - dataInput.readFully(code); - codeAttribute.code = code; - - // Read the exceptions. - codeAttribute.u2exceptionTableLength = dataInput.readUnsignedShort(); - - codeAttribute.exceptionTable = new ExceptionInfo[codeAttribute.u2exceptionTableLength]; - for (int index = 0; index < codeAttribute.u2exceptionTableLength; index++) - { - ExceptionInfo exceptionInfo = new ExceptionInfo(); - visitExceptionInfo(clazz, method, codeAttribute, exceptionInfo); - codeAttribute.exceptionTable[index] = exceptionInfo; - } - - // Read the code attributes. - codeAttribute.u2attributesCount = dataInput.readUnsignedShort(); - - codeAttribute.attributes = new Attribute[codeAttribute.u2attributesCount]; - for (int index = 0; index < codeAttribute.u2attributesCount; index++) - { - Attribute attribute = createAttribute(clazz); - attribute.accept(clazz, method, codeAttribute, this); - codeAttribute.attributes[index] = attribute; - } - } - - - public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute) - { - // Read the stack map frames (only full frames, without tag). - stackMapAttribute.u2stackMapFramesCount = dataInput.readUnsignedShort(); - - stackMapAttribute.stackMapFrames = new FullFrame[stackMapAttribute.u2stackMapFramesCount]; - for (int index = 0; index < stackMapAttribute.u2stackMapFramesCount; index++) - { - FullFrame stackMapFrame = new FullFrame(); - visitFullFrame(clazz, method, codeAttribute, index, stackMapFrame); - stackMapAttribute.stackMapFrames[index] = stackMapFrame; - } - } - - - public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute) - { - // Read the stack map frames. - stackMapTableAttribute.u2stackMapFramesCount = dataInput.readUnsignedShort(); - - stackMapTableAttribute.stackMapFrames = new StackMapFrame[stackMapTableAttribute.u2stackMapFramesCount]; - for (int index = 0; index < stackMapTableAttribute.u2stackMapFramesCount; index++) - { - StackMapFrame stackMapFrame = createStackMapFrame(); - stackMapFrame.accept(clazz, method, codeAttribute, 0, this); - stackMapTableAttribute.stackMapFrames[index] = stackMapFrame; - } - } - - - public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute) - { - // Read the line numbers. - lineNumberTableAttribute.u2lineNumberTableLength = dataInput.readUnsignedShort(); - - lineNumberTableAttribute.lineNumberTable = new LineNumberInfo[lineNumberTableAttribute.u2lineNumberTableLength]; - for (int index = 0; index < lineNumberTableAttribute.u2lineNumberTableLength; index++) - { - LineNumberInfo lineNumberInfo = new LineNumberInfo(); - visitLineNumberInfo(clazz, method, codeAttribute, lineNumberInfo); - lineNumberTableAttribute.lineNumberTable[index] = lineNumberInfo; - } - } - - - public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute) - { - // Read the local variables. - localVariableTableAttribute.u2localVariableTableLength = dataInput.readUnsignedShort(); - - localVariableTableAttribute.localVariableTable = new LocalVariableInfo[localVariableTableAttribute.u2localVariableTableLength]; - for (int index = 0; index < localVariableTableAttribute.u2localVariableTableLength; index++) - { - LocalVariableInfo localVariableInfo = new LocalVariableInfo(); - visitLocalVariableInfo(clazz, method, codeAttribute, localVariableInfo); - localVariableTableAttribute.localVariableTable[index] = localVariableInfo; - } - } - - - public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute) - { - // Read the local variable types. - localVariableTypeTableAttribute.u2localVariableTypeTableLength = dataInput.readUnsignedShort(); - - localVariableTypeTableAttribute.localVariableTypeTable = new LocalVariableTypeInfo[localVariableTypeTableAttribute.u2localVariableTypeTableLength]; - for (int index = 0; index < localVariableTypeTableAttribute.u2localVariableTypeTableLength; index++) - { - LocalVariableTypeInfo localVariableTypeInfo = new LocalVariableTypeInfo(); - visitLocalVariableTypeInfo(clazz, method, codeAttribute, localVariableTypeInfo); - localVariableTypeTableAttribute.localVariableTypeTable[index] = localVariableTypeInfo; - } - } - - - public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute) - { - // Read the annotations. - annotationsAttribute.u2annotationsCount = dataInput.readUnsignedShort(); - - annotationsAttribute.annotations = new Annotation[annotationsAttribute.u2annotationsCount]; - for (int index = 0; index < annotationsAttribute.u2annotationsCount; index++) - { - Annotation annotation = new Annotation(); - visitAnnotation(clazz, annotation); - annotationsAttribute.annotations[index] = annotation; - } - } - - - public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute) - { - // Read the parameter annotations. - parameterAnnotationsAttribute.u1parametersCount = dataInput.readUnsignedByte(); - - // The java compilers of JDK 1.5, JDK 1.6, and Eclipse all count the - // number of parameters of constructors of non-static inner classes - // incorrectly. Fix it right here. - int parameterStart = 0; - if (method.getName(clazz).equals(ClassConstants.METHOD_NAME_INIT)) - { - int realParametersCount = ClassUtil.internalMethodParameterCount(method.getDescriptor(clazz)); - parameterStart = realParametersCount - parameterAnnotationsAttribute.u1parametersCount; - parameterAnnotationsAttribute.u1parametersCount = realParametersCount; - } - - parameterAnnotationsAttribute.u2parameterAnnotationsCount = new int[parameterAnnotationsAttribute.u1parametersCount]; - parameterAnnotationsAttribute.parameterAnnotations = new Annotation[parameterAnnotationsAttribute.u1parametersCount][]; - - for (int parameterIndex = parameterStart; parameterIndex < parameterAnnotationsAttribute.u1parametersCount; parameterIndex++) - { - // Read the parameter annotations of the given parameter. - int u2annotationsCount = dataInput.readUnsignedShort(); - - Annotation[] annotations = new Annotation[u2annotationsCount]; - - for (int index = 0; index < u2annotationsCount; index++) - { - Annotation annotation = new Annotation(); - visitAnnotation(clazz, annotation); - annotations[index] = annotation; - } - - parameterAnnotationsAttribute.u2parameterAnnotationsCount[parameterIndex] = u2annotationsCount; - parameterAnnotationsAttribute.parameterAnnotations[parameterIndex] = annotations; - } - } - - - public void visitAnyTypeAnnotationsAttribute(Clazz clazz, TypeAnnotationsAttribute typeAnnotationsAttribute) - { - // Read the type annotations. - typeAnnotationsAttribute.u2annotationsCount = dataInput.readUnsignedShort(); - - typeAnnotationsAttribute.annotations = new TypeAnnotation[typeAnnotationsAttribute.u2annotationsCount]; - for (int index = 0; index < typeAnnotationsAttribute.u2annotationsCount; index++) - { - TypeAnnotation typeAnnotation = new TypeAnnotation(); - visitTypeAnnotation(clazz, typeAnnotation); - typeAnnotationsAttribute.annotations[index] = typeAnnotation; - } - } - - - public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute) - { - // Read the default element value. - ElementValue elementValue = createElementValue(); - elementValue.accept(clazz, null, this); - annotationDefaultAttribute.defaultValue = elementValue; - } - - - // Implementations for BootstrapMethodInfoVisitor. - - public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo) - { - bootstrapMethodInfo.u2methodHandleIndex = dataInput.readUnsignedShort(); - - // Read the bootstrap method arguments. - bootstrapMethodInfo.u2methodArgumentCount = dataInput.readUnsignedShort(); - bootstrapMethodInfo.u2methodArguments = new int[bootstrapMethodInfo.u2methodArgumentCount]; - for (int index = 0; index < bootstrapMethodInfo.u2methodArgumentCount; index++) - { - bootstrapMethodInfo.u2methodArguments[index] = dataInput.readUnsignedShort(); - } - } - - - // Implementations for InnerClassesInfoVisitor. - - public void visitInnerClassesInfo(Clazz clazz, InnerClassesInfo innerClassesInfo) - { - innerClassesInfo.u2innerClassIndex = dataInput.readUnsignedShort(); - innerClassesInfo.u2outerClassIndex = dataInput.readUnsignedShort(); - innerClassesInfo.u2innerNameIndex = dataInput.readUnsignedShort(); - innerClassesInfo.u2innerClassAccessFlags = dataInput.readUnsignedShort(); - } - - - // Implementations for ExceptionInfoVisitor. - - public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo) - { - exceptionInfo.u2startPC = dataInput.readUnsignedShort(); - exceptionInfo.u2endPC = dataInput.readUnsignedShort(); - exceptionInfo.u2handlerPC = dataInput.readUnsignedShort(); - exceptionInfo.u2catchType = dataInput.readUnsignedShort(); - } - - - // Implementations for StackMapFrameVisitor. - - public void visitSameZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameZeroFrame sameZeroFrame) - { - if (sameZeroFrame.getTag() == StackMapFrame.SAME_ZERO_FRAME_EXTENDED) - { - sameZeroFrame.u2offsetDelta = dataInput.readUnsignedShort(); - } - } - - - public void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame) - { - if (sameOneFrame.getTag() == StackMapFrame.SAME_ONE_FRAME_EXTENDED) - { - sameOneFrame.u2offsetDelta = dataInput.readUnsignedShort(); - } - - // Read the verification type of the stack entry. - VerificationType verificationType = createVerificationType(); - verificationType.accept(clazz, method, codeAttribute, offset, this); - sameOneFrame.stackItem = verificationType; - } - - - public void visitLessZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LessZeroFrame lessZeroFrame) - { - lessZeroFrame.u2offsetDelta = dataInput.readUnsignedShort(); - } - - - public void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame) - { - moreZeroFrame.u2offsetDelta = dataInput.readUnsignedShort(); - - // Read the verification types of the additional local variables. - moreZeroFrame.additionalVariables = new VerificationType[moreZeroFrame.additionalVariablesCount]; - for (int index = 0; index < moreZeroFrame.additionalVariablesCount; index++) - { - VerificationType verificationType = createVerificationType(); - verificationType.accept(clazz, method, codeAttribute, offset, this); - moreZeroFrame.additionalVariables[index] = verificationType; - } - } - - - public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame) - { - fullFrame.u2offsetDelta = dataInput.readUnsignedShort(); - - // Read the verification types of the local variables. - fullFrame.variablesCount = dataInput.readUnsignedShort(); - fullFrame.variables = new VerificationType[fullFrame.variablesCount]; - for (int index = 0; index < fullFrame.variablesCount; index++) - { - VerificationType verificationType = createVerificationType(); - verificationType.variablesAccept(clazz, method, codeAttribute, offset, index, this); - fullFrame.variables[index] = verificationType; - } - - // Read the verification types of the stack entries. - fullFrame.stackCount = dataInput.readUnsignedShort(); - fullFrame.stack = new VerificationType[fullFrame.stackCount]; - for (int index = 0; index < fullFrame.stackCount; index++) - { - VerificationType verificationType = createVerificationType(); - verificationType.stackAccept(clazz, method, codeAttribute, offset, index, this); - fullFrame.stack[index] = verificationType; - } - } - - - // Implementations for VerificationTypeVisitor. - - public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType) - { - // Most verification types don't contain any additional information. - } - - - public void visitObjectType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ObjectType objectType) - { - objectType.u2classIndex = dataInput.readUnsignedShort(); - } - - - public void visitUninitializedType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, UninitializedType uninitializedType) - { - uninitializedType.u2newInstructionOffset = dataInput.readUnsignedShort(); - } - - - // Implementations for LineNumberInfoVisitor. - - public void visitLineNumberInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberInfo lineNumberInfo) - { - lineNumberInfo.u2startPC = dataInput.readUnsignedShort(); - lineNumberInfo.u2lineNumber = dataInput.readUnsignedShort(); - } - - - // Implementations for ParameterInfoVisitor. - - public void visitParameterInfo(Clazz clazz, Method method, int parameterIndex, ParameterInfo parameterInfo) - { - parameterInfo.u2nameIndex = dataInput.readUnsignedShort(); - parameterInfo.u2accessFlags = dataInput.readUnsignedShort(); - } - - - // Implementations for LocalVariableInfoVisitor. - - public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo) - { - localVariableInfo.u2startPC = dataInput.readUnsignedShort(); - localVariableInfo.u2length = dataInput.readUnsignedShort(); - localVariableInfo.u2nameIndex = dataInput.readUnsignedShort(); - localVariableInfo.u2descriptorIndex = dataInput.readUnsignedShort(); - localVariableInfo.u2index = dataInput.readUnsignedShort(); - } - - - // Implementations for LocalVariableTypeInfoVisitor. - - public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo) - { - localVariableTypeInfo.u2startPC = dataInput.readUnsignedShort(); - localVariableTypeInfo.u2length = dataInput.readUnsignedShort(); - localVariableTypeInfo.u2nameIndex = dataInput.readUnsignedShort(); - localVariableTypeInfo.u2signatureIndex = dataInput.readUnsignedShort(); - localVariableTypeInfo.u2index = dataInput.readUnsignedShort(); - } - - - // Implementations for AnnotationVisitor. - - public void visitAnnotation(Clazz clazz, Annotation annotation) - { - // Read the annotation type. - annotation.u2typeIndex = dataInput.readUnsignedShort(); - - // Read the element value pairs. - annotation.u2elementValuesCount = dataInput.readUnsignedShort(); - - annotation.elementValues = new ElementValue[annotation.u2elementValuesCount]; - for (int index = 0; index < annotation.u2elementValuesCount; index++) - { - int u2elementNameIndex = dataInput.readUnsignedShort(); - ElementValue elementValue = createElementValue(); - elementValue.u2elementNameIndex = u2elementNameIndex; - elementValue.accept(clazz, annotation, this); - annotation.elementValues[index] = elementValue; - } - } - - - // Implementations for TypeAnnotationVisitor. - - public void visitTypeAnnotation(Clazz clazz, TypeAnnotation typeAnnotation) - { - // Read the target info. - TargetInfo targetInfo = createTargetInfo(); - targetInfo.accept(clazz, typeAnnotation, this); - typeAnnotation.targetInfo = targetInfo; - - // Read the type path. - int u1pathLength = dataInput.readUnsignedByte(); - - typeAnnotation.typePath = new TypePathInfo[u1pathLength]; - for (int index = 0; index < u1pathLength; index++) - { - TypePathInfo typePathInfo = new TypePathInfo(); - visitTypePathInfo(clazz, typeAnnotation, typePathInfo); - typeAnnotation.typePath[index] = typePathInfo; - } - - // Read the actual annotation. - visitAnnotation(clazz, typeAnnotation); - } - - - // Implementations for TargetInfoVisitor. - - public void visitTypeParameterTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypeParameterTargetInfo typeParameterTargetInfo) - { - typeParameterTargetInfo.u1typeParameterIndex = dataInput.readUnsignedByte(); - } - - - public void visitSuperTypeTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, SuperTypeTargetInfo superTypeTargetInfo) - { - superTypeTargetInfo.u2superTypeIndex = dataInput.readUnsignedShort(); - } - - - public void visitTypeParameterBoundTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypeParameterBoundTargetInfo typeParameterBoundTargetInfo) - { - typeParameterBoundTargetInfo.u1typeParameterIndex = dataInput.readUnsignedByte(); - typeParameterBoundTargetInfo.u1boundIndex = dataInput.readUnsignedByte(); - } - - - public void visitEmptyTargetInfo(Clazz clazz, Member member, TypeAnnotation typeAnnotation, EmptyTargetInfo emptyTargetInfo) - { - } - - - public void visitFormalParameterTargetInfo(Clazz clazz, Method method, TypeAnnotation typeAnnotation, FormalParameterTargetInfo formalParameterTargetInfo) - { - formalParameterTargetInfo.u1formalParameterIndex = dataInput.readUnsignedByte(); - } - - - public void visitThrowsTargetInfo(Clazz clazz, Method method, TypeAnnotation typeAnnotation, ThrowsTargetInfo throwsTargetInfo) - { - throwsTargetInfo.u2throwsTypeIndex = dataInput.readUnsignedShort(); - } - - - public void visitLocalVariableTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo) - { - // Read the local variable target elements. - localVariableTargetInfo.u2tableLength = dataInput.readUnsignedShort(); - - localVariableTargetInfo.table = new LocalVariableTargetElement[localVariableTargetInfo.u2tableLength]; - for (int index = 0; index < localVariableTargetInfo.u2tableLength; index++) - { - LocalVariableTargetElement element = new LocalVariableTargetElement(); - visitLocalVariableTargetElement(clazz, method, codeAttribute, typeAnnotation, localVariableTargetInfo, element); - localVariableTargetInfo.table[index] = element; - } - } - - - public void visitCatchTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, CatchTargetInfo catchTargetInfo) - { - catchTargetInfo.u2exceptionTableIndex = dataInput.readUnsignedShort(); - } - - - public void visitOffsetTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, OffsetTargetInfo offsetTargetInfo) - { - offsetTargetInfo.u2offset = dataInput.readUnsignedShort(); - } - - - public void visitTypeArgumentTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, TypeArgumentTargetInfo typeArgumentTargetInfo) - { - typeArgumentTargetInfo.u2offset = dataInput.readUnsignedShort(); - typeArgumentTargetInfo.u1typeArgumentIndex = dataInput.readUnsignedByte(); - } - - - // Implementations for TypePathInfoVisitor. - - public void visitTypePathInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypePathInfo typePathInfo) - { - typePathInfo.u1typePathKind = dataInput.readUnsignedByte(); - typePathInfo.u1typeArgumentIndex = dataInput.readUnsignedByte(); - } - - - // Implementations for LocalVariableTargetElementVisitor. - - public void visitLocalVariableTargetElement(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo, LocalVariableTargetElement localVariableTargetElement) - { - localVariableTargetElement.u2startPC = dataInput.readShort(); - localVariableTargetElement.u2length = dataInput.readShort(); - localVariableTargetElement.u2index = dataInput.readShort(); - } - - - // Implementations for ElementValueVisitor. - - public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue) - { - constantElementValue.u2constantValueIndex = dataInput.readUnsignedShort(); - } - - - public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue) - { - enumConstantElementValue.u2typeNameIndex = dataInput.readUnsignedShort(); - enumConstantElementValue.u2constantNameIndex = dataInput.readUnsignedShort(); - } - - - public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue) - { - classElementValue.u2classInfoIndex = dataInput.readUnsignedShort(); - } - - - public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue) - { - // Read the annotation. - Annotation annotationValue = new Annotation(); - visitAnnotation(clazz, annotationValue); - annotationElementValue.annotationValue = annotationValue; - } - - - public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue) - { - // Read the element values. - arrayElementValue.u2elementValuesCount = dataInput.readUnsignedShort(); - - arrayElementValue.elementValues = new ElementValue[arrayElementValue.u2elementValuesCount]; - for (int index = 0; index < arrayElementValue.u2elementValuesCount; index++) - { - ElementValue elementValue = createElementValue(); - elementValue.accept(clazz, annotation, this); - arrayElementValue.elementValues[index] = elementValue; - } - } - - - // Small utility methods. - - private Constant createConstant() - { - int u1tag = dataInput.readUnsignedByte(); - - switch (u1tag) - { - case ClassConstants.CONSTANT_Integer: return new IntegerConstant(); - case ClassConstants.CONSTANT_Float: return new FloatConstant(); - case ClassConstants.CONSTANT_Long: return new LongConstant(); - case ClassConstants.CONSTANT_Double: return new DoubleConstant(); - case ClassConstants.CONSTANT_String: return new StringConstant(); - case ClassConstants.CONSTANT_Utf8: return new Utf8Constant(); - case ClassConstants.CONSTANT_InvokeDynamic: return new InvokeDynamicConstant(); - case ClassConstants.CONSTANT_MethodHandle: return new MethodHandleConstant(); - case ClassConstants.CONSTANT_Fieldref: return new FieldrefConstant(); - case ClassConstants.CONSTANT_Methodref: return new MethodrefConstant(); - case ClassConstants.CONSTANT_InterfaceMethodref: return new InterfaceMethodrefConstant(); - case ClassConstants.CONSTANT_Class: return new ClassConstant(); - case ClassConstants.CONSTANT_MethodType: return new MethodTypeConstant(); - case ClassConstants.CONSTANT_NameAndType: return new NameAndTypeConstant(); - - default: throw new RuntimeException("Unknown constant type ["+u1tag+"] in constant pool"); - } - } - - - private Attribute createAttribute(Clazz clazz) - { - int u2attributeNameIndex = dataInput.readUnsignedShort(); - int u4attributeLength = dataInput.readInt(); - String attributeName = clazz.getString(u2attributeNameIndex); - - Attribute attribute = - attributeName.equals(ClassConstants.ATTR_BootstrapMethods) ? (Attribute)new BootstrapMethodsAttribute(): - attributeName.equals(ClassConstants.ATTR_SourceFile) ? (Attribute)new SourceFileAttribute(): - attributeName.equals(ClassConstants.ATTR_SourceDir) ? (Attribute)new SourceDirAttribute(): - attributeName.equals(ClassConstants.ATTR_InnerClasses) ? (Attribute)new InnerClassesAttribute(): - attributeName.equals(ClassConstants.ATTR_EnclosingMethod) ? (Attribute)new EnclosingMethodAttribute(): - attributeName.equals(ClassConstants.ATTR_Deprecated) ? (Attribute)new DeprecatedAttribute(): - attributeName.equals(ClassConstants.ATTR_Synthetic) ? (Attribute)new SyntheticAttribute(): - attributeName.equals(ClassConstants.ATTR_Signature) ? (Attribute)new SignatureAttribute(): - attributeName.equals(ClassConstants.ATTR_ConstantValue) ? (Attribute)new ConstantValueAttribute(): - attributeName.equals(ClassConstants.ATTR_MethodParameters) ? (Attribute)new MethodParametersAttribute(): - attributeName.equals(ClassConstants.ATTR_Exceptions) ? (Attribute)new ExceptionsAttribute(): - attributeName.equals(ClassConstants.ATTR_Code) ? (Attribute)new CodeAttribute(): - attributeName.equals(ClassConstants.ATTR_StackMap) ? (Attribute)new StackMapAttribute(): - attributeName.equals(ClassConstants.ATTR_StackMapTable) ? (Attribute)new StackMapTableAttribute(): - attributeName.equals(ClassConstants.ATTR_LineNumberTable) ? (Attribute)new LineNumberTableAttribute(): - attributeName.equals(ClassConstants.ATTR_LocalVariableTable) ? (Attribute)new LocalVariableTableAttribute(): - attributeName.equals(ClassConstants.ATTR_LocalVariableTypeTable) ? (Attribute)new LocalVariableTypeTableAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeVisibleAnnotations) ? (Attribute)new RuntimeVisibleAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeInvisibleAnnotations) ? (Attribute)new RuntimeInvisibleAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeVisibleParameterAnnotations) ? (Attribute)new RuntimeVisibleParameterAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeInvisibleParameterAnnotations) ? (Attribute)new RuntimeInvisibleParameterAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeVisibleTypeAnnotations) ? (Attribute)new RuntimeVisibleTypeAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_RuntimeInvisibleTypeAnnotations) ? (Attribute)new RuntimeInvisibleTypeAnnotationsAttribute(): - attributeName.equals(ClassConstants.ATTR_AnnotationDefault) ? (Attribute)new AnnotationDefaultAttribute(): - (Attribute)new UnknownAttribute(u2attributeNameIndex, u4attributeLength); - attribute.u2attributeNameIndex = u2attributeNameIndex; - - return attribute; - } - - - private StackMapFrame createStackMapFrame() - { - int u1tag = dataInput.readUnsignedByte(); - - return - u1tag < StackMapFrame.SAME_ONE_FRAME ? (StackMapFrame)new SameZeroFrame(u1tag) : - u1tag < StackMapFrame.SAME_ONE_FRAME_EXTENDED ? (StackMapFrame)new SameOneFrame(u1tag) : - u1tag < StackMapFrame.LESS_ZERO_FRAME ? (StackMapFrame)new SameOneFrame(u1tag) : - u1tag < StackMapFrame.SAME_ZERO_FRAME_EXTENDED ? (StackMapFrame)new LessZeroFrame(u1tag) : - u1tag < StackMapFrame.MORE_ZERO_FRAME ? (StackMapFrame)new SameZeroFrame(u1tag) : - u1tag < StackMapFrame.FULL_FRAME ? (StackMapFrame)new MoreZeroFrame(u1tag) : - (StackMapFrame)new FullFrame(); - } - - - private VerificationType createVerificationType() - { - int u1tag = dataInput.readUnsignedByte(); - - switch (u1tag) - { - case VerificationType.INTEGER_TYPE: return new IntegerType(); - case VerificationType.FLOAT_TYPE: return new FloatType(); - case VerificationType.LONG_TYPE: return new LongType(); - case VerificationType.DOUBLE_TYPE: return new DoubleType(); - case VerificationType.TOP_TYPE: return new TopType(); - case VerificationType.OBJECT_TYPE: return new ObjectType(); - case VerificationType.NULL_TYPE: return new NullType(); - case VerificationType.UNINITIALIZED_TYPE: return new UninitializedType(); - case VerificationType.UNINITIALIZED_THIS_TYPE: return new UninitializedThisType(); - - default: throw new RuntimeException("Unknown verification type ["+u1tag+"] in stack map frame"); - } - } - - - private TargetInfo createTargetInfo() - { - byte u1targetType = dataInput.readByte(); - - switch (u1targetType) - { - case ClassConstants.ANNOTATION_TARGET_ParameterGenericClass: - case ClassConstants.ANNOTATION_TARGET_ParameterGenericMethod: return new TypeParameterTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Extends: return new SuperTypeTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_BoundGenericClass: - case ClassConstants.ANNOTATION_TARGET_BoundGenericMethod: return new TypeParameterBoundTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Field: - case ClassConstants.ANNOTATION_TARGET_Return: - case ClassConstants.ANNOTATION_TARGET_Receiver: return new EmptyTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Parameter: return new FormalParameterTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Throws: return new ThrowsTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_LocalVariable: - case ClassConstants.ANNOTATION_TARGET_ResourceVariable: return new LocalVariableTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Catch: return new CatchTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_InstanceOf: - case ClassConstants.ANNOTATION_TARGET_New: - case ClassConstants.ANNOTATION_TARGET_MethodReferenceNew: - case ClassConstants.ANNOTATION_TARGET_MethodReference: return new OffsetTargetInfo(u1targetType); - case ClassConstants.ANNOTATION_TARGET_Cast: - case ClassConstants.ANNOTATION_TARGET_ArgumentGenericMethodNew: - case ClassConstants.ANNOTATION_TARGET_ArgumentGenericMethod: - case ClassConstants.ANNOTATION_TARGET_ArgumentGenericMethodReferenceNew: - case ClassConstants.ANNOTATION_TARGET_ArgumentGenericMethodReference: return new TypeArgumentTargetInfo(u1targetType); - - default: throw new RuntimeException("Unknown annotation target type ["+u1targetType+"]"); - } - } - - - private ElementValue createElementValue() - { - int u1tag = dataInput.readUnsignedByte(); - - switch (u1tag) - { - case ClassConstants.TYPE_BOOLEAN: - case ClassConstants.TYPE_BYTE: - case ClassConstants.TYPE_CHAR: - case ClassConstants.TYPE_SHORT: - case ClassConstants.TYPE_INT: - case ClassConstants.TYPE_FLOAT: - case ClassConstants.TYPE_LONG: - case ClassConstants.TYPE_DOUBLE: - case ClassConstants.ELEMENT_VALUE_STRING_CONSTANT: return new ConstantElementValue((char)u1tag); - - case ClassConstants.ELEMENT_VALUE_ENUM_CONSTANT: return new EnumConstantElementValue(); - case ClassConstants.ELEMENT_VALUE_CLASS: return new ClassElementValue(); - case ClassConstants.ELEMENT_VALUE_ANNOTATION: return new AnnotationElementValue(); - case ClassConstants.ELEMENT_VALUE_ARRAY: return new ArrayElementValue(); - - default: throw new IllegalArgumentException("Unknown element value tag ["+u1tag+"]"); - } - } -} diff --git a/src/proguard/classfile/io/ProgramClassWriter.java b/src/proguard/classfile/io/ProgramClassWriter.java deleted file mode 100644 index bcc3403..0000000 --- a/src/proguard/classfile/io/ProgramClassWriter.java +++ /dev/null @@ -1,873 +0,0 @@ -/* - * ProGuard -- shrinking, optimization, obfuscation, and preverification - * of Java bytecode. - * - * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package proguard.classfile.io; - -import proguard.classfile.*; -import proguard.classfile.attribute.*; -import proguard.classfile.attribute.annotation.*; -import proguard.classfile.attribute.annotation.target.*; -import proguard.classfile.attribute.annotation.target.visitor.*; -import proguard.classfile.attribute.annotation.visitor.*; -import proguard.classfile.attribute.preverification.*; -import proguard.classfile.attribute.preverification.visitor.*; -import proguard.classfile.attribute.visitor.*; -import proguard.classfile.constant.*; -import proguard.classfile.constant.visitor.ConstantVisitor; -import proguard.classfile.util.*; -import proguard.classfile.visitor.*; - -import java.io.*; - -/** - * This ClassVisitor writes out the ProgramClass objects that it visits to the - * given DataOutput object. - * - * @author Eric Lafortune - */ -public class ProgramClassWriter -extends SimplifiedVisitor -implements ClassVisitor, - MemberVisitor, - ConstantVisitor, - AttributeVisitor -{ - private RuntimeDataOutput dataOutput; - - private final ConstantBodyWriter constantBodyWriter = new ConstantBodyWriter(); - private final AttributeBodyWriter attributeBodyWriter = new AttributeBodyWriter(); - private final StackMapFrameBodyWriter stackMapFrameBodyWriter = new StackMapFrameBodyWriter(); - private final VerificationTypeBodyWriter verificationTypeBodyWriter = new VerificationTypeBodyWriter(); - private final ElementValueBodyWriter elementValueBodyWriter = new ElementValueBodyWriter(); - - - /** - * Creates a new ProgramClassWriter for writing to the given DataOutput. - */ - public ProgramClassWriter(DataOutput dataOutput) - { - this.dataOutput = new RuntimeDataOutput(dataOutput); - } - - - // Implementations for ClassVisitor. - - public void visitProgramClass(ProgramClass programClass) - { - // Write the magic number. - dataOutput.writeInt(programClass.u4magic); - - // Write the version numbers. - dataOutput.writeShort(ClassUtil.internalMinorClassVersion(programClass.u4version)); - dataOutput.writeShort(ClassUtil.internalMajorClassVersion(programClass.u4version)); - - // Write the constant pool. - dataOutput.writeShort(programClass.u2constantPoolCount); - - programClass.constantPoolEntriesAccept(this); - - // Write the general class information. - dataOutput.writeShort(programClass.u2accessFlags); - dataOutput.writeShort(programClass.u2thisClass); - dataOutput.writeShort(programClass.u2superClass); - - // Write the interfaces. - dataOutput.writeShort(programClass.u2interfacesCount); - - for (int index = 0; index < programClass.u2interfacesCount; index++) - { - dataOutput.writeShort(programClass.u2interfaces[index]); - } - - // Write the fields. - dataOutput.writeShort(programClass.u2fieldsCount); - - programClass.fieldsAccept(this); - - // Write the methods. - dataOutput.writeShort(programClass.u2methodsCount); - - programClass.methodsAccept(this); - - // Write the class attributes. - dataOutput.writeShort(programClass.u2attributesCount); - - programClass.attributesAccept(this); - } - - - public void visitLibraryClass(LibraryClass libraryClass) - { - } - - - // Implementations for MemberVisitor. - - public void visitProgramField(ProgramClass programClass, ProgramField programField) - { - // Write the general field information. - dataOutput.writeShort(programField.u2accessFlags); - dataOutput.writeShort(programField.u2nameIndex); - dataOutput.writeShort(programField.u2descriptorIndex); - - // Write the field attributes. - dataOutput.writeShort(programField.u2attributesCount); - - programField.attributesAccept(programClass, this); - } - - - public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) - { - // Write the general method information. - dataOutput.writeShort(programMethod.u2accessFlags); - dataOutput.writeShort(programMethod.u2nameIndex); - dataOutput.writeShort(programMethod.u2descriptorIndex); - - // Write the method attributes. - dataOutput.writeShort(programMethod.u2attributesCount); - - programMethod.attributesAccept(programClass, this); - } - - - public void visitLibraryMember(LibraryClass libraryClass, LibraryMember libraryMember) - { - } - - - // Implementations for ConstantVisitor. - - public void visitAnyConstant(Clazz clazz, Constant constant) - { - // Write the tag. - dataOutput.writeByte(constant.getTag()); - - // Write the actual body. - constant.accept(clazz, constantBodyWriter); - } - - - private class ConstantBodyWriter - extends SimplifiedVisitor - implements ConstantVisitor - { - // Implementations for ConstantVisitor. - - public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant) - { - dataOutput.writeInt(integerConstant.u4value); - } - - - public void visitLongConstant(Clazz clazz, LongConstant longConstant) - { - dataOutput.writeLong(longConstant.u8value); - } - - - public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant) - { - dataOutput.writeFloat(floatConstant.f4value); - } - - - public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant) - { - dataOutput.writeDouble(doubleConstant.f8value); - } - - - public void visitStringConstant(Clazz clazz, StringConstant stringConstant) - { - dataOutput.writeShort(stringConstant.u2stringIndex); - } - - - public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant) - { - byte[] bytes = utf8Constant.getBytes(); - - dataOutput.writeShort(bytes.length); - dataOutput.write(bytes); - } - - - public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant) - { - dataOutput.writeShort(invokeDynamicConstant.u2bootstrapMethodAttributeIndex); - dataOutput.writeShort(invokeDynamicConstant.u2nameAndTypeIndex); - } - - - public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant) - { - dataOutput.writeByte(methodHandleConstant.u1referenceKind); - dataOutput.writeShort(methodHandleConstant.u2referenceIndex); - } - - - public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant) - { - dataOutput.writeShort(refConstant.u2classIndex); - dataOutput.writeShort(refConstant.u2nameAndTypeIndex); - } - - - public void visitClassConstant(Clazz clazz, ClassConstant classConstant) - { - dataOutput.writeShort(classConstant.u2nameIndex); - } - - - public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant) - { - dataOutput.writeShort(methodTypeConstant.u2descriptorIndex); - } - - - public void visitNameAndTypeConstant(Clazz clazz, NameAndTypeConstant nameAndTypeConstant) - { - dataOutput.writeShort(nameAndTypeConstant.u2nameIndex); - dataOutput.writeShort(nameAndTypeConstant.u2descriptorIndex); - } - } - - - // Implementations for AttributeVisitor. - - public void visitAnyAttribute(Clazz clazz, Attribute attribute) - { - // Write the attribute name index. - dataOutput.writeShort(attribute.u2attributeNameIndex); - - // We'll write the attribute body into an array first, so we can - // automatically figure out its length. - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - - // Temporarily replace the current data output. - RuntimeDataOutput oldDataOutput = dataOutput; - dataOutput = new RuntimeDataOutput(new DataOutputStream(byteArrayOutputStream)); - - // Write the attribute body into the array. Note that the - // accept method with two dummy null arguments never throws - // an UnsupportedOperationException. - attribute.accept(clazz, null, null, attributeBodyWriter); - - // Restore the original data output. - dataOutput = oldDataOutput; - - // Write the attribute length and body. - byte[] info = byteArrayOutputStream.toByteArray(); - - dataOutput.writeInt(info.length); - dataOutput.write(info); - } - - - private class AttributeBodyWriter - extends SimplifiedVisitor - implements AttributeVisitor, - BootstrapMethodInfoVisitor, - InnerClassesInfoVisitor, - ExceptionInfoVisitor, - StackMapFrameVisitor, - VerificationTypeVisitor, - LineNumberInfoVisitor, - ParameterInfoVisitor, - LocalVariableInfoVisitor, - LocalVariableTypeInfoVisitor, - AnnotationVisitor, - TypeAnnotationVisitor, - TargetInfoVisitor, - TypePathInfoVisitor, - LocalVariableTargetElementVisitor, - ElementValueVisitor - { - // Implementations for AttributeVisitor. - - public void visitUnknownAttribute(Clazz clazz, UnknownAttribute unknownAttribute) - { - // Write the unknown information. - dataOutput.write(unknownAttribute.info); - } - - - public void visitBootstrapMethodsAttribute(Clazz clazz, BootstrapMethodsAttribute bootstrapMethodsAttribute) - { - // Write the bootstrap methods. - dataOutput.writeShort(bootstrapMethodsAttribute.u2bootstrapMethodsCount); - - bootstrapMethodsAttribute.bootstrapMethodEntriesAccept(clazz, this); - } - - - public void visitSourceFileAttribute(Clazz clazz, SourceFileAttribute sourceFileAttribute) - { - dataOutput.writeShort(sourceFileAttribute.u2sourceFileIndex); - } - - - public void visitSourceDirAttribute(Clazz clazz, SourceDirAttribute sourceDirAttribute) - { - dataOutput.writeShort(sourceDirAttribute.u2sourceDirIndex); - } - - - public void visitInnerClassesAttribute(Clazz clazz, InnerClassesAttribute innerClassesAttribute) - { - // Write the inner classes. - dataOutput.writeShort(innerClassesAttribute.u2classesCount); - - innerClassesAttribute.innerClassEntriesAccept(clazz, this); - } - - - public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute) - { - dataOutput.writeShort(enclosingMethodAttribute.u2classIndex); - dataOutput.writeShort(enclosingMethodAttribute.u2nameAndTypeIndex); - } - - - public void visitDeprecatedAttribute(Clazz clazz, DeprecatedAttribute deprecatedAttribute) - { - // This attribute does not contain any additional information. - } - - - public void visitSyntheticAttribute(Clazz clazz, SyntheticAttribute syntheticAttribute) - { - // This attribute does not contain any additional information. - } - - - public void visitSignatureAttribute(Clazz clazz, SignatureAttribute signatureAttribute) - { - dataOutput.writeShort(signatureAttribute.u2signatureIndex); - } - - - public void visitConstantValueAttribute(Clazz clazz, Field field, ConstantValueAttribute constantValueAttribute) - { - dataOutput.writeShort(constantValueAttribute.u2constantValueIndex); - } - - - public void visitMethodParametersAttribute(Clazz clazz, Method method, MethodParametersAttribute methodParametersAttribute) - { - // Write the parameter information. - dataOutput.writeByte(methodParametersAttribute.u1parametersCount); - - methodParametersAttribute.parametersAccept(clazz, method, this); - } - - - public void visitExceptionsAttribute(Clazz clazz, Method method, ExceptionsAttribute exceptionsAttribute) - { - // Write the exceptions. - dataOutput.writeShort(exceptionsAttribute.u2exceptionIndexTableLength); - - for (int index = 0; index < exceptionsAttribute.u2exceptionIndexTableLength; index++) - { - dataOutput.writeShort(exceptionsAttribute.u2exceptionIndexTable[index]); - } - } - - - public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) - { - // Write the stack size and local variable frame size. - dataOutput.writeShort(codeAttribute.u2maxStack); - dataOutput.writeShort(codeAttribute.u2maxLocals); - - // Write the byte code. - dataOutput.writeInt(codeAttribute.u4codeLength); - - dataOutput.write(codeAttribute.code, 0, codeAttribute.u4codeLength); - - // Write the exceptions. - dataOutput.writeShort(codeAttribute.u2exceptionTableLength); - - codeAttribute.exceptionsAccept(clazz, method, this); - - // Write the code attributes. - dataOutput.writeShort(codeAttribute.u2attributesCount); - - codeAttribute.attributesAccept(clazz, method, ProgramClassWriter.this); - } - - - public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute) - { - // Write the stack map frames (only full frames, without tag). - dataOutput.writeShort(stackMapAttribute.u2stackMapFramesCount); - - stackMapAttribute.stackMapFramesAccept(clazz, method, codeAttribute, stackMapFrameBodyWriter); - } - - - public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute) - { - // Write the stack map frames. - dataOutput.writeShort(stackMapTableAttribute.u2stackMapFramesCount); - - stackMapTableAttribute.stackMapFramesAccept(clazz, method, codeAttribute, this); - } - - - public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute) - { - // Write the line numbers. - dataOutput.writeShort(lineNumberTableAttribute.u2lineNumberTableLength); - - lineNumberTableAttribute.lineNumbersAccept(clazz, method, codeAttribute, this); - } - - - public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute) - { - // Write the local variables. - dataOutput.writeShort(localVariableTableAttribute.u2localVariableTableLength); - - localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); - } - - - public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute) - { - // Write the local variable types. - dataOutput.writeShort(localVariableTypeTableAttribute.u2localVariableTypeTableLength); - - localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this); - } - - - public void visitAnyAnnotationsAttribute(Clazz clazz, AnnotationsAttribute annotationsAttribute) - { - // Write the annotations. - dataOutput.writeShort(annotationsAttribute.u2annotationsCount); - - annotationsAttribute.annotationsAccept(clazz, this); - } - - - public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute) - { - // Write the parameter annotations. - dataOutput.writeByte(parameterAnnotationsAttribute.u1parametersCount); - - for (int parameterIndex = 0; parameterIndex < parameterAnnotationsAttribute.u1parametersCount; parameterIndex++) - { - // Write the parameter annotations of the given parameter. - int u2annotationsCount = parameterAnnotationsAttribute.u2parameterAnnotationsCount[parameterIndex]; - Annotation[] annotations = parameterAnnotationsAttribute.parameterAnnotations[parameterIndex]; - - dataOutput.writeShort(u2annotationsCount); - - for (int index = 0; index < u2annotationsCount; index++) - { - visitAnnotation(clazz, annotations[index]); - } - - } - } - - - public void visitAnyTypeAnnotationsAttribute(Clazz clazz, TypeAnnotationsAttribute typeAnnotationsAttribute) - { - // Write the type annotations. - dataOutput.writeShort(typeAnnotationsAttribute.u2annotationsCount); - - typeAnnotationsAttribute.typeAnnotationsAccept(clazz, this); - } - - - public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute) - { - // Write the default element value. - annotationDefaultAttribute.defaultValue.accept(clazz, null, this); - } - - - // Implementations for BootstrapMethodInfoVisitor. - - public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo) - { - dataOutput.writeShort(bootstrapMethodInfo.u2methodHandleIndex); - - // Write the bootstrap method arguments. - dataOutput.writeShort(bootstrapMethodInfo.u2methodArgumentCount); - - for (int index = 0; index < bootstrapMethodInfo.u2methodArgumentCount; index++) - { - dataOutput.writeShort(bootstrapMethodInfo.u2methodArguments[index]); - } - } - - - // Implementations for InnerClassesInfoVisitor. - - public void visitInnerClassesInfo(Clazz clazz, InnerClassesInfo innerClassesInfo) - { - dataOutput.writeShort(innerClassesInfo.u2innerClassIndex); - dataOutput.writeShort(innerClassesInfo.u2outerClassIndex); - dataOutput.writeShort(innerClassesInfo.u2innerNameIndex); - dataOutput.writeShort(innerClassesInfo.u2innerClassAccessFlags); - } - - - // Implementations for ExceptionInfoVisitor. - - public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo) - { - dataOutput.writeShort(exceptionInfo.u2startPC); - dataOutput.writeShort(exceptionInfo.u2endPC); - dataOutput.writeShort(exceptionInfo.u2handlerPC); - dataOutput.writeShort(exceptionInfo.u2catchType); - } - - - // Implementations for StackMapFrameVisitor. - - public void visitAnyStackMapFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, StackMapFrame stackMapFrame) - { - // Write the stack map frame tag. - dataOutput.writeByte(stackMapFrame.getTag()); - - // Write the actual body. - stackMapFrame.accept(clazz, method, codeAttribute, offset, stackMapFrameBodyWriter); - } - - - // Implementations for LineNumberInfoVisitor. - - public void visitLineNumberInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberInfo lineNumberInfo) - { - dataOutput.writeShort(lineNumberInfo.u2startPC); - dataOutput.writeShort(lineNumberInfo.u2lineNumber); - } - - - // Implementations for ParameterInfoVisitor. - - public void visitParameterInfo(Clazz clazz, Method method, int parameterIndex, ParameterInfo parameterInfo) - { - dataOutput.writeShort(parameterInfo.u2nameIndex); - dataOutput.writeShort(parameterInfo.u2accessFlags); - } - - - // Implementations for LocalVariableInfoVisitor. - - public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo) - { - dataOutput.writeShort(localVariableInfo.u2startPC); - dataOutput.writeShort(localVariableInfo.u2length); - dataOutput.writeShort(localVariableInfo.u2nameIndex); - dataOutput.writeShort(localVariableInfo.u2descriptorIndex); - dataOutput.writeShort(localVariableInfo.u2index); - } - - - // Implementations for LocalVariableTypeInfoVisitor. - - public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo) - { - dataOutput.writeShort(localVariableTypeInfo.u2startPC); - dataOutput.writeShort(localVariableTypeInfo.u2length); - dataOutput.writeShort(localVariableTypeInfo.u2nameIndex); - dataOutput.writeShort(localVariableTypeInfo.u2signatureIndex); - dataOutput.writeShort(localVariableTypeInfo.u2index); - } - - - // Implementations for AnnotationVisitor. - - public void visitAnnotation(Clazz clazz, Annotation annotation) - { - // Write the annotation type. - dataOutput.writeShort(annotation.u2typeIndex); - - // Write the element value pairs. - dataOutput.writeShort(annotation.u2elementValuesCount); - - annotation.elementValuesAccept(clazz, this); - } - - - // Implementations for TypeAnnotationVisitor. - - public void visitTypeAnnotation(Clazz clazz, TypeAnnotation typeAnnotation) - { - // Write the target info. - dataOutput.writeByte(typeAnnotation.targetInfo.u1targetType); - - typeAnnotation.targetInfoAccept(clazz, this); - - // Write the type path. - dataOutput.writeByte(typeAnnotation.typePath.length); - - typeAnnotation.typePathInfosAccept(clazz, this); - - // Write the actual annotation. - visitAnnotation(clazz, typeAnnotation); - } - - - // Implementations for TargetInfoVisitor. - - public void visitTypeParameterTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypeParameterTargetInfo typeParameterTargetInfo) - { - dataOutput.writeByte(typeParameterTargetInfo.u1typeParameterIndex); - } - - - public void visitSuperTypeTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, SuperTypeTargetInfo superTypeTargetInfo) - { - dataOutput.writeShort(superTypeTargetInfo.u2superTypeIndex); - } - - - public void visitTypeParameterBoundTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypeParameterBoundTargetInfo typeParameterBoundTargetInfo) - { - dataOutput.writeByte(typeParameterBoundTargetInfo.u1typeParameterIndex); - dataOutput.writeByte(typeParameterBoundTargetInfo.u1boundIndex); - } - - - public void visitEmptyTargetInfo(Clazz clazz, Member member, TypeAnnotation typeAnnotation, EmptyTargetInfo emptyTargetInfo) - { - } - - - public void visitFormalParameterTargetInfo(Clazz clazz, Method method, TypeAnnotation typeAnnotation, FormalParameterTargetInfo formalParameterTargetInfo) - { - dataOutput.writeByte(formalParameterTargetInfo.u1formalParameterIndex); - } - - - public void visitThrowsTargetInfo(Clazz clazz, Method method, TypeAnnotation typeAnnotation, ThrowsTargetInfo throwsTargetInfo) - { - dataOutput.writeShort(throwsTargetInfo.u2throwsTypeIndex); - } - - - public void visitLocalVariableTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo) - { - // Write the local variable target elements. - dataOutput.writeShort(localVariableTargetInfo.u2tableLength); - - localVariableTargetInfo.targetElementsAccept(clazz, method, codeAttribute, typeAnnotation, this); - } - - - public void visitCatchTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, CatchTargetInfo catchTargetInfo) - { - dataOutput.writeShort(catchTargetInfo.u2exceptionTableIndex); - } - - - public void visitOffsetTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, OffsetTargetInfo offsetTargetInfo) - { - dataOutput.writeShort(offsetTargetInfo.u2offset); - } - - - public void visitTypeArgumentTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, TypeArgumentTargetInfo typeArgumentTargetInfo) - { - dataOutput.writeShort(typeArgumentTargetInfo.u2offset); - dataOutput.writeByte(typeArgumentTargetInfo.u1typeArgumentIndex); - } - - - // Implementations for TypePathInfoVisitor. - - public void visitTypePathInfo(Clazz clazz, TypeAnnotation typeAnnotation, TypePathInfo typePathInfo) - { - dataOutput.writeByte(typePathInfo.u1typePathKind); - dataOutput.writeByte(typePathInfo.u1typeArgumentIndex); - } - - - // Implementations for LocalVariableTargetElementVisitor. - - public void visitLocalVariableTargetElement(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo, LocalVariableTargetElement localVariableTargetElement) - { - dataOutput.writeShort(localVariableTargetElement.u2startPC); - dataOutput.writeShort(localVariableTargetElement.u2length); - dataOutput.writeShort(localVariableTargetElement.u2index); - } - - - // Implementations for ElementValueVisitor. - - public void visitAnyElementValue(Clazz clazz, Annotation annotation, ElementValue elementValue) - { - // Write the element name index, if applicable. - int u2elementNameIndex = elementValue.u2elementNameIndex; - if (u2elementNameIndex != 0) - { - dataOutput.writeShort(u2elementNameIndex); - } - - // Write the tag. - dataOutput.writeByte(elementValue.getTag()); - - // Write the actual body. - elementValue.accept(clazz, annotation, elementValueBodyWriter); - } - } - - - private class StackMapFrameBodyWriter - extends SimplifiedVisitor - implements StackMapFrameVisitor, - VerificationTypeVisitor - { - public void visitSameZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameZeroFrame sameZeroFrame) - { - if (sameZeroFrame.getTag() == StackMapFrame.SAME_ZERO_FRAME_EXTENDED) - { - dataOutput.writeShort(sameZeroFrame.u2offsetDelta); - } - } - - - public void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame) - { - if (sameOneFrame.getTag() == StackMapFrame.SAME_ONE_FRAME_EXTENDED) - { - dataOutput.writeShort(sameOneFrame.u2offsetDelta); - } - - // Write the verification type of the stack entry. - sameOneFrame.stackItemAccept(clazz, method, codeAttribute, offset, this); - } - - - public void visitLessZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LessZeroFrame lessZeroFrame) - { - dataOutput.writeShort(lessZeroFrame.u2offsetDelta); - } - - - public void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame) - { - dataOutput.writeShort(moreZeroFrame.u2offsetDelta); - - // Write the verification types of the additional local variables. - moreZeroFrame.additionalVariablesAccept(clazz, method, codeAttribute, offset, this); - } - - - public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame) - { - dataOutput.writeShort(fullFrame.u2offsetDelta); - - // Write the verification types of the local variables. - dataOutput.writeShort(fullFrame.variablesCount); - fullFrame.variablesAccept(clazz, method, codeAttribute, offset, this); - - // Write the verification types of the stack entries. - dataOutput.writeShort(fullFrame.stackCount); - fullFrame.stackAccept(clazz, method, codeAttribute, offset, this); - } - - - // Implementations for VerificationTypeVisitor. - - public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType) - { - // Write the verification type tag. - dataOutput.writeByte(verificationType.getTag()); - - // Write the actual body. - verificationType.accept(clazz, method, codeAttribute, offset, verificationTypeBodyWriter); - } - } - - - private class VerificationTypeBodyWriter - extends SimplifiedVisitor - implements VerificationTypeVisitor - { - // Implementations for VerificationTypeVisitor. - - public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType) - { - // Most verification types don't contain any additional information. - } - - - public void visitObjectType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ObjectType objectType) - { - dataOutput.writeShort(objectType.u2classIndex); - } - - - public void visitUninitializedType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, UninitializedType uninitializedType) - { - dataOutput.writeShort(uninitializedType.u2newInstructionOffset); - } - } - - - private class ElementValueBodyWriter - extends SimplifiedVisitor - implements ElementValueVisitor - { - // Implementations for ElementValueVisitor. - - public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue) - { - dataOutput.writeShort(constantElementValue.u2constantValueIndex); - } - - - public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue) - { - dataOutput.writeShort(enumConstantElementValue.u2typeNameIndex); - dataOutput.writeShort(enumConstantElementValue.u2constantNameIndex); - } - - - public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue) - { - dataOutput.writeShort(classElementValue.u2classInfoIndex); - } - - - public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue) - { - // Write the annotation. - attributeBodyWriter.visitAnnotation(clazz, annotationElementValue.annotationValue); - } - - - public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue) - { - // Write the element values. - dataOutput.writeShort(arrayElementValue.u2elementValuesCount); - - arrayElementValue.elementValuesAccept(clazz, annotation, attributeBodyWriter); - } - } -} diff --git a/src/proguard/classfile/io/RuntimeDataInput.java b/src/proguard/classfile/io/RuntimeDataInput.java deleted file mode 100644 index 905d828..0000000 --- a/src/proguard/classfile/io/RuntimeDataInput.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * ProGuard -- shrinking, optimization, obfuscation, and preverification - * of Java bytecode. - * - * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package proguard.classfile.io; - -import java.io.*; - -/** - * This class delegates its method calls to the corresponding DataInput methods, - * converting its IOExceptions to RuntimeExceptions. - * - * @author Eric Lafortune - */ -final class RuntimeDataInput -{ - private final DataInput dataInput; - - - public RuntimeDataInput(DataInput dataInput) - { - this.dataInput = dataInput; - } - - - // Methods delegating to DataInput. - - public boolean readBoolean() - { - try - { - return dataInput.readBoolean(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public byte readByte() - { - try - { - return dataInput.readByte(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public char readChar() - { - try - { - return dataInput.readChar(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public double readDouble() - { - try - { - return dataInput.readDouble(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public float readFloat() - { - try - { - return dataInput.readFloat(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public void readFully(byte[] b) - { - try - { - dataInput.readFully(b); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public void readFully(byte[] b, int off, int len) - { - try - { - dataInput.readFully(b, off, len); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public int readInt() - { - try - { - return dataInput.readInt(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public String readLine() - { - try - { - return dataInput.readLine(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public long readLong() - { - try - { - return dataInput.readLong(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public short readShort() - { - try - { - return dataInput.readShort(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public int readUnsignedByte() - { - try - { - return dataInput.readUnsignedByte(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public int readUnsignedShort() - { - try - { - return dataInput.readUnsignedShort(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public String readUTF() - { - try - { - return dataInput.readUTF(); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - public int skipBytes(int n) - { - try - { - return dataInput.skipBytes(n); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } -} diff --git a/src/proguard/classfile/io/RuntimeDataOutput.java b/src/proguard/classfile/io/RuntimeDataOutput.java deleted file mode 100644 index 9d89332..0000000 --- a/src/proguard/classfile/io/RuntimeDataOutput.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * ProGuard -- shrinking, optimization, obfuscation, and preverification - * of Java bytecode. - * - * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package proguard.classfile.io; - -import java.io.*; - -/** - * This class delegates its method calls to the corresponding DataOutput methods, - * converting its IOExceptions to RuntimeExceptions. - * - * @author Eric Lafortune - */ -final class RuntimeDataOutput -{ - private final DataOutput dataOutput; - - - public RuntimeDataOutput(DataOutput dataOutput) - { - this.dataOutput = dataOutput; - } - - - // Methods delegating to DataOutput. - - public void write(byte[] b) - { - try - { - dataOutput.write(b); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void write(byte[] b, int off, int len) - { - try - { - dataOutput.write(b, off, len); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void write(int b) - { - try - { - dataOutput.write(b); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeBoolean(boolean v) - { - try - { - dataOutput.writeBoolean(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeByte(int v) - { - try - { - dataOutput.writeByte(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeBytes(String s) - { - try - { - dataOutput.writeBytes(s); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeChar(int v) - { - try - { - dataOutput.writeChar(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeChars(String s) - { - try - { - dataOutput.writeChars(s); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeDouble(double v) - { - try - { - dataOutput.writeDouble(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeFloat(float v) - { - try - { - dataOutput.writeFloat(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeInt(int v) - { - try - { - dataOutput.writeInt(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeLong(long v) - { - try - { - dataOutput.writeLong(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeShort(int v) - { - try - { - dataOutput.writeShort(v); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } - - - public void writeUTF(String str) - { - try - { - dataOutput.writeUTF(str); - } - catch (IOException ex) - { - throw new RuntimeException(ex.getMessage()); - } - } -} diff --git a/src/proguard/classfile/io/package.html b/src/proguard/classfile/io/package.html deleted file mode 100644 index 780b917..0000000 --- a/src/proguard/classfile/io/package.html +++ /dev/null @@ -1,3 +0,0 @@ -<body> -This package contains classes for reading and writing class files. -</body> |