Index: src/main/java/org/apache/harmony/pack200/SegmentConstantPool.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -290,47 +290,4 @@ throw new Error("Get value incomplete"); } } - - protected ConstantPoolEntry cachedCpAll[] = {}; - public ConstantPoolEntry[] getCpAll() throws Pack200Exception { - if(cachedCpAll.length == 0) { - ArrayList cpAll = new ArrayList(); - // TODO: this is 1.5-specific. Try to get rid - // of it. - for(int index=0; index < bands.getCpUTF8().length; index++) { - cpAll.add(getConstantPoolEntry(UTF_8, index)); - } - for(int index=0; index < bands.getCpInt().length; index++) { - cpAll.add(getConstantPoolEntry(CP_INT, index)); - } - for(int index=0; index < bands.getCpFloat().length; index++) { - cpAll.add(getConstantPoolEntry(CP_FLOAT, index)); - } - for(int index=0; index < bands.getCpLong().length; index++) { - cpAll.add(getConstantPoolEntry(CP_LONG, index)); - } - for(int index=0; index < bands.getCpDouble().length; index++) { - cpAll.add(getConstantPoolEntry(CP_DOUBLE, index)); - } - for(int index=0; index < bands.getCpString().length; index++) { - cpAll.add(getConstantPoolEntry(CP_STRING, index)); - } - for(int index=0; index < bands.getCpClass().length; index++) { - cpAll.add(getConstantPoolEntry(CP_CLASS, index)); - } - for(int index=0; index < bands.getCpFieldClass().length; index++) { - cpAll.add(getConstantPoolEntry(CP_FIELD, index)); - } - for(int index=0; index < bands.getCpMethodClass().length; index++) { - cpAll.add(getConstantPoolEntry(CP_METHOD, index)); - } - for(int index=0; index < bands.getCpIMethodClass().length; index++) { - cpAll.add(getConstantPoolEntry(CP_IMETHOD, index)); - } - ConstantPoolEntry[] result = new ConstantPoolEntry[cpAll.size()]; - cpAll.toArray(result); - cachedCpAll = result; - } - return cachedCpAll; - } } \ No newline at end of file Index: src/main/java/org/apache/harmony/pack200/bytecode/CPClass.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -19,8 +19,8 @@ import java.io.DataOutputStream; import java.io.IOException; +public class CPClass extends ConstantPoolEntry { -public class CPClass extends ConstantPoolEntry { private int index; public String name; @@ -25,8 +25,13 @@ public String name; - private CPUTF8 utf8; + private final CPUTF8 utf8; + /** + * Creates a new CPClass + * @param name + * @throws NullPointerException if name is null + */ public CPClass(CPUTF8 name) { super(ConstantPoolEntry.CP_Class); this.name = name.underlyingString(); @@ -32,6 +37,9 @@ this.name = name.underlyingString(); this.domain = ClassConstantPool.DOMAIN_CLASSREF; this.utf8 = name; + if(name == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } public boolean equals(Object obj) { @@ -42,17 +50,7 @@ if (this.getClass() != obj.getClass()) return false; final CPClass other = (CPClass) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (utf8 == null) { - if (other.utf8 != null) - return false; - } else if (!utf8.equals(other.utf8)) - return false; - return true; + return utf8.equals(other.utf8); } protected ClassFileEntry[] getNestedClassFileEntries() { @@ -61,11 +59,7 @@ public int hashCode() { - final int PRIME = 31; - int result = 1; - result = PRIME * result + ((name == null) ? 0 : name.hashCode()); - result = PRIME * result + ((utf8 == null) ? 0 : utf8.hashCode()); - return result; + return utf8.hashCode(); } protected void resolve(ClassConstantPool pool) { Index: src/main/java/org/apache/harmony/pack200/bytecode/CPConstant.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -19,14 +19,22 @@ public abstract class CPConstant extends ConstantPoolEntry { - private Object value; + private final Object value; + /** + * Create a new CPConstant + * @param tag + * @param value + * @throws NullPointerException if value is null + */ public CPConstant(byte tag, Object value) { super(tag); this.value = value; + if (value == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } - public boolean equals(Object obj) { if (this == obj) return true; Index: src/main/java/org/apache/harmony/pack200/bytecode/CPMember.java =================================================================== --- . (revision 641277) +++ . (working copy) @@ -18,7 +18,7 @@ import java.io.DataOutputStream; import java.io.IOException; -import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -28,9 +28,17 @@ short flags; CPUTF8 name; transient int nameIndex; - private CPUTF8 descriptor; + private final CPUTF8 descriptor; transient int descriptorIndex; + /** + * Create a new CPMember + * @param name + * @param descriptor + * @param flags + * @param attributes + * @throws NullPointerException if name or descriptor is null + */ public CPMember(CPUTF8 name, CPUTF8 descriptor, long flags, List attributes) { this.name = name; this.descriptor = descriptor; @@ -35,7 +43,10 @@ this.name = name; this.descriptor = descriptor; this.flags = (short) flags; - this.attributes = (attributes == null ? new ArrayList() : attributes); + this.attributes = (attributes == null ? Collections.EMPTY_LIST : attributes); + if(name == null || descriptor == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } protected ClassFileEntry[] getNestedClassFileEntries() { @@ -66,10 +77,10 @@ public int hashCode() { final int PRIME = 31; int result = 1; - result = PRIME * result + ((attributes == null) ? 0 : attributes.hashCode()); - result = PRIME * result + ((descriptor == null) ? 0 : descriptor.hashCode()); + result = PRIME * result + attributes.hashCode(); + result = PRIME * result + descriptor.hashCode(); result = PRIME * result + flags; - result = PRIME * result + ((name == null) ? 0 : name.hashCode()); + result = PRIME * result + name.hashCode(); return result; } @@ -81,22 +92,13 @@ if (getClass() != obj.getClass()) return false; final CPMember other = (CPMember) obj; - if (attributes == null) { - if (other.attributes != null) - return false; - } else if (!attributes.equals(other.attributes)) + if (!attributes.equals(other.attributes)) return false; - if (descriptor == null) { - if (other.descriptor != null) - return false; - } else if (!descriptor.equals(other.descriptor)) + if (!descriptor.equals(other.descriptor)) return false; if (flags != other.flags) return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) + if (!name.equals(other.name)) return false; return true; } Index: src/main/java/org/apache/harmony/pack200/bytecode/CPNameAndType.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -31,6 +31,13 @@ transient int nameIndex; + /** + * Create a new CPNameAndType + * @param name + * @param descriptor + * @param domain + * @throws NullPointerException if name or descriptor is null + */ public CPNameAndType(CPUTF8 name, CPUTF8 descriptor, int domain) { super(ConstantPoolEntry.CP_NameAndType); this.name = name; @@ -36,6 +43,9 @@ this.name = name; this.descriptor = descriptor; this.domain = domain; + if(name == null || descriptor == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } protected ClassFileEntry[] getNestedClassFileEntries() { @@ -67,8 +77,8 @@ public int hashCode() { final int PRIME = 31; int result = 1; - result = PRIME * result + ((descriptor == null) ? 0 : descriptor.hashCode()); - result = PRIME * result + ((name == null) ? 0 : name.hashCode()); + result = PRIME * result + descriptor.hashCode(); + result = PRIME * result + name.hashCode(); return result; } @@ -81,15 +91,9 @@ if (getClass() != obj.getClass()) return false; final CPNameAndType other = (CPNameAndType) obj; - if (descriptor == null) { - if (other.descriptor != null) - return false; - } else if (!descriptor.equals(other.descriptor)) + if (!descriptor.equals(other.descriptor)) return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) + if (!name.equals(other.name)) return false; return true; } Index: src/main/java/org/apache/harmony/pack200/bytecode/CPRef.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -27,6 +27,13 @@ protected CPNameAndType nameAndType; transient int nameAndTypeIndex; + /** + * Create a new CPRef + * @param type + * @param className + * @param descriptor + * @throws NullPointerException if descriptor or className is null + */ public CPRef(byte type, CPClass className, CPNameAndType descriptor) { super(type); this.className = className; @@ -31,6 +38,9 @@ super(type); this.className = className; this.nameAndType = descriptor; + if(descriptor == null || className == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } public boolean equals(Object obj) { @@ -44,15 +54,9 @@ return false; } final CPRef other = (CPRef) obj; - if (className == null) { - if (other.className != null) - return false; - } else if (!className.equals(other.className)) + if (!className.equals(other.className)) return false; - if (nameAndType == null) { - if (other.nameAndType != null) - return false; - } else if (!nameAndType.equals(other.nameAndType)) + if (!nameAndType.equals(other.nameAndType)) return false; return true; } @@ -65,13 +69,8 @@ } public int hashCode() { - final int PRIME = 31; - int result = 1; - result = PRIME * result - + ((className == null) ? 0 : className.hashCode()); - result = PRIME * result - + ((nameAndType == null) ? 0 : nameAndType.hashCode()); - return result; + final int PRIME = 37; + return (PRIME * className.hashCode()) + nameAndType.hashCode(); } protected void resolve(ClassConstantPool pool) { Index: src/main/java/org/apache/harmony/pack200/bytecode/CPUTF8.java =================================================================== --- . (revision 643840) +++ . (working copy) @@ -22,8 +22,14 @@ public class CPUTF8 extends ConstantPoolEntry { - private String utf8; + private final String utf8; + /** + * Creates a new CPUTF8 instance + * @param utf8 + * @param domain + * @throws NullPointerException if utf8 is null + */ public CPUTF8(String utf8, int domain) { super(ConstantPoolEntry.CP_UTF8); this.utf8 = utf8; @@ -31,6 +37,9 @@ if(domain == ClassConstantPool.DOMAIN_UNDEFINED) { throw new RuntimeException(); } + if(utf8 == null) { + throw new NullPointerException("Null arguments are not allowed"); + } } public boolean equals(Object obj) { @@ -41,12 +50,7 @@ if (this.getClass() != obj.getClass()) return false; final CPUTF8 other = (CPUTF8) obj; - if (utf8 == null) { - if (other.utf8 != null) - return false; - } else if (!utf8.equals(other.utf8)) - return false; - return true; + return utf8.equals(other.utf8); } public int hashCode() { @@ -51,9 +55,7 @@ public int hashCode() { final int PRIME = 31; - int result = 1; - result = PRIME * result + ((utf8 == null) ? 0 : utf8.hashCode()); - return result; + return PRIME + utf8.hashCode(); } public String toString() { @@ -64,11 +66,7 @@ byte[] bytes; try { // TODO Check that this is the right UTF-8 for bytes - if (utf8 == null) { - bytes = new byte[0]; - } else { - bytes = utf8.getBytes("UTF-8"); - } + bytes = utf8.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Couldn't convert string " + utf8 + " to UTF-8");