Skip to content

Commit

Permalink
upgrade javassist to 3.30.2-GA
Browse files Browse the repository at this point in the history
  • Loading branch information
shibin authored and skybber committed Jan 14, 2025
1 parent ecf6628 commit 7c57e2e
Show file tree
Hide file tree
Showing 134 changed files with 1,505 additions and 1,426 deletions.
2 changes: 1 addition & 1 deletion hotswap-agent-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ although it is usually better to use plugin custom "dependency injection" via @I
classloading issues.
* Watcher - service to register filesystem events (create/modify/delete a resource).
* Transformer - service to register hotwap transformation (define or reload a class definition).
* Javaassist - source code for 3.17.1-GA official javaasist release repackaged to custom location to avoid conflicts.
* Javaassist - source code for 3.30.2-GA official javaasist release repackaged to custom location to avoid conflicts.
* Logger - Simple logging mechanism. Custom logging is use instead of library to avoid conflicts with frameworks / app servers.

Classloading issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ClassClassPath(Class<?> c) {
* class paths if javassist.jar is put in jre/lib/ext/
* (with JDK1.4).
*/
this(java.lang.Object.class);
this(Object.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package org.hotswap.agent.javassist;

import java.util.HashMap;

import org.hotswap.agent.javassist.bytecode.Descriptor;

import java.util.HashMap;

/**
* A hash table associating class names with different names.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@

package org.hotswap.agent.javassist;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.hotswap.agent.javassist.bytecode.ClassFile;
import org.hotswap.agent.javassist.bytecode.Descriptor;
import org.hotswap.agent.javassist.util.proxy.DefineClassHelper;
import org.hotswap.agent.javassist.util.proxy.DefinePackageHelper;

import java.io.*;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import org.hotswap.agent.javassist.bytecode.ClassFile;
import org.hotswap.agent.javassist.bytecode.Descriptor;
import org.hotswap.agent.javassist.util.proxy.DefinePackageHelper;

/**
* A container of <code>CtClass</code> objects.
* A <code>CtClass</code> object must be obtained from this object.
Expand Down Expand Up @@ -113,6 +110,14 @@ public class ClassPool {
*/
public static boolean releaseUnmodifiedClassFile = true;

/**
* If true, the contents of a jar file are cached after the jar
* file is opened.
*
* <p>The initial value is true.
*/
public static boolean cacheOpenedJarFile = true; // see ClassPoolTail.JarClassPath#openClassfile(String)

protected ClassPoolTail source;
protected ClassPool parent;
protected Hashtable classes; // should be synchronous
Expand Down Expand Up @@ -293,7 +298,7 @@ public void clearImportedPackages() {
}

/**
* Returns all the package names recorded by <code>importPackage()</code>.
* Returns all the package names recorded by <code>importPackage()</code>.
*
* @see #importPackage(String)
* @since 3.1
Expand Down Expand Up @@ -466,7 +471,7 @@ public CtClass getOrNull(String classname) {
* that classname can be an array-type "descriptor" (an encoded
* type name) such as <code>[Ljava/lang/Object;</code>.
*
* <p>Using this method is not recommended; this method should be
* <p>Using this method is not recommended; this method should be
* used only to obtain the <code>CtClass</code> object
* with a name returned from <code>getClassInfo</code> in
* <code>javassist.bytecode.ClassPool</code>. <code>getClassInfo</code>
Expand Down Expand Up @@ -530,7 +535,7 @@ protected synchronized CtClass get0(String classname, boolean useCache)
* @return null if the class file could not be found.
*/
protected CtClass createCtClass(String classname, boolean useCache) {
// accept "[L<class name>;" as a class name.
// accept "[L<class name>;" as a class name.
if (classname.charAt(0) == '[')
classname = Descriptor.toClassName(classname);

Expand Down Expand Up @@ -882,7 +887,7 @@ public synchronized CtClass makeInterface(String name, CtClass superclass)
* the new interface overwrites that previous one.
*
* @param name a fully-qualified interface name.
* Or null if the annotation has no super interface.
* Or null if the annotation has no super interface.
* @throws RuntimeException if the existing interface is frozen.
* @since 3.19
*/
Expand Down Expand Up @@ -1017,8 +1022,8 @@ public void appendPathList(String pathlist) throws NotFoundException {
* allowed any more.
* To load the class, this method uses the context class loader
* of the current thread. It is obtained by calling
* <code>getClassLoader()</code>.
*
* <code>getClassLoader()</code>.
*
* <p>This behavior can be changed by subclassing the pool and changing
* the <code>getClassLoader()</code> method.
* If the program is running on some application
Expand All @@ -1039,32 +1044,32 @@ public void appendPathList(String pathlist) throws NotFoundException {
* protection domain is not specified.</p>
*
* @see #toClass(CtClass,Class)
* @see #toClass(CtClass,Class,java.lang.ClassLoader,ProtectionDomain)
* @see #toClass(CtClass,Class, ClassLoader,ProtectionDomain)
* @see #getClassLoader()
*/
public Class toClass(CtClass clazz) throws CannotCompileException {
// Some subclasses of ClassPool may override toClass(CtClass,ClassLoader).
// So we should call that method instead of toClass(.., ProtectionDomain).
return toClass(clazz, getClassLoader());
return toClass(clazz, getClassLoader());
}

/**
* Get the classloader for <code>toClass()</code>, <code>getAnnotations()</code> in
* <code>CtClass</code>, etc.
*
*
* <p>The default is the context class loader.
*
*
* @return the classloader for the pool
* @see #toClass(CtClass)
* @see CtClass#getAnnotations()
*/
public ClassLoader getClassLoader() {
return getContextClassLoader();
}

/**
* Obtains a class loader that seems appropriate to look up a class
* by name.
* by name.
*/
static ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
Expand Down Expand Up @@ -1147,7 +1152,7 @@ public Class<?> toClass(CtClass ct, Class<?> neighbor)
throws CannotCompileException
{
try {
return org.hotswap.agent.javassist.util.proxy.DefineClassHelper.toClass(neighbor,
return DefineClassHelper.toClass(neighbor,
ct.toBytecode());
}
catch (IOException e) {
Expand All @@ -1173,7 +1178,7 @@ public Class<?> toClass(CtClass ct,
throws CannotCompileException
{
try {
return org.hotswap.agent.javassist.util.proxy.DefineClassHelper.toClass(lookup,
return DefineClassHelper.toClass(lookup,
ct.toBytecode());
}
catch (IOException e) {
Expand Down Expand Up @@ -1229,7 +1234,7 @@ public Class toClass(CtClass ct, Class<?> neighbor, ClassLoader loader,
throws CannotCompileException
{
try {
return org.hotswap.agent.javassist.util.proxy.DefineClassHelper.toClass(ct.getName(),
return DefineClassHelper.toClass(ct.getName(),
neighbor, loader, domain, ct.toBytecode());
}
catch (IOException e) {
Expand All @@ -1242,15 +1247,11 @@ public Class toClass(CtClass ct, Class<?> neighbor, ClassLoader loader,
* performs nothing.
*
* <p>You do not necessarily need to
* call this method. If this method is called, then
* <code>getPackage()</code> on the <code>Class</code> object returned
* call this method. If this method is called, then
* <code>getPackage()</code> on the <code>Class</code> object returned
* by <code>toClass()</code> will return a non-null object.</p>
*
* <p>The jigsaw module introduced by Java 9 has broken this method.
* In Java 9 or later, the VM argument
* <code>--add-opens java.base/java.lang=ALL-UNNAMED</code>
* has to be given to the JVM so that this method can run.
* </p>
* <p>The jigsaw module introduced by Java 9 has broken this method.</p>
*
* @param loader the class loader passed to <code>toClass()</code> or
* the default one obtained by <code>getClassLoader()</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

package org.hotswap.agent.javassist;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.hotswap.agent.javassist.bytecode.ClassFile;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

Expand All @@ -54,7 +50,7 @@ public InputStream openClassfile(String classname) {
char sep = File.separatorChar;
String filename = directory + sep
+ classname.replace('.', sep) + ".class";
return new FileInputStream(filename.toString());
return new FileInputStream(filename);
}
catch (FileNotFoundException e) {}
catch (SecurityException e) {}
Expand Down Expand Up @@ -128,15 +124,15 @@ public URL find(String classname) {
}

final class JarClassPath implements ClassPath {
List<String> jarfileEntries;
Set<String> jarfileEntries;
String jarfileURL;

JarClassPath(String pathname) throws NotFoundException {
JarFile jarfile = null;
try {
jarfile = new JarFile(pathname);
jarfileEntries = new ArrayList<String>();
for (JarEntry je:Collections.list(jarfile.entries()))
jarfileEntries = new HashSet<String>();
for (JarEntry je: Collections.list(jarfile.entries()))
if (je.getName().endsWith(".class"))
jarfileEntries.add(je.getName());
jarfileURL = new File(pathname).getCanonicalFile()
Expand All @@ -159,9 +155,13 @@ public InputStream openClassfile(String classname)
URL jarURL = find(classname);
if (null != jarURL)
try {
java.net.URLConnection con = jarURL.openConnection();
con.setUseCaches(false);
return con.getInputStream();
if (ClassPool.cacheOpenedJarFile)
return jarURL.openConnection().getInputStream();
else {
java.net.URLConnection con = jarURL.openConnection();
con.setUseCaches(false);
return con.getInputStream();
}
}
catch (IOException e) {
throw new NotFoundException("broken jar file?: "
Expand All @@ -183,7 +183,7 @@ public URL find(String classname) {

@Override
public String toString() {
return jarfileURL == null ? "<null>" : jarfileURL.toString();
return jarfileURL == null ? "<null>" : jarfileURL;
}
}

Expand All @@ -196,7 +196,7 @@ public ClassPoolTail() {

@Override
public String toString() {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
buf.append("[class path: ");
ClassPathList list = pathList;
while (list != null) {
Expand Down Expand Up @@ -244,7 +244,7 @@ public synchronized void removeClassPath(ClassPath cp) {
}

public ClassPath appendSystemPath() {
if (org.hotswap.agent.javassist.bytecode.ClassFile.MAJOR_VERSION < org.hotswap.agent.javassist.bytecode.ClassFile.JAVA_9)
if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_9)
return appendClassPath(new ClassClassPath());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return appendClassPath(new LoaderClassPath(cl));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,8 @@

package org.hotswap.agent.javassist;

import org.hotswap.agent.javassist.bytecode.BadBytecode;
import org.hotswap.agent.javassist.bytecode.CodeAttribute;
import org.hotswap.agent.javassist.bytecode.CodeIterator;
import org.hotswap.agent.javassist.bytecode.ConstPool;
import org.hotswap.agent.javassist.bytecode.MethodInfo;
import org.hotswap.agent.javassist.convert.TransformAccessArrayField;
import org.hotswap.agent.javassist.convert.TransformAfter;
import org.hotswap.agent.javassist.convert.TransformBefore;
import org.hotswap.agent.javassist.convert.TransformCall;
import org.hotswap.agent.javassist.convert.TransformFieldAccess;
import org.hotswap.agent.javassist.convert.TransformNew;
import org.hotswap.agent.javassist.convert.TransformNewClass;
import org.hotswap.agent.javassist.convert.TransformReadField;
import org.hotswap.agent.javassist.convert.TransformWriteField;
import org.hotswap.agent.javassist.convert.Transformer;
import org.hotswap.agent.javassist.bytecode.*;
import org.hotswap.agent.javassist.convert.*;

/**
* Simple translator of method bodies
Expand Down Expand Up @@ -406,6 +393,42 @@ public void redirectMethodCall(String oldMethodName,
= new TransformCall(transformers, oldMethodName, newMethod);
}

/**
* Redirect non-static method invocations in a method body to a static
* method. The return type must be same with the originally invoked method.
* As parameters, the static method receives
* the target object and all the parameters to the originally invoked
* method. For example, if the originally invoked method is
* <code>move()</code>:
*
* <pre>class Point {
* Point move(int x, int y) { ... }
* }</pre>
*
* <p>Then the static method must be something like this:
*
* <pre>class Verbose {
* static Point print(Point target, int x, int y) { ... }
* }</pre>
*
* <p>The <code>CodeConverter</code> would translate bytecode
* equivalent to:
*
* <pre>Point p2 = p.move(x + y, 0);</pre>
*
* <p>into the bytecode equivalent to:
*
* <pre>Point p2 = Verbose.print(p, x + y, 0);</pre>
*
* @param origMethod original method
* @param staticMethod static method
*/
public void redirectMethodCallToStatic(CtMethod origMethod,
CtMethod staticMethod) {
transformers = new TransformCallToStatic(transformers, origMethod,
staticMethod);
}

/**
* Insert a call to another method before an existing method call.
* That "before" method must be static. The return type must be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ public CtClass getSuperclass() throws NotFoundException
return pool.get(javaLangObject);
}

public String getSuperclassName() throws NotFoundException
{
CtClass superclass = getSuperclass();
return superclass != null ? superclass.getName() : null;
}

@Override
public CtMethod[] getMethods()
{
Expand Down
Loading

0 comments on commit 7c57e2e

Please sign in to comment.