From 6d567bd3e4f296fe9804524071c37d6710a71a8f Mon Sep 17 00:00:00 2001 From: Humberto Silva Naves Date: Fri, 18 Jul 2008 01:05:45 -0300 Subject: [PATCH] Criado um novo pacote --- pom.xml | 15 +++++ .../br/org/javadecompiler/decoder/Decoder.java | 66 ++++++++-------------- .../{decoder => disassembler}/Disassembler.java | 3 +- .../br/org/javadecompiler/decoder/DecoderTest.java | 2 +- 4 files changed, 42 insertions(+), 44 deletions(-) rename src/main/java/br/org/javadecompiler/{decoder => disassembler}/Disassembler.java (99%) diff --git a/pom.xml b/pom.xml index 6a82809..792f93d 100644 --- a/pom.xml +++ b/pom.xml @@ -25,4 +25,19 @@ + + + + org.apache.maven.plugins + maven-surefire-plugin + + + classes/** + + + + + + + \ No newline at end of file diff --git a/src/main/java/br/org/javadecompiler/decoder/Decoder.java b/src/main/java/br/org/javadecompiler/decoder/Decoder.java index 8dfe140..1570fc6 100644 --- a/src/main/java/br/org/javadecompiler/decoder/Decoder.java +++ b/src/main/java/br/org/javadecompiler/decoder/Decoder.java @@ -15,6 +15,7 @@ import br.org.javadecompiler.classfile.attribute.ExceptionsAttribute; import br.org.javadecompiler.classfile.attribute.InnerClassesAttribute; import br.org.javadecompiler.classfile.attribute.LineNumberTableAttribute; import br.org.javadecompiler.classfile.attribute.LocalVariableTableAttribute; +import br.org.javadecompiler.classfile.attribute.SignatureAttribute; import br.org.javadecompiler.classfile.attribute.SourceFileAttribute; import br.org.javadecompiler.classfile.attribute.SyntheticAttribute; import br.org.javadecompiler.classfile.attribute.UnknownAttribute; @@ -108,19 +109,24 @@ public final class Decoder { reader.mark(); if (logger.isDebugEnabled()) logger.debug("Decoding constants: step 1"); - for(int index = 1; index < constants.length; index++) { + + int index; + for(index = 1; index < constants.length; index++) { Constant constant = decodeConstantFirstPass(index); constants[index] = constant; if ((constant instanceof LongConstant) || (constant instanceof DoubleConstant)) index++; } + if (index > constants.length) + throw new DecoderException("Last constant cannot be of type Long or Double"); + reader.rollback(); if (logger.isDebugEnabled()) { logger.debug("Decoding constants: step 2"); } - for(int index = 1; index < constants.length; index++) { + for(index = 1; index < constants.length; index++) { Constant constant = constants[index]; decodeConstantSecondPass(constant, pool); @@ -184,20 +190,13 @@ public final class Decoder { result.setValue(reader.readSignedInt()); } else if (constant instanceof FloatConstant) { FloatConstant result = (FloatConstant) constant; - int bits = reader.readSignedInt(); - result.setValue(Float.intBitsToFloat(bits)); + result.setValue(reader.readFloat()); } else if (constant instanceof DoubleConstant) { DoubleConstant result = (DoubleConstant) constant; - long highHalf = reader.readUnsignedInt(); - long lowHalf = reader.readUnsignedInt(); - long bits = highHalf << 32 | lowHalf; - result.setValue(Double.longBitsToDouble(bits)); + result.setValue(reader.readDouble()); } else if (constant instanceof LongConstant) { LongConstant result = (LongConstant) constant; - long highHalf = reader.readUnsignedInt(); - long lowHalf = reader.readUnsignedInt(); - long bits = highHalf << 32 | lowHalf; - result.setValue(bits); + result.setValue(reader.readSignedLong()); } else if (constant instanceof NameAndTypeConstant) { NameAndTypeConstant result = (NameAndTypeConstant) constant; int nameIndex = reader.readUnsignedShort(); @@ -206,34 +205,7 @@ public final class Decoder { result.setDescriptor((UTF8Constant) pool.getConstant(descriptorIndex)); } else if (constant instanceof UTF8Constant) { UTF8Constant result = (UTF8Constant) constant; - byte[] bytes = new byte[reader.readUnsignedShort()]; - for(int i = 0; i < bytes.length; i++) - bytes[i] = reader.readSignedByte(); - - StringBuffer sb = new StringBuffer(); - for(int i = 0; i < bytes.length; i++) { - byte x, y, z; - char c; - x = bytes[i]; - if ((x & 0x80) != 0) { - if (i == (bytes.length - 1)) - throw new DecoderException("Can't parse UTF8 constant"); - - y = bytes[++i]; - if ((x & 0x40) != 0) { - if (i == (bytes.length - 2)) - throw new DecoderException("Can't parse UTF8 constant"); - z = bytes[++i]; - c = (char) (((x & 0x0f) << 12) | ((y & 0x3f) << 6) | (z & 0x3f)) ; - } else { - c = (char) (((x & 0x1f) << 6) | (y & 0x3f)) ; - } - } else { - c = (char) x; - } - sb.append(c); - } - result.setValue(sb.toString()); + result.setValue(reader.readUtf8()); } } @@ -270,8 +242,6 @@ public final class Decoder { for(int i = 0; i < interfaces.length; i++) { int interfaceIndex = reader.readUnsignedShort(); interfaces[i] = (ClassConstant) ownerClass.getConstantPool().getConstant(interfaceIndex); - if (logger.isDebugEnabled()) - logger.debug("implements " + interfaces[i]); } ownerClass.setInterfaces(interfaces); } @@ -336,6 +306,8 @@ public final class Decoder { result = decodeSourceFileAttribute(); } else if (Attribute.SYNTHETIC_ATTRIBUTE.equals(name)) { result = decodeSyntheticAttribute(); + } else if (Attribute.SIGNATURE_ATTRIBUTE.equals(name)) { + result = decodeSignatureAttribute(); } else { result = decodeUnknownAttribute(); if (logger.isDebugEnabled()) { @@ -490,6 +462,16 @@ public final class Decoder { return result; } + private SignatureAttribute decodeSignatureAttribute() throws DecoderException, ClassReaderException { + SignatureAttribute result = new SignatureAttribute(); + long attributeLength = reader.readUnsignedInt(); + if (attributeLength != 2) + throw new DecoderException("Invalid Signature attribute length"); + int signatureIndex = reader.readUnsignedShort(); + result.setSignature((UTF8Constant) ownerClass.getConstantPool().getConstant(signatureIndex)); + return result; + } + private UnknownAttribute decodeUnknownAttribute() throws DecoderException, ClassReaderException { UnknownAttribute result = new UnknownAttribute(); long attributeLength = reader.readUnsignedInt(); diff --git a/src/main/java/br/org/javadecompiler/decoder/Disassembler.java b/src/main/java/br/org/javadecompiler/disassembler/Disassembler.java similarity index 99% rename from src/main/java/br/org/javadecompiler/decoder/Disassembler.java rename to src/main/java/br/org/javadecompiler/disassembler/Disassembler.java index 2b32963..c538117 100644 --- a/src/main/java/br/org/javadecompiler/decoder/Disassembler.java +++ b/src/main/java/br/org/javadecompiler/disassembler/Disassembler.java @@ -1,6 +1,7 @@ -package br.org.javadecompiler.decoder; +package br.org.javadecompiler.disassembler; import br.org.javadecompiler.classfile.attribute.CodeAttribute; +import br.org.javadecompiler.decoder.Utils; public class Disassembler { public static final int diff --git a/src/test/java/br/org/javadecompiler/decoder/DecoderTest.java b/src/test/java/br/org/javadecompiler/decoder/DecoderTest.java index 5a603da..4fa261b 100644 --- a/src/test/java/br/org/javadecompiler/decoder/DecoderTest.java +++ b/src/test/java/br/org/javadecompiler/decoder/DecoderTest.java @@ -22,7 +22,7 @@ public class DecoderTest extends TestCase { private void decodeClass(Class clazz) throws Exception { JavaClass result = Decoder.decodeClass(readClass(clazz)); assertNotNull(result); - System.out.println(result); + //System.out.println(result); } public void testDecodeThis() throws Exception { -- 2.11.4.GIT