1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
|
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
*
* This program and the accompanying materials are made available under
* the terms of the Common Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/cpl-v10.html
*
* $Id: ClassDef.java,v 1.1.1.1.2.1 2004/07/16 23:32:30 vlad_r Exp $
*/
package com.vladium.jcd.cls;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import com.vladium.jcd.cls.attribute.AttributeElementFactory;
import com.vladium.jcd.cls.attribute.Attribute_info;
import com.vladium.jcd.cls.attribute.CodeAttribute_info;
import com.vladium.jcd.cls.attribute.InnerClassesAttribute_info;
import com.vladium.jcd.cls.constant.CONSTANT_Class_info;
import com.vladium.jcd.cls.constant.CONSTANT_Fieldref_info;
import com.vladium.jcd.cls.constant.CONSTANT_NameAndType_info;
import com.vladium.jcd.cls.constant.CONSTANT_String_info;
import com.vladium.jcd.cls.constant.CONSTANT_Utf8_info;
import com.vladium.jcd.compiler.IClassFormatOutput;
import com.vladium.jcd.lib.Types;
import com.vladium.jcd.lib.UDataOutputStream;
import com.vladium.util.ByteArrayOStream;
// ----------------------------------------------------------------------------
/**
* This class represents the abstract syntax table (AST) that {@link com.vladium.jcd.parser.ClassDefParser}
* produces from bytecode. Most elements are either settable or extendible.
* This class also implements {@link com.vladium.jcd.compiler.IClassFormatOutput}
* and works with {@link com.vladium.jcd.compiler.ClassWriter} to produce
* bytecode without an external compiler.<P>
*
* MT-safety: this class and all interfaces used by it are not safe for
* access from multiple concurrent threads.
*
* @author (C) 2001, Vlad Roubtsov
*/
public
final class ClassDef implements Cloneable, IAccessFlags, IClassFormatOutput
{
// public: ................................................................
public ClassDef ()
{
m_version = new int [2];
m_constants = ElementFactory.newConstantCollection (-1);
m_interfaces = ElementFactory.newInterfaceCollection (-1);
m_fields = ElementFactory.newFieldCollection (-1);
m_methods = ElementFactory.newMethodCollection (-1);
m_attributes = ElementFactory.newAttributeCollection (-1);
}
// Visitor:
public void accept (final IClassDefVisitor visitor, final Object ctx)
{
visitor.visit (this, ctx);
}
public long getMagic ()
{
return m_magic;
}
public void setMagic (final long magic)
{
m_magic = magic;
}
public int [] getVersion ()
{
return m_version;
}
public void setVersion (final int [] version)
{
m_version [0] = version [0];
m_version [1] = version [1];
}
public final void setDeclaredSUID (final long suid)
{
m_declaredSUID = suid;
}
public int getThisClassIndex ()
{
return m_this_class_index;
}
public void setThisClassIndex (final int this_class_index)
{
m_this_class_index = this_class_index;
}
public CONSTANT_Class_info getThisClass ()
{
return (CONSTANT_Class_info) m_constants.get (m_this_class_index);
}
public CONSTANT_Class_info getSuperClass ()
{
return (CONSTANT_Class_info) m_constants.get (m_super_class_index);
}
public String getName ()
{
return getThisClass ().getName (this);
}
public int getSuperClassIndex ()
{
return m_super_class_index;
}
public void setSuperClassIndex (final int super_class_index)
{
m_super_class_index = super_class_index;
}
// IAccessFlags:
public final int getAccessFlags ()
{
return m_access_flags;
}
public final void setAccessFlags (final int flags)
{
m_access_flags = flags;
}
public boolean isInterface ()
{
return (m_access_flags & ACC_INTERFACE) != 0;
}
public boolean isSynthetic ()
{
return m_attributes.hasSynthetic ();
}
public boolean isNested (final int [] nestedAccessFlags)
{
final InnerClassesAttribute_info innerClassesAttribute = m_attributes.getInnerClassesAttribute ();
if (innerClassesAttribute == null)
return false;
else
return innerClassesAttribute.makesClassNested (m_this_class_index, nestedAccessFlags);
}
// methods for getting various nested tables:
public IConstantCollection getConstants ()
{
return m_constants;
}
public IInterfaceCollection getInterfaces ()
{
return m_interfaces;
}
public IFieldCollection getFields ()
{
return m_fields;
}
public IMethodCollection getMethods ()
{
return m_methods;
}
public IAttributeCollection getAttributes ()
{
return m_attributes;
}
public int [] getFields (final String name)
{
return m_fields.get (this, name);
}
public int [] getMethods (final String name)
{
return m_methods.get (this, name);
}
// Cloneable:
/**
* Performs a deep copy.
*/
public Object clone ()
{
try
{
final ClassDef _clone = (ClassDef) super.clone ();
// do deep copy:
_clone.m_version = (int []) m_version.clone ();
_clone.m_constants = (IConstantCollection) m_constants.clone ();
_clone.m_interfaces = (IInterfaceCollection) m_interfaces.clone ();
_clone.m_fields = (IFieldCollection) m_fields.clone ();
_clone.m_methods = (IMethodCollection) m_methods.clone ();
_clone.m_attributes = (IAttributeCollection) m_attributes.clone ();
return _clone;
}
catch (CloneNotSupportedException e)
{
throw new InternalError (e.toString ());
}
}
// IClassFormatOutput:
public void writeInClassFormat (final UDataOutputStream out) throws IOException
{
if (out == null) throw new IllegalArgumentException ("null input: out");
out.writeU4 (m_magic);
out.writeU2 (m_version [1]);
out.writeU2 (m_version [0]);
m_constants.writeInClassFormat (out);
out.writeU2 (m_access_flags);
out.writeU2 (m_this_class_index);
out.writeU2 (m_super_class_index);
m_interfaces.writeInClassFormat (out);
m_fields.writeInClassFormat (out);
m_methods.writeInClassFormat (out);
m_attributes.writeInClassFormat (out);
}
public final long getDeclaredSUID ()
{
return m_declaredSUID;
}
/**
* This follows the spec at http://java.sun.com/j2se/1.4.1/docs/guide/serialization/spec/class.doc6.html#4100
* as well as undocumented hacks used by Sun's 1.4.2 J2SDK
*/
public final long computeSUID (final boolean skipCLINIT)
{
long result = m_declaredSUID;
if (result != 0L)
return result;
else
{
try
{
final ByteArrayOStream bout = new ByteArrayOStream (1024); // TODO: reuse these
final DataOutputStream dout = new DataOutputStream (bout);
// (1) The class name written using UTF encoding:
dout.writeUTF (Types.vmNameToJavaName (getName ())); // [in Java format]
// (2) The class modifiers written as a 32-bit integer:
// ACC_STATIC is never written for nested classes/interfaces;
// however, ACC_SUPER must be ignored:
{
// this is tricky: for static/non-static nested classes that
// were declared protected in the source the usual access flags
// will have ACC_PUBLIC set; the only way to achieve J2SDK
// compatibility is to recover the source access flags
// from the InnerClasses attribute:
final int [] nestedAccessFlags = new int [1];
final int modifiers = (isNested (nestedAccessFlags)
? nestedAccessFlags [0]
: getAccessFlags ())
& (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT);
// if/when emma decides to instrument interfaces for <clinit>
// coverage, compensate for javac bug in which ABSTRACT bit
// was set for an interface only if the interface declared methods
// [Sun's J2SDK]:
dout.writeInt (modifiers);
}
// not doing another J2SDK compensation for arrays, because
// we never load/instrument those
// (3) The name of each interface sorted by name written using UTF encoding
{
final IInterfaceCollection interfaces = getInterfaces ();
final String [] ifcs = new String [interfaces.size ()];
final int iLimit = ifcs.length;
for (int i = 0; i < iLimit; ++ i)
{
// [in Java format]
ifcs [i] = Types.vmNameToJavaName (((CONSTANT_Class_info) m_constants.get (interfaces.get (i))).getName (this));
}
Arrays.sort (ifcs);
for (int i = 0; i < iLimit; ++ i)
{
dout.writeUTF (ifcs [i]);
}
}
// (4) For each field of the class sorted by field name (except
// private static and private transient fields):
// a. The name of the field in UTF encoding.
// b. The modifiers of the field written as a 32-bit integer.
// c. The descriptor of the field in UTF encoding
{
final IFieldCollection fields = getFields ();
final FieldDescriptor [] fds = new FieldDescriptor [fields.size ()];
int fcount = 0;
for (int f = 0, fLimit = fds.length; f < fLimit; ++ f)
{
final Field_info field = fields.get (f);
final int modifiers = field.getAccessFlags ();
if (((modifiers & ACC_PRIVATE) == 0) ||
((modifiers & (ACC_STATIC | ACC_TRANSIENT)) == 0))
fds [fcount ++] = new FieldDescriptor (field.getName (this), modifiers, field.getDescriptor (this));
}
if (fcount > 0)
{
Arrays.sort (fds, 0, fcount);
for (int i = 0; i < fcount; ++ i)
{
final FieldDescriptor fd = fds [i];
dout.writeUTF (fd.m_name);
dout.writeInt (fd.m_modifiers);
dout.writeUTF (fd.m_descriptor);
}
}
}
// (5) If a class initializer exists, write out the following:
// a. The name of the method, <clinit>, in UTF encoding.
// b. The modifier of the method, ACC_STATIC, written as a 32-bit integer.
// c. The descriptor of the method, ()V, in UTF encoding.
// (6) For each non-private constructor sorted by method name and signature:
// a. The name of the method, <init>, in UTF encoding.
// b. The modifiers of the method written as a 32-bit integer.
// c. The descriptor of the method in UTF encoding.
// (7) For each non-private method sorted by method name and signature:
// a. The name of the method in UTF encoding.
// b. The modifiers of the method written as a 32-bit integer.
// c. The descriptor of the method in UTF encoding.
// note: although this is not documented, J2SDK code uses '.''s as
// descriptor separators (this is done for methods only, not for fields)
{
final IMethodCollection methods = getMethods ();
boolean hasCLINIT = false;
final ConstructorDescriptor [] cds = new ConstructorDescriptor [methods.size ()];
final MethodDescriptor [] mds = new MethodDescriptor [cds.length];
int ccount = 0, mcount = 0;
for (int i = 0, iLimit = cds.length; i < iLimit; ++ i)
{
final Method_info method = methods.get (i);
final String name = method.getName (this);
if (! hasCLINIT && IClassDefConstants.CLINIT_NAME.equals (name))
{
hasCLINIT = true;
continue;
}
else
{
final int modifiers = method.getAccessFlags ();
if ((modifiers & ACC_PRIVATE) == 0)
{
if (IClassDefConstants.INIT_NAME.equals (name))
cds [ccount ++] = new ConstructorDescriptor (modifiers, method.getDescriptor (this));
else
mds [mcount ++] = new MethodDescriptor (name, modifiers, method.getDescriptor (this));
}
}
}
if (hasCLINIT && ! skipCLINIT)
{
dout.writeUTF (IClassDefConstants.CLINIT_NAME);
dout.writeInt (ACC_STATIC);
dout.writeUTF (IClassDefConstants.CLINIT_DESCRIPTOR);
}
if (ccount > 0)
{
Arrays.sort (cds, 0, ccount);
for (int i = 0; i < ccount; ++ i)
{
final ConstructorDescriptor cd = cds [i];
dout.writeUTF (IClassDefConstants.INIT_NAME);
dout.writeInt (cd.m_modifiers);
dout.writeUTF (cd.m_descriptor.replace ('/', '.'));
}
}
if (mcount > 0)
{
Arrays.sort (mds, 0, mcount);
for (int i = 0; i < mcount; ++ i)
{
final MethodDescriptor md = mds [i];
dout.writeUTF (md.m_name);
dout.writeInt (md.m_modifiers);
dout.writeUTF (md.m_descriptor.replace ('/', '.'));
}
}
}
dout.flush();
if (DEBUG_SUID)
{
byte [] dump = bout.copyByteArray ();
for (int x = 0; x < dump.length; ++ x)
{
System.out.println ("DUMP[" + x + "] = " + dump [x] + "\t" + (char) dump[x]);
}
}
final MessageDigest md = MessageDigest.getInstance ("SHA");
md.update (bout.getByteArray (), 0, bout.size ());
final byte [] hash = md.digest ();
if (DEBUG_SUID)
{
for (int x = 0; x < hash.length; ++ x)
{
System.out.println ("HASH[" + x + "] = " + hash [x]);
}
}
// final int hash0 = hash [0];
// final int hash1 = hash [1];
// result = ((hash0 >>> 24) & 0xFF) | ((hash0 >>> 16) & 0xFF) << 8 | ((hash0 >>> 8) & 0xFF) << 16 | ((hash0 >>> 0) & 0xFF) << 24 |
// ((hash1 >>> 24) & 0xFF) << 32 | ((hash1 >>> 16) & 0xFF) << 40 | ((hash1 >>> 8) & 0xFF) << 48 | ((hash1 >>> 0) & 0xFF) << 56;
for (int i = Math.min (hash.length, 8) - 1; i >= 0; -- i)
{
result = (result << 8) | (hash [i] & 0xFF);
}
return result;
}
catch (IOException ioe)
{
throw new Error (ioe.getMessage ());
}
catch (NoSuchAlgorithmException nsae)
{
throw new SecurityException (nsae.getMessage());
}
}
}
public int addCONSTANT_Utf8 (final String value, final boolean keepUnique)
{
if (keepUnique)
{
final int existing = m_constants.findCONSTANT_Utf8 (value);
if (existing > 0)
{
return existing;
}
// [else fall through]
}
return m_constants.add (new CONSTANT_Utf8_info (value));
}
public int addStringConstant (final String value)
{
final int value_index = addCONSTANT_Utf8 (value, true);
// TODO: const uniqueness
return m_constants.add (new CONSTANT_String_info (value_index));
}
public int addNameType (final String name, final String typeDescriptor)
{
final int name_index = addCONSTANT_Utf8 (name, true);
final int descriptor_index = addCONSTANT_Utf8 (typeDescriptor, true);
return m_constants.add (new CONSTANT_NameAndType_info (name_index, descriptor_index));
}
public int addClassref (final String classJVMName)
{
final int name_index = addCONSTANT_Utf8 (classJVMName, true);
// TODO: this should do uniqueness checking:
return m_constants.add (new CONSTANT_Class_info (name_index));
}
/**
* Adds a new declared field to this class [with no attributes]
*/
public int addField (final String name, final String descriptor, final int access_flags)
{
// TODO: support Fields with initializer attributes?
// TODO: no "already exists" check done here
final int name_index = addCONSTANT_Utf8 (name, true);
final int descriptor_index = addCONSTANT_Utf8 (descriptor, true);
final Field_info field = new Field_info (access_flags, name_index, descriptor_index,
ElementFactory.newAttributeCollection (0));
return m_fields.add (field);
}
/**
* Adds a new declared field to this class [with given attributes]
*/
public int addField (final String name, final String descriptor, final int access_flags,
final IAttributeCollection attributes)
{
// TODO: support Fields with initializer attributes?
// TODO: no "already exists" check done here
final int name_index = addCONSTANT_Utf8 (name, true);
final int descriptor_index = addCONSTANT_Utf8 (descriptor, true);
final Field_info field = new Field_info (access_flags, name_index, descriptor_index, attributes);
return m_fields.add (field);
}
// TODO: rework this API
public Method_info newEmptyMethod (final String name, final String descriptor, final int access_flags)
{
// TODO: flag for making synthetic etc
final int attribute_name_index = addCONSTANT_Utf8 (Attribute_info.ATTRIBUTE_CODE, true);
final int name_index = addCONSTANT_Utf8 (name, true);
final int descriptor_index = addCONSTANT_Utf8 (descriptor, true);
final IAttributeCollection attributes = ElementFactory.newAttributeCollection (0);
final CodeAttribute_info code = new CodeAttribute_info (attribute_name_index, 0, 0,
CodeAttribute_info.EMPTY_BYTE_ARRAY,
AttributeElementFactory.newExceptionHandlerTable (0),
ElementFactory.newAttributeCollection (0));
attributes.add (code);
final Method_info method = new Method_info (access_flags, name_index, descriptor_index, attributes);
return method;
}
public int addMethod (final Method_info method)
{
return m_methods.add (method);
}
/**
* Adds a reference to a field declared by this class.
*
* @return constant pool index of the reference
*/
public int addFieldref (final Field_info field)
{
// TODO: keepUnique flag
final CONSTANT_NameAndType_info nametype = new CONSTANT_NameAndType_info (field.m_name_index, field.m_descriptor_index);
final int nametype_index = m_constants.add (nametype); // TODO: unique logic
return m_constants.add (new CONSTANT_Fieldref_info (m_this_class_index, nametype_index));
}
/**
* Adds a reference to a field declared by this class.
*
* @return constant pool index of the reference
*/
public int addFieldref (final int offset)
{
// TODO: keepUnique flag
final Field_info field = m_fields.get (offset);
final CONSTANT_NameAndType_info nametype = new CONSTANT_NameAndType_info (field.m_name_index, field.m_descriptor_index);
final int nametype_index = m_constants.add (nametype); // TODO: unique logic
return m_constants.add (new CONSTANT_Fieldref_info (m_this_class_index, nametype_index));
}
// protected: .............................................................
// package: ...............................................................
// private: ...............................................................
private static final class FieldDescriptor implements Comparable
{
// Comparable:
public final int compareTo (final Object obj)
{
return m_name.compareTo (((FieldDescriptor) obj).m_name);
}
FieldDescriptor (final String name, final int modifiers, final String descriptor)
{
m_name = name;
m_modifiers = modifiers;
m_descriptor = descriptor;
}
final String m_name;
final int m_modifiers;
final String m_descriptor;
} // end of nested class
private static final class ConstructorDescriptor implements Comparable
{
// Comparable:
public final int compareTo (final Object obj)
{
return m_descriptor.compareTo (((ConstructorDescriptor) obj).m_descriptor);
}
ConstructorDescriptor (final int modifiers, final String descriptor)
{
m_modifiers = modifiers;
m_descriptor = descriptor;
}
final int m_modifiers;
final String m_descriptor;
} // end of nested class
private static final class MethodDescriptor implements Comparable
{
// Comparable:
public final int compareTo (final Object obj)
{
final MethodDescriptor rhs = (MethodDescriptor) obj;
int result = m_name.compareTo (rhs.m_name);
if (result == 0)
result = m_descriptor.compareTo (rhs.m_descriptor);
return result;
}
MethodDescriptor (final String name, final int modifiers, final String descriptor)
{
m_name = name;
m_modifiers = modifiers;
m_descriptor = descriptor;
}
final String m_name;
final int m_modifiers;
final String m_descriptor;
} // end of nested class
// TODO: final fields
private long m_magic;
private int [] /* major, minor */ m_version;
private int m_access_flags;
private int m_this_class_index, m_super_class_index;
private IConstantCollection m_constants;
private IInterfaceCollection m_interfaces;
private IFieldCollection m_fields;
private IMethodCollection m_methods;
private IAttributeCollection m_attributes;
private long m_declaredSUID;
private static final boolean DEBUG_SUID = false;
} // end of class
// ----------------------------------------------------------------------------
|