modified Loader so that getPackage() works. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@32 30ef5769-5b8d-40dd-aea6-55b5d6557bb3tags/rel_3_17_1_ga
@@ -242,6 +242,8 @@ see javassist.Dump. | |||
<li>CtConstructor.setBody() now works for class initializers. | |||
<li>CtNewMethod.delegator() now works for static methods. | |||
<li>javassist.expr.Expr.indexOfBytecode() has been added. | |||
<li>javassist.Loader has been modified so that getPackage() returns | |||
a package object. | |||
</ul> | |||
<p>- version 2.5.1 in May, 2003. |
@@ -372,7 +372,7 @@ public class ClassPool { | |||
/** | |||
* Returns a <code>java.lang.Class</code> object that has been loaded | |||
* by <code>writeAsClass()</code>. Note that such a class cannot be | |||
* by <code>writeAsClass()</code>. Note that such an object cannot be | |||
* obtained by <code>java.lang.Class.forName()</code> because it has | |||
* been loaded by an internal class loader. | |||
* | |||
@@ -399,9 +399,20 @@ public class ClassPool { | |||
* loads only the classes explicitly specified by this method | |||
* <code>writeAsClass()</code>. The other classes are loaded | |||
* by the parent class loader (the sytem class loader) by delegation. | |||
* Thus, if a class <code>X</code> loaded by the internal class | |||
* loader refers to a class <code>Y</code>, then the class | |||
* <code>Y</code> is loaded by the parent class loader. | |||
* | |||
* <p>For example, | |||
* | |||
* <ul><pre>class Line { Point p1, p2; }</pre></ul> | |||
* | |||
* <p>If the class <code>Line</code> is loaded by the internal class | |||
* loader and the class <code>Point</code> has not been loaded yet, | |||
* then the class <code>Point</code> that the class <code>Line</code> | |||
* refers to is loaded by the parent class loader. There is no | |||
* chance of modifying the definition of <code>Point</code> with | |||
* Javassist. | |||
* | |||
* <p>The internal class loader is shared among all the instances | |||
* of <code>ClassPool</code>. | |||
* | |||
* @param classname a fully-qualified class name. | |||
* |
@@ -174,8 +174,10 @@ public abstract class CtClass { | |||
public boolean isModified() { return false; } | |||
/** | |||
* Returns true if the class has been loaded and thus it cannot be | |||
* modified any more. | |||
* Returns true if the class has been loaded or written out | |||
* and thus it cannot be modified any more. | |||
* | |||
* @see #defrost() | |||
*/ | |||
public boolean isFrozen() { return true; } | |||
@@ -189,12 +191,14 @@ public abstract class CtClass { | |||
} | |||
/** | |||
* Defrosts the class so that the class can be modified. | |||
* Defrosts the class so that the class can be modified again. | |||
* | |||
* To avoid changes that are never reflected, | |||
* To avoid changes that will be never reflected, | |||
* the class is frozen to be unmodifiable if it is loaded or | |||
* written out. This method should be called only in a case | |||
* that the class will be reloaded or written out later again. | |||
* | |||
* @see #isFrozen() | |||
*/ | |||
public void defrost() { | |||
throw new RuntimeException("cannot defrost " + getName()); | |||
@@ -753,6 +757,8 @@ public abstract class CtClass { | |||
* | |||
* <p>See the description of <code>ClassPool.writeAsClass()</code> | |||
* before you use this method. | |||
* This method is provided for convenience. If you need more | |||
* complex functionality, you should write your own class loader. | |||
* | |||
* @see javassist.ClassPool#writeAsClass(String) | |||
* @see javassist.ClassPool#forName(String) |
@@ -296,6 +296,20 @@ public class Loader extends ClassLoader { | |||
return null; | |||
} | |||
int i = name.lastIndexOf('.'); | |||
if (i != -1) { | |||
String pname = name.substring(0, i); | |||
if (getPackage(pname) == null) | |||
try { | |||
definePackage(pname, null, null, null, | |||
null, null, null, null); | |||
} | |||
catch (IllegalArgumentException e) { | |||
// ignore. maybe the package object for the same | |||
// name has been created just right away. | |||
} | |||
} | |||
return defineClass(name, classfile, 0, classfile.length); | |||
} | |||
@@ -343,4 +357,18 @@ public class Loader extends ClassLoader { | |||
else | |||
return findSystemClass(classname); | |||
} | |||
protected Package getPackage(String name) { | |||
return super.getPackage(name); | |||
} | |||
/* | |||
// Package p = super.getPackage(name); | |||
Package p = null; | |||
if (p == null) | |||
return definePackage(name, null, null, null, | |||
null, null, null, null); | |||
else | |||
return p; | |||
} | |||
*/ | |||
} |
@@ -35,6 +35,9 @@ public abstract class Expr implements Opcode { | |||
static final String javaLangObject = "java.lang.Object"; | |||
/** | |||
* Undocumented constructor. Do not use; internal-use only. | |||
*/ | |||
protected Expr(int pos, CodeIterator i, CtClass declaring, MethodInfo m) { | |||
currentPos = pos; | |||
iterator = i; |
@@ -23,6 +23,32 @@ the Java reflection API. <code>CtClass</code> provides | |||
definition. It allows to add a new field, constructor, and method. | |||
Instrumenting a method body is also possible. | |||
<p> | |||
Methods are represented by <code>CtMethod</code> objects. | |||
<code>CtMethod</code> provides several methods for modifying | |||
the definition of the method. Note that if a method is inherited | |||
from a super class, then | |||
the same <code>CtMethod</code> object | |||
that represents the inherited method represents the method declared | |||
in that super class. | |||
A <code>CtMethod</code> object corresponds to every method declaration. | |||
<p> | |||
For example, if class <code>Point</code> declares method <code>move()</code> | |||
and a subclass <code>ColorPoint</code> of <code>Point</code> does | |||
not override <code>move()</code>, the two <code>move()</code> methods | |||
declared in <code>Point</code> and inherited in <code>ColorPoint</code> | |||
are represented by the identical <code>CtMethod</code> object. | |||
If the method definition represented by this | |||
<code>CtMethod</code> object is modified, the modification is | |||
reflected on both the methods. | |||
If you want to modify only the <code>move()</code> method in | |||
<code>ColorPoint</code>, you first have to add to <code>ColorPoint</code> | |||
a copy of the <code>CtMethod</code> object representing <code>move()</code> | |||
in <code>Point</code>. A copy of the the <code>CtMethod</code> object | |||
can be obtained by <code>CtNewMethod.copy()</code>. | |||
<p><hr width="40%"> | |||
<ul> |