jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Hovemeyer <dav...@cs.umd.edu>
Subject problem with ObjectType class
Date Fri, 25 Oct 2002 20:23:42 GMT
Howdy,

I think there is a problem with the org.apache.bcel.generic.ObjectType
class.  Specifically, its constructor takes the class name as its
only argument.  However, a class name is ambigous, since we don't
know a priori which part of the name corresponds to the package
the class is in.

For example, "A.B.C" might be class "C" in package "A.B", or it
might be class "B.C" in package "A".  The current implementation
of ObjectType only allows the former.

An easy solution would be a constructor which takes both package and
class name, where the package name must be a prefix of the class
name.  It could look something like:

----snip----8<----snip----8<----snip----8<----snip----8<----
  /**
   * This constructor is useful for inner classes, since it
   * explicitly disambiguates which part of the class name
   * corresponds to the package.
   *
   * @param class_name fully qualified class name, e.g. java.lang.String
   * @param package_name name of package, e.g. java.lang
   */ 
  public ObjectType(String package_name, String class_name) {
    super(Constants.T_REFERENCE,
	  classAndPackageNameToSignature(package_name, class_name));
    this.class_name = class_name;
  }

  /**
   * Convert package and class name to Java signature.
   * @param class_name fully qualified class name, e.g. java.lang.String
   * @param package_name name of package, e.g. java.lang
   * @return the Java signature, e.g. Ljava/lang/String;
   */
  private static String classAndPackageNameToSignature(
    String package_name, String class_name) {
  
    if (!class_name.startsWith(package_name))
      throw new IllegalArgumentException(class_name +
        " is not part of package " + package_name);
  
    StringBuffer buf = new StringBuffer();
    buf.append('L');
    buf.append(package_name.replace('.', '/');
    if (!package_name.equals(""))
      buf.append('/');
    String shortClassName = class_name.substring(package_name.length());
    buf.append(shortClassName.replace('.', '$');
    buf.append(';');
    return buf.toString();
  }
----snip----8<----snip----8<----snip----8<----snip----8<----

(I haven't tested the code above, but I'm sure you get the idea.)

-Dave

--
To unsubscribe, e-mail:   <mailto:bcel-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:bcel-user-help@jakarta.apache.org>


Mime
View raw message