26 November 2007

XMLClassUtil Project


/*
* XMLClassUtil.java
*
* Copyright (2007)
* by Emory Merryman, III
* http://indicativeserviceconcierge.blogspot.com/

* (For Contact Information See Web Page.
*
* All Rights Reserved.
*
* Software is confidential and copyrighted.
* Title to Software and all associated intellectual property
* rights is retained by Emory Merryman, III.
* You acknowledge that Licensed Software is not designed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* Emory Merryman, III disclaims any express or implied warranty
* of fitness for such uses.
* No right, title or interest in or to any trademark, service
* mark, logo or trade name of Emory Merryman, III is granted.
*
* All express or implied conditions, representations and warranties,
* including any implied warranty of merchantability, fitness
* for a particular purpose or non infringement are disclaimed,
* except to the extent that these disclaimers are held
* to be legally invalid.
*
* To the extent not prohibited by law, in no event will Emory
* Merryman, III be liable for any lost revenue, profit or data,
* or for special indirect, consequential, incidental or punitive
* damages, arising out of or related to the use or inability to
* use software if if Emory Merryman, III has been advised of such
* damages.
* In no event will Emory Merryman III's liability to you, whether
* in contract, tort (including negligence), or otherwise, exceed
* the amount paid by you for this software.
*/

final class XMLClassUtil
{
private static final int CAFEBABE = -889275714 ;

private static final void diff ( final java . lang . String fn1 , final java . lang . String fn2 ) throws java . lang . Exception
{
final java . io . InputStream in1 = new java . io . FileInputStream ( new java . io . File ( fn1 ) ) ;
final java . io . InputStream in2 = new java . io . FileInputStream ( new java . io . File ( fn2 ) ) ;
int i , r1 , r2 ;
for ( i = 0 , r1 = in1 . read ( ) , r2 = in2 . read ( ) ; ( r1 != -1 ) || ( r2 != -1 ) ; i ++ , r1 = in1 . read ( ) , r2 = in2 . read ( ) )
{
java . lang . System . out . println ( i + "\t" + r1 + "\t" + r2 + "\t" + ( r1 == r2 ) ) ;
}
in1 .close ( ) ;
in2 . close ( ) ;
}

public static final void main ( final java . lang . String [ ] args ) throws java . lang . Exception
{
// diff ( args [ 1 ] , args [ 2 ] ) ;
// java . lang . System . exit ( 0 ) ;
final java . io . DataInputStream input = new java . io . DataInputStream ( new java . io . FileInputStream ( new java . io . File ( args [ 1 ] ) ) ) ;
final java . io . DataOutputStream output = new java . io . DataOutputStream ( new java . io . FileOutputStream ( new java. io . File ( args [ 2 ] ) ) ) ;
final XMLClassUtil util = new XMLClassUtil ( ) ;
if ( args [ 0 ] . equals ( "toXML" ) )
{
final ClassFile classFile = util . dataInputStreamFactory . make ( input ) ;
final java . io . PrintStream pOut = new java . io . PrintStream ( output ) ;
final org . w3c . dom . Document classDocument = util . xmlClass ( classFile ) ;
util . print ( pOut , classDocument ) ;
}
else if ( args [ 0 ] . equals ( "toClass" ) )
{
final javax . xml . parsers . DocumentBuilderFactory dbf = javax . xml . parsers . DocumentBuilderFactory . newInstance ( ) ;
final javax . xml . parsers . DocumentBuilder db = dbf . newDocumentBuilder ( ) ;
final org . w3c . dom . Document document = db . parse ( input ) ;
final org . w3c . dom . Element node1 = document . getDocumentElement ( ) ;
final org . w3c . dom . NodeList nodes = node1 . getElementsByTagName ( "classFile" ) ;
final org . w3c . dom . Node node2 = nodes . item ( 0 ) ;
final ClassFile classFile = util . xmlReader . read ( node2 ) ;
util . byteWriter . write ( classFile , output ) ;
}
input . close ( ) ;
output . close ( ) ;
input . close ( ) ;
}

private final ClassFile loadClass ( final java . io . DataInputStream input ) throws XMLClassUtilException
{
final ClassFile classFile = dataInputStreamFactory . make ( input ) ;
return ( classFile ) ;
}

private final org . w3c . dom . Document xmlClass ( final ClassFile classFile ) throws java . lang . Exception
{
final javax . xml . parsers . DocumentBuilderFactory dbf = javax . xml . parsers . DocumentBuilderFactory . newInstance ( ) ;
final javax . xml . parsers . DocumentBuilder db = dbf . newDocumentBuilder ( ) ;
final org . w3c . dom . Document document = db . newDocument ( ) ;
final org . w3c . dom . Node element = document . createElement ( "xml" ) ;
document . appendChild ( element ) ;
classFileWriter . write ( classFile , document , element ) ;
return ( document ) ;
}

private interface Factory < R , P , E extends java . lang . Exception >
{
public abstract R make ( final P p ) throws E ;
}

private interface ByteWriter < P , E extends java . lang . Exception >
{
public abstract void write ( final P p , final java . io . DataOutputStream output ) throws E ;
}

private interface ClassFile
{
public abstract int getMagic ( ) ;

public abstract short getMinorVersion ( ) ;

public abstract short getMajorVersion ( ) ;

public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAccessFlags ( ) ;

public abstract short getThisClass ( ) ;

public abstract short getSuperClass ( ) ;

public abstract short [ ] getInterfaces ( ) ;

public abstract java . util . List < FieldInfo > getFields ( ) ;

public abstract java . util . List < MethodInfo > getMethods ( ) ;

public abstract java . util . List < AttributeInfo > getAttributes ( ) ;
}

private interface ConstantPool
{
public abstract byte getTag ( ) ;

public abstract byte [ ] getInfo ( ) ;
}

private interface FieldMethodInfo
{
public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAccessFlags ( ) ;

public abstract short getNameIndex ( ) ;

public abstract short getDescriptorIndex ( ) ;

public abstract java . util . List < AttributeInfo > getAttributes ( ) ;
}

private interface FieldInfo extends FieldMethodInfo
{
}

private interface MethodInfo extends FieldMethodInfo
{
}

private interface FMInfo extends FieldInfo , MethodInfo
{
}

private interface AttributeInfo
{
public abstract java . util . List < ConstantPool > getConstantPool ( ) ;

public abstract short getAttributeNameIndex ( ) ;

public abstract byte [ ] getInfo ( ) ;
}

public interface ConstantClassInfo
{
public abstract short getNameIndex ( ) ;
}

public interface ConstantFieldRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantMethodRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantInterfaceMethodRef
{
public abstract short getClassIndex ( ) ;

public abstract short getNameAndTypeIndex ( ) ;
}

public interface ConstantString
{
public abstract short getStringIndex ( ) ;
}

public interface ConstantInteger
{
public abstract int getBytes ( ) ;
}

public interface ConstantFloat
{
public abstract int getBytes ( ) ;
}

public interface ConstantLong
{
public abstract int getHighBytes ( ) ;

public abstract int getLowBytes ( ) ;
}

public interface ConstantDouble
{
public abstract int getHighBytes ( ) ;

public abstract int getLowBytes ( ) ;
}

public interface ConstantNameAndType
{
public abstract short getNameIndex ( ) ;

public abstract short getDescriptorIndex ( ) ;
}

public interface ConstantUTF8
{
public abstract byte [ ] getBytes ( ) ;
}

public interface XMLWriter < T >
{
public abstract void write ( final T t , final org . w3c . dom . Document document , final org . w3c . dom . Node parent ) ;
}

public interface XMLReader < T >
{
public abstract T read ( final org . w3c . dom . Node node ) ;
}

private final class XMLClassUtilException extends java . lang . Exception
{
private XMLClassUtilException ( final java . lang . String message , final java . lang . Throwable cause )
{
super ( message , cause ) ;
}

private XMLClassUtilException ( final java . lang . String message )
{
super ( message ) ;
}

private XMLClassUtilException ( final java . lang . Throwable cause )
{
super ( cause ) ;
}
}

private final void error ( final java . lang . String message , final java . lang . Throwable cause ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( message , cause ) ;
throw ( error ) ;
}

private final void error ( final java . lang . Throwable cause ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( cause ) ;
throw ( error ) ;
}

private final void error ( final java . lang . String message ) throws XMLClassUtilException
{
final XMLClassUtilException error = new XMLClassUtilException ( message ) ;
throw ( error ) ;
}

private final Factory < ClassFile , java . io . DataInputStream , XMLClassUtilException > dataInputStreamFactory = new Factory < ClassFile , java . io . DataInputStream , XMLClassUtilException > ( )
{
public final ClassFile make ( final java . io . DataInputStream input ) throws XMLClassUtilException
{
ClassFile classFile = null ;
try
{
classFile = this . tryMake ( input ) ;
}
catch ( final java . io . IOException cause )
{
error ( cause ) ;
}
catch ( final java . lang . Exception cause )
{
error ( cause ) ;
}
return ( classFile ) ;
}

private final ClassFile tryMake ( final java . io . DataInputStream input ) throws java . io . IOException , XMLClassUtilException , java . lang . Exception
{
final int [ ] floor = new int [ 1 ] ;
floor [ 0 ] = 1 ;
final class AbstractListFactory < R > implements Factory < java . util . List < R > , java . io . DataInputStream , java . lang . Exception >
{
private AbstractListFactory ( final Factory < R , java . io . DataInputStream , java . lang . Exception > rFactory )
{
super ( ) ;
this . rFactory = rFactory ;
}

private Factory < R , java . io . DataInputStream , java . lang . Exception > rFactory ;

public final java . util . List < R > make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final short size = u2 ( input ) ;
final java . util . List < R > list = new java . util . ArrayList < R > ( ) ;
final int f = floor [ 0 ] ;
for ( int i = f ; i < size ; i ++ )
{
final R r = rFactory . make ( input ) ;
list . add ( r ) ;
}
return ( list ) ;
}
}
final Factory < ConstantPool , java . io . DataInputStream , java . lang . Exception > constantPoolFactory = new Factory < ConstantPool , java . io . DataInputStream , java . lang . Exception > ( )
{
public final ConstantPool make ( final java . io . DataInputStream input ) throws java . io . IOException , XMLClassUtilException
{
final byte tag = u1 ( input ) ;
// java . lang . System . out . print ( "\t" + tag ) ;
byte [ ] b = null ;
switch ( tag )
{
case 7 :
case 8 :
b = new byte [ 2 ] ;
break ;
case 9 :
case 10 :
case 11 :
case 3 :
case 4 :
case 12 :
b = new byte [ 4 ] ;
break ;
case 5 :
case 6 :
b = new byte [ 8 ] ;
break ;
case 1 :
short size = u2 ( input ) ;
b = new byte [ size ] ;
break ;
default :
error ( "Undefined Tag (" + tag + ")" ) ;
break ;
} ;
input . read ( b ) ;
final byte [ ] info = b ;
final ConstantPool cp = new ConstantPool ( )
{
public final byte getTag ( )
{
return ( tag ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( cp ) ;
}
} ;
final Factory < java . util . List < ConstantPool > , java . io . DataInputStream , java . lang . Exception > constantPoolListFactory = new AbstractListFactory < ConstantPool > ( constantPoolFactory ) ;
final int magic = u4 ( input ) ;
if ( magic != CAFEBABE )
{
error ( "Incorrect Magic Number (" + magic + ")" ) ;
}
final short minorVersion = u2 ( input ) ;
final short majorVersion = u2 ( input ) ;
final java . util . List < ConstantPool > constantPool = constantPoolListFactory . make ( input ) ;
floor [ 0 ] = 0 ;
final Factory < AttributeInfo , java . io . DataInputStream , java . lang . Exception > attributeInfoFactory = new Factory < AttributeInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final AttributeInfo make ( final java . io . DataInputStream input ) throws java . io . IOException
{
final short attributeNameIndex = u2 ( input ) ;
final int attributeLength = u4 ( input ) ;
final byte [ ] info = bytes ( input , attributeLength ) ;
// java . lang . System . out . println ( attributeNameIndex + "\t" + attributeLength ) ;
final AttributeInfo attributeInfo = new AttributeInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAttributeNameIndex ( )
{
return ( attributeNameIndex ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( attributeInfo ) ;
}
} ;
final Factory < java . util . List < AttributeInfo > , java . io . DataInputStream , java . lang . Exception > attributeInfoListFactory = new AbstractListFactory < AttributeInfo > ( attributeInfoFactory ) ;
final Factory < FMInfo , java . io . DataInputStream , java . lang . Exception > fmInfoFactory = new Factory < FMInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final FMInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final short accessFlags = u2 ( input ) ;
final short nameIndex = u2 ( input ) ;
final short descriptorIndex = u2 ( input ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListFactory . make ( input ) ;
// java . lang . System . out . println ( accessFlags + "\t" + nameIndex + "\t" + descriptorIndex ) ;
final FMInfo fmInfo = new FMInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getNameIndex ( )
{
return ( nameIndex ) ;
}

public final short getDescriptorIndex ( )
{
return ( descriptorIndex ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( fmInfo ) ;
}
} ;
final Factory < FieldInfo , java . io . DataInputStream , java . lang . Exception > fieldInfoFactory = new Factory < FieldInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final FieldInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final FMInfo r = fmInfoFactory . make ( input ) ;
return ( r ) ;
}
} ;
final Factory < java . util . List < FieldInfo > , java . io . DataInputStream , java . lang . Exception > fieldInfoListFactory = new AbstractListFactory < FieldInfo > ( fieldInfoFactory ) ;
final Factory < MethodInfo , java . io . DataInputStream , java . lang . Exception > methodInfoFactory = new Factory < MethodInfo , java . io . DataInputStream , java . lang . Exception > ( )
{
public final MethodInfo make ( final java . io . DataInputStream input ) throws java . lang . Exception
{
final FMInfo r = fmInfoFactory . make ( input ) ;
return ( r ) ;
}
} ;
final Factory < java . util . List < MethodInfo > , java . io . DataInputStream , java . lang . Exception > methodInfoListFactory = new AbstractListFactory < MethodInfo > ( methodInfoFactory ) ;
final short accessFlags = u2 ( input ) ;
final short thisClass = u2 ( input ) ;
final short superClass = u2 ( input ) ;
final short interfacesCount = u2 ( input ) ;
final short [ ] interfaces = new short [ interfacesCount ] ;
for ( int i = 1 ; i < interfacesCount ; i ++ )
{
interfaces [ i ] = u2 ( input ) ;
}
final java . util . List < FieldInfo > fields = fieldInfoListFactory . make ( input ) ;
final java . util . List < MethodInfo > methods = methodInfoListFactory . make ( input ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListFactory . make ( input ) ;
final ClassFile classFile = new ClassFile ( )
{
public final int getMagic ( )
{
return ( magic ) ;
}

public final short getMinorVersion ( )
{
return ( minorVersion ) ;
}

public final short getMajorVersion ( )
{
return ( majorVersion ) ;
}

public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getThisClass ( )
{
return ( thisClass ) ;
}

public final short getSuperClass ( )
{
return ( superClass ) ;
}

public final short [ ] getInterfaces ( )
{
return ( interfaces ) ;
}

public final java . util . List < FieldInfo > getFields ( )
{
return ( fields ) ;
}

public final java . util . List < MethodInfo > getMethods ( )
{
return ( methods ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( classFile ) ;
}
} ;

private final byte u1 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readByte ( ) ;
final byte b = ( byte ) ( i ) ;
// java . lang . System . out . print ( "\t" + b ) ;
return ( b ) ;
}

private final short u2 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readShort ( ) ;
final short s = ( short ) ( i ) ;
java . lang . System . out . print ( "\t" + s ) ;
return ( s ) ;
}

private final int u4 ( final java . io . DataInputStream input ) throws java . io . IOException
{
final int i = input . readInt ( ) ;
return ( i ) ;
}

private final byte [ ] bytes ( final java . io . DataInputStream input , final int len ) throws java . io . IOException
{
final byte [ ] b = new byte [ len ] ;
input . read ( b ) ;
return ( b ) ;
}

private final ByteWriter < ClassFile , java . io . IOException > byteWriter = new ByteWriter < ClassFile , java . io . IOException > ( )
{
public final void write ( final ClassFile classFile , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short [ ] floor = new short [ 1 ] ;
floor [ 0 ] = 1 ;
final class AbstractListWriter < R > implements ByteWriter < java . util . List < R > , java . io . IOException >
{
AbstractListWriter ( final ByteWriter < R , java . io . IOException > rWriter )
{
super ( ) ;
this . rWriter = rWriter ;
}

private ByteWriter < R , java . io . IOException > rWriter ;

public final void write ( final java . util . List < R > list , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short count = ( short ) ( list . size ( ) + floor [ 0 ] ) ;
java . lang . System . out . print ( "{" ) ;
u2 ( output , count ) ;
java . lang . System . out . print ( "}" ) ;
final java . util . Iterator < R > iterator = list . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
final R r = iterator . next ( ) ;
rWriter . write ( r , output ) ;
}
}
}
final ByteWriter < ConstantPool , java . io . IOException > constantPoolWriter = new ByteWriter < ConstantPool , java . io . IOException > ( )
{
public final void write ( final ConstantPool cp , final java . io . DataOutputStream output ) throws java . io . IOException
{
final byte tag = cp . getTag ( ) ;
// java . lang . System . out . print ( "\t" + tag ) ;
final byte [ ] info = cp . getInfo ( ) ;
u1 ( output , tag ) ;
if ( tag == 1 )
{
bytes ( output , info , 2 ) ;
}
else
{
bytes ( output , info , 0 ) ;
}
}
} ;
final ByteWriter < java . util . List < ConstantPool > , java . io . IOException > constantPoolListWriter = new AbstractListWriter < ConstantPool > ( constantPoolWriter ) ;
final ByteWriter < AttributeInfo , java . io . IOException > attributeInfoWriter = new ByteWriter < AttributeInfo , java . io . IOException > ( )
{
public final void write ( final AttributeInfo attributeInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short attributeNameIndex = attributeInfo . getAttributeNameIndex ( ) ;
final byte [ ] info = attributeInfo . getInfo ( ) ;
// java . lang . System . out . println ( attributeNameIndex + "\t" + info . length ) ;
u2 ( output , attributeNameIndex ) ;
bytes ( output , info , 4 ) ;
}
} ;
final ByteWriter < java . util . List < AttributeInfo > , java . io . IOException > attributeInfoListWriter = new AbstractListWriter < AttributeInfo > ( attributeInfoWriter ) ;
final ByteWriter < FieldMethodInfo , java . io . IOException > fmInfoWriter = new ByteWriter < FieldMethodInfo , java . io . IOException > ( )
{
public final void write ( final FieldMethodInfo fmInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
final short accessFlags = fmInfo . getAccessFlags ( ) ;
final short nameIndex = fmInfo . getNameIndex ( ) ;
final short descriptorIndex = fmInfo . getDescriptorIndex ( ) ;
// java . lang . System . out . println ( accessFlags + "\t" + nameIndex + "\t" + descriptorIndex ) ;
final java . util . List < AttributeInfo > attributes = fmInfo . getAttributes ( ) ;
java . lang . System . out . print ( "[" ) ;
u2 ( output , accessFlags ) ;
u2 ( output , nameIndex ) ;
u2 ( output , descriptorIndex ) ;
attributeInfoListWriter . write ( attributes , output ) ;
java . lang . System . out . print ( "]" ) ;
}
} ;
final ByteWriter < FieldInfo , java . io . IOException > fieldInfoWriter = new ByteWriter < FieldInfo , java . io . IOException > ( )
{
public final void write ( final FieldInfo fieldInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
fmInfoWriter . write ( fieldInfo , output ) ;
}
} ;
final ByteWriter < java . util . List < FieldInfo > , java . io . IOException > fieldInfoListWriter = new AbstractListWriter < FieldInfo > ( fieldInfoWriter ) ;
final ByteWriter < MethodInfo , java . io . IOException > methodInfoWriter = new ByteWriter < MethodInfo , java . io . IOException > ( )
{
public final void write ( final MethodInfo methodInfo , final java . io . DataOutputStream output ) throws java . io . IOException
{
fmInfoWriter . write ( methodInfo , output ) ;
}
} ;
final ByteWriter < java . util . List < MethodInfo > , java . io . IOException > methodInfoListWriter = new AbstractListWriter < MethodInfo > ( methodInfoWriter ) ;
final int magic = classFile . getMagic ( ) ;
final short minorVersion = classFile . getMinorVersion ( ) ;
final short majorVersion = classFile . getMajorVersion ( ) ;
final java . util . List < ConstantPool > constantPool = classFile . getConstantPool ( ) ;
final short accessFlags = classFile . getAccessFlags ( ) ;
final short thisClass = classFile . getThisClass ( ) ;
final short superClass = classFile . getSuperClass ( ) ;
final short [ ] interfaces = classFile . getInterfaces ( ) ;
final java . util . List < FieldInfo > fields = classFile . getFields ( ) ;
final java . util . List < MethodInfo > methods = classFile . getMethods ( ) ;
final java . util . List < AttributeInfo > attributes = classFile . getAttributes ( ) ;
u4 ( output , magic ) ;
u2 ( output , minorVersion ) ;
u2 ( output , majorVersion ) ;
constantPoolListWriter . write ( constantPool , output ) ;
floor [ 0 ] = 0 ;
u2 ( output , accessFlags ) ;
u2 ( output , thisClass ) ;
u2 ( output , superClass ) ;
final short interfacesCount = ( short ) ( interfaces . length ) ;
u2 ( output , interfacesCount ) ;
for ( int i = 0 ; i < interfaces . length ; i ++ )
{
u2 ( output , interfaces [ i ] ) ;
}
fieldInfoListWriter . write ( fields , output ) ;
methodInfoListWriter . write ( methods , output ) ;
attributeInfoListWriter . write ( attributes , output ) ;
}

private final void u1 ( final java . io . DataOutputStream output , final byte b ) throws java . io . IOException
{
output . writeByte ( b ) ;
// java . lang . System . out . print ( "\t" + b ) ;
}

private final void u2 ( final java . io . DataOutputStream output , final short s ) throws java . io . IOException
{
output . writeShort ( s ) ;
java . lang . System . out . print ( "\t" + s + "" ) ;
if ( s == 11 )
{
final java . lang . RuntimeException error = new java . lang . RuntimeException ( "FUCK" ) ;
// throw ( error ) ;
}
}

private final void u4 ( final java . io . DataOutputStream output , final int i ) throws java . io . IOException
{
output . writeInt ( i ) ;
}

private final void bytes ( final java . io . DataOutputStream output , final byte [ ] buff , final int size ) throws java . io . IOException
{
final int n = buff . length ;
byte b = -1 ;
short s = -1 ;
int i = -1 ;
switch ( size )
{
case 0 :
break ;
case 1 :
b = ( byte ) ( n ) ;
u1 ( output , b ) ;
break ;
case 2 :
s = ( short ) ( n ) ;
u2 ( output , s ) ;
break ;
case 4 :
i = ( int ) ( n ) ;
u4 ( output , i ) ;
break ;
case 100 :
i = ( int ) ( n + 1 ) ;
u4 ( output , i ) ;
break ;
}
output . write ( buff ) ;
}
} ;

private final XMLWriter < ClassFile > classFileWriter = new XMLWriter < ClassFile > ( )
{
public final void write ( final ClassFile classFile , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final class AbstractListWriter < R > implements XMLWriter < java . util . List < R > >
{
private AbstractListWriter ( final XMLWriter < R > rWriter )
{
super ( ) ;
this . rWriter = rWriter ;
}

private XMLWriter < R > rWriter ;

public final void write ( final java . util . List < R > list , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final java . util . Iterator < R > iterator = list . iterator ( ) ;
while ( iterator . hasNext ( ) )
{
final R r = iterator . next ( ) ;
rWriter . write ( r , document , parent ) ;
}
}
}
final XMLWriter < ConstantPool > constantPoolWriter = new XMLWriter < ConstantPool > ( )
{
public final void write ( ConstantPool cp , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( "constantPool" ) ;
final byte tag = cp . getTag ( ) ;
final byte [ ] info = cp . getInfo ( ) ;
u1 ( "tag" , tag , document , element ) ;
bytes ( info , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < java . util . List < ConstantPool > > constantPoolListWriter = new AbstractListWriter ( constantPoolWriter ) ;
final XMLWriter < AttributeInfo > attributeInfoWriter = new XMLWriter < AttributeInfo > ( )
{
public final void write ( AttributeInfo attributeInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( "attribute" ) ;
final short attributeNameIndex = attributeInfo . getAttributeNameIndex ( ) ;
final byte [ ] info = attributeInfo . getInfo ( ) ;
u2 ( "attributeNameIndex" , attributeNameIndex , document , element ) ;
bytes ( info , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < java . util . List < AttributeInfo > > attributeInfoListWriter = new AbstractListWriter ( attributeInfoWriter ) ;
final class FMWriter implements XMLWriter < FieldMethodInfo >
{
private FMWriter ( final java . lang . String name )
{
this . name = name ;
}

private java . lang . String name ;

public final void write ( FieldMethodInfo fmInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
final org . w3c . dom . Element element = document . createElement ( name ) ;
final short accessFlags = fmInfo . getAccessFlags ( ) ;
final short nameIndex = fmInfo . getNameIndex ( ) ;
final short descriptorIndex = fmInfo . getDescriptorIndex ( ) ;
final java . util . List < AttributeInfo > attributes = fmInfo . getAttributes ( ) ;
u2 ( "accessFlags" , accessFlags , document , element ) ;
u2 ( "nameIndex" , nameIndex , document , element ) ;
u2 ( "descriptorIndex" , descriptorIndex , document , element ) ;
attributeInfoListWriter . write ( attributes , document , element ) ;
parent . appendChild ( element ) ;
}
} ;
final XMLWriter < FieldMethodInfo > fieldInfoWriter1 = new FMWriter ( "field" ) ;
final XMLWriter < FieldInfo > fieldInfoWriter = new XMLWriter < FieldInfo > ( )
{
public final void write ( FieldInfo fieldInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
fieldInfoWriter1 . write ( fieldInfo , document , parent ) ;
}
} ;
final XMLWriter < java . util . List < FieldInfo > > fieldInfoListWriter = new AbstractListWriter ( fieldInfoWriter ) ;
final XMLWriter < FieldMethodInfo > methodInfoWriter1 = new FMWriter ( "field" ) ;
final XMLWriter < MethodInfo > methodInfoWriter = new XMLWriter < MethodInfo > ( )
{
public final void write ( MethodInfo methodInfo , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
methodInfoWriter1 . write ( methodInfo , document , parent ) ;
}
} ;
final XMLWriter < java . util . List < MethodInfo > > methodInfoListWriter = new AbstractListWriter ( methodInfoWriter ) ;
final int magic = classFile . getMagic ( ) ;
final short minorVersion = classFile . getMinorVersion ( ) ;
final short majorVersion = classFile . getMajorVersion ( ) ;
final java . util . List < ConstantPool > constantPool = classFile . getConstantPool ( ) ;
final short accessFlags = classFile . getAccessFlags ( ) ;
final short thisClass = classFile . getThisClass ( ) ;
final short superClass = classFile . getSuperClass ( ) ;
final short [ ] interfaces = classFile . getInterfaces ( ) ;
final java . util . List < FieldInfo > fields = classFile . getFields ( ) ;
final java . util . List < MethodInfo > methods = classFile . getMethods ( ) ;
final java . util . List < AttributeInfo > attributes = classFile . getAttributes ( ) ;
final org . w3c . dom . Element element = document . createElement ( "classFile" ) ;
u4 ( "magic" , magic , document , element ) ;
u2 ( "minorVersion" , minorVersion , document , element ) ;
u2 ( "majorVersion" , majorVersion , document , element ) ;
constantPoolListWriter . write ( constantPool , document , element ) ;
u2 ( "accessFlags" , accessFlags , document , element ) ;
u2 ( "thisClass" , thisClass , document , element ) ;
u2 ( "superClass" , superClass , document , element ) ;
for ( int i = 0 ; i < interfaces . length ; i ++ )
{
final org . w3c . dom . Element e = document . createElement ( "interface" ) ;
u2 ( "constantPoolIndex" , interfaces [ i ] , document , e ) ;
element . appendChild ( e ) ;
}
fieldInfoListWriter . write ( fields , document , element ) ;
methodInfoListWriter . write ( methods , document , element ) ;
attributeInfoListWriter . write ( attributes , document , element ) ;
parent . appendChild ( element ) ;
}

private final void u1 ( final java . lang . String name , final byte value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Byte . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void u2 ( final java . lang . String name , final short value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Short . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void u4 ( final java . lang . String name , final int value , final org . w3c . dom . Document document , final org . w3c . dom . Element element )
{
final org . w3c . dom . Attr attribute = document . createAttribute ( name ) ;
final java . lang . String v = java . lang . Integer . toString ( value ) ;
attribute . setNodeValue ( v ) ;
element . setAttributeNode ( attribute ) ;
}

private final void bytes ( final byte [ ] value , final org . w3c . dom . Document document , final org . w3c . dom . Node parent )
{
java . lang . String v = "" ;
if ( value . length > 0 )
{
v += value [ 0 ] ;
}
for ( int i = 1 ; i < value . length ; i ++ )
{
v += "," + value [ i ] ;
}
final org . w3c . dom . CDATASection cData = document . createCDATASection ( v ) ;
parent . appendChild ( cData ) ;
}
} ;

private final XMLReader < ClassFile > xmlReader = new XMLReader < ClassFile > ( )
{
public ClassFile read ( final org . w3c . dom . Node node )
{
class AbstractListReader < T > implements XMLReader < java . util . List < T > >
{
AbstractListReader ( final java . lang . String name , final XMLReader < T > tReader )
{
super ( ) ;
this . name = name ;
this . tReader = tReader ;
}

private java . lang . String name ;

private XMLReader < T > tReader ;

public final java . util . List < T > read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . Element element = ( org . w3c . dom . Element ) ( node ) ;
final java . util . List < T > list = this . read ( element ) ;
return ( list ) ;
}

private final java . util . List < T > read ( final org . w3c . dom . Element element )
{
final java . util . List < T > list = new java . util . ArrayList < T > ( ) ;
final org . w3c . dom . NodeList nodes = getElementsByTagName ( element , this . name ) ;
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
T t = this . tReader . read ( node ) ;
list . add ( t ) ;
}
return ( list ) ;
}

private final org . w3c . dom . NodeList getElementsByTagName ( final org . w3c . dom . Node node , final java . lang . String tag )
{
final org . w3c . dom . NodeList children = node . getChildNodes ( ) ;
final java . util . List < org . w3c . dom . Node > nodes = new java . util . ArrayList < org . w3c . dom . Node > ( ) ;
for ( int i = 0 ; i < children . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node child = children . item ( i ) ;
final java . lang . String name = child . getNodeName ( ) ;
if ( tag . equals ( name ) )
{
nodes . add ( child ) ;
}
}
final org . w3c . dom . NodeList elements = new org . w3c . dom . NodeList ( )
{
public final int getLength ( )
{
final int length = nodes . size ( ) ;
return ( length ) ;
}

public final org . w3c . dom . Node item ( final int index )
{
final org . w3c . dom . Node item = nodes . get ( index ) ;
return ( item ) ;
}
} ;
return ( elements ) ;
}
}
final XMLReader < ConstantPool > constantPoolReader = new XMLReader ( )
{
public final ConstantPool read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . Element element = ( org . w3c . dom . Element ) ( node ) ;
final ConstantPool cp = this . read ( element ) ;
return ( cp ) ;
}

private final ConstantPool read ( final org . w3c . dom . Element element )
{
final org . w3c . dom . NamedNodeMap attr = element . getAttributes ( ) ;
final byte tag = u1 ( attr , "tag" ) ;
final byte [ ] info = bytes ( element ) ;
final ConstantPool cp = new ConstantPool ( )
{
public final byte getTag ( )
{
return ( tag ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( cp ) ;
}
} ;
final XMLReader < java . util . List < ConstantPool > > constantPoolListReader = new AbstractListReader ( "constantPool" , constantPoolReader ) ;
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final int magic = u4 ( attr , "magic" ) ;
final short minorVersion = u2 ( attr , "minorVersion" ) ;
final short majorVersion = u2 ( attr , "majorVersion" ) ;
final java . util . List < ConstantPool > constantPool = constantPoolListReader . read ( node ) ;
final XMLReader < AttributeInfo > attributeInfoReader = new XMLReader < AttributeInfo > ( )
{
public final AttributeInfo read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final short attributeNameIndex = u2 ( attr , "attributeNameIndex" ) ;
final byte [ ] info = bytes ( node ) ;
final AttributeInfo attributeInfo = new AttributeInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAttributeNameIndex ( )
{
return ( attributeNameIndex ) ;
}

public final byte [ ] getInfo ( )
{
return ( info ) ;
}
} ;
return ( attributeInfo ) ;
}
} ;
final XMLReader < java . util . List < AttributeInfo > > attributeInfoListReader = new AbstractListReader ( "attribute" , attributeInfoReader ) ;
final XMLReader < FMInfo > fmInfoReader = new XMLReader ( )
{
public final FMInfo read ( final org . w3c . dom . Node node )
{
final org . w3c . dom . NamedNodeMap attr = node . getAttributes ( ) ;
final short accessFlags = u2 ( attr , "accessFlags" ) ;
final short nameIndex = u2 ( attr , "nameIndex" ) ;
final short descriptorIndex = u2 ( attr , "descriptorIndex" ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListReader . read ( node ) ;
final FMInfo fmInfo = new FMInfo ( )
{
public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getNameIndex ( )
{
return ( nameIndex ) ;
}

public final short getDescriptorIndex ( )
{
return ( descriptorIndex ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( fmInfo ) ;
}
} ;
final XMLReader < FieldInfo > fieldInfoReader = new XMLReader < FieldInfo > ( )
{
public final FieldInfo read ( final org . w3c . dom . Node node )
{
final FieldInfo fieldInfo = fmInfoReader . read ( node ) ;
return ( fieldInfo ) ;
}
} ;
final XMLReader < java . util . List < FieldInfo > > fieldInfoListReader = new AbstractListReader ( "field" , fieldInfoReader ) ;
final XMLReader < MethodInfo > methodInfoReader = new XMLReader < MethodInfo > ( )
{
public final MethodInfo read ( final org . w3c . dom . Node node )
{
final MethodInfo methodInfo = fmInfoReader . read ( node ) ;
return ( methodInfo ) ;
}
} ;
final XMLReader < java . util . List < MethodInfo > > methodInfoListReader = new AbstractListReader ( "method" , methodInfoReader ) ;
final short accessFlags = u2 ( attr , "accessFlags" ) ;
final short thisClass = u2 ( attr , "thisClass" ) ;
final short superClass = u2 ( attr , "superClass" ) ;
final org . w3c . dom . NodeList interfaceList = ( ( org . w3c . dom . Element ) ( node ) ) . getElementsByTagName ( "interface" ) ;
final short [ ] interfaces = new short [ interfaceList . getLength ( ) ] ;
for ( int i = 0 ; i < interfaceList . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node intrfcNode = interfaceList . item ( i ) ;
final org . w3c . dom . NamedNodeMap intrfcAttr = intrfcNode . getAttributes ( ) ;
interfaces [ i ] = u2 ( intrfcAttr , "constantPoolIndex" ) ;
}
final java . util . List < FieldInfo > fields = fieldInfoListReader . read ( node ) ;
final java . util . List < MethodInfo > methods = methodInfoListReader . read ( node ) ;
final java . util . List < AttributeInfo > attributes = attributeInfoListReader . read ( node ) ;
final ClassFile classFile = new ClassFile ( )
{
public final int getMagic ( )
{
return ( magic ) ;
}

public final short getMinorVersion ( )
{
return ( minorVersion ) ;
}

public final short getMajorVersion ( )
{
return ( majorVersion ) ;
}

public final java . util . List < ConstantPool > getConstantPool ( )
{
return ( constantPool ) ;
}

public final short getAccessFlags ( )
{
return ( accessFlags ) ;
}

public final short getThisClass ( )
{
return ( thisClass ) ;
}

public final short getSuperClass ( )
{
return ( superClass ) ;
}

public final short [ ] getInterfaces ( )
{
return ( interfaces ) ;
}

public final java . util . List < FieldInfo > getFields ( )
{
return ( fields ) ;
}

public final java . util . List < MethodInfo > getMethods ( )
{
return ( methods ) ;
}

public final java . util . List < AttributeInfo > getAttributes ( )
{
return ( attributes ) ;
}
} ;
return ( classFile ) ;
}

private final byte u1 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final byte b = java . lang . Byte . parseByte ( v ) ;
return ( b ) ;
}

private final short u2 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final short b = java . lang . Short . parseShort ( v ) ;
return ( b ) ;
}

private final int u4 ( final org . w3c . dom . NamedNodeMap attributes , final java . lang . String key )
{
final org . w3c . dom . Node node = attributes . getNamedItem ( key ) ;
final java . lang . String v = node . getNodeValue ( ) ;
final int b = java . lang . Integer . parseInt ( v ) ;
return ( b ) ;
}

private final byte [ ] bytes ( final org . w3c . dom . Node node )
{
byte [ ] b = null ;
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
for ( int i = 0 ; i < childNodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node child = childNodes . item ( i ) ;
if ( child instanceof org . w3c . dom . CDATASection )
{
final java . lang . String string = child . getNodeValue ( ) ;
final java . util . StringTokenizer counter = new java . util . StringTokenizer ( string , "," , false ) ;
int count = 0 ;
for ( count = 0 ; counter . hasMoreTokens ( ) ; count ++ )
{
counter . nextToken ( ) ;
}
b = new byte [ count ] ;
final java . util . StringTokenizer tokenizer = new java . util . StringTokenizer ( string , "," , false ) ;
for ( int j = 0 ; j < count ; j ++ )
{
final java . lang . String token = tokenizer . nextToken ( ) ;
final byte t = java . lang . Byte . parseByte ( token ) ;
b [ j ] = t ;
}
}
}
return ( b ) ;
}
} ;

private final void print ( final java . io . PrintStream out , final org . w3c . dom . NodeList nodes )
{
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
this . print ( out , node ) ;
}
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . NamedNodeMap nodes )
{
for ( int i = 0 ; i < nodes . getLength ( ) ; i ++ )
{
final org . w3c . dom . Node node = nodes . item ( i ) ;
this . print ( out , node ) ;
}
}

private final java . lang . Object printer = new java . lang . Object ( )
{
} ;

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Node node )
{
if ( node instanceof org . w3c . dom . Document )
{
this . print ( out , ( org . w3c . dom . Document ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . Element )
{
this . print ( out , ( org . w3c . dom . Element ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . Attr )
{
this . print ( out , ( org . w3c . dom . Attr ) ( node ) ) ;
}
else if ( node instanceof org . w3c . dom . CDATASection )
{
this . print ( out , ( org . w3c . dom . CDATASection ) ( node ) ) ;
}
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Document node )
{
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
this . print ( out , childNodes ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Element node )
{
final java . lang . String tagName = node . getTagName ( ) ;
out . print ( "<" + tagName ) ;
final org . w3c . dom . NamedNodeMap namedNodeMap = node . getAttributes ( ) ;
print ( out , namedNodeMap ) ;
out . println ( ">" ) ;
final org . w3c . dom . NodeList childNodes = node . getChildNodes ( ) ;
this . print ( out , childNodes ) ;
out . println ( "</" + tagName + ">" ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . Attr node )
{
final java . lang . String name = node . getName ( ) ;
final java . lang . String value = node . getValue ( ) ;
out . print ( " " + name + "=\"" + value + "\"" ) ;
}

private final void print ( final java . io . PrintStream out , final org . w3c . dom . CDATASection node )
{
final java . lang . String value = node . getNodeValue ( ) ;
out . println ( "<![CDATA[" + value + "]]>" ) ;
}
}

/*
* XMLClassUtil.java
*/
I have been working on this project for a little bit.
The program is still very rough.
This is an outline of what it is supposed to do.

You can use this program to read in a java class file.
It will read all the bytes and organize them into an XML file.
The program will print the XML file.

You can easily read the XML file.
You can make minor changes.

The program can also read in a xml file and write out bytes.

Project Goal:  to better understand the class file format.

Project Status:  the program works almost.
I have used HelloWorld.class as a simple test case.
I converted HelloWorld.class to HelloWorld.xml and reconverted HelloWorld.xml to HelloWorld.class.1.
HelloWorld.class is the same length as HelloWorld.class.1.
Most of the bytes are the same.
In fact, the first 70 some percent bytes exactly match.
At some point, if you shift, then they would exactly match.
(Shifting=put x byte at end, x+1 byte at x, x+2 byte at x+1, ...., end byte at end-1.)

No comments: