├── .gitignore ├── Java 内存马查杀工具.html ├── README.md ├── pom.xml ├── properties └── tomcat_server.properties └── src └── main ├── java └── org │ └── xxxx │ ├── Run.java │ ├── agent │ ├── Agent.java │ ├── Config.java │ ├── JarFileHelper.java │ ├── Module.java │ └── ModuleLoader.java │ ├── core │ ├── CoreBoot.java │ ├── genie │ │ ├── GenieBase.java │ │ └── tomcat │ │ │ └── ApplicationFilterChainGenie.java │ ├── killer │ │ ├── AgentKiller.java │ │ ├── KillerBase.java │ │ ├── NormalKiller.java │ │ └── TransformKiller.java │ └── trasnformer │ │ ├── ClassDumpTransformer.java │ │ ├── GenieTransformer.java │ │ ├── KillerTransformer.java │ │ ├── ProtectedTransformer.java │ │ ├── ScanTransformer.java │ │ └── TransformerBase.java │ ├── javassist │ ├── ByteArrayClassPath.java │ ├── CannotCompileException.java │ ├── ClassClassPath.java │ ├── ClassMap.java │ ├── ClassPath.java │ ├── ClassPool.java │ ├── ClassPoolTail.java │ ├── CodeConverter.java │ ├── CtArray.java │ ├── CtBehavior.java │ ├── CtClass.java │ ├── CtClassType.java │ ├── CtConstructor.java │ ├── CtField.java │ ├── CtMember.java │ ├── CtMethod.java │ ├── CtNewClass.java │ ├── CtNewConstructor.java │ ├── CtNewMethod.java │ ├── CtNewNestedClass.java │ ├── CtNewWrappedConstructor.java │ ├── CtNewWrappedMethod.java │ ├── CtPrimitiveType.java │ ├── Loader.java │ ├── LoaderClassPath.java │ ├── Modifier.java │ ├── NotFoundException.java │ ├── SerialVersionUID.java │ ├── Translator.java │ ├── URLClassPath.java │ ├── bytecode │ │ ├── AccessFlag.java │ │ ├── AnnotationDefaultAttribute.java │ │ ├── AnnotationsAttribute.java │ │ ├── AttributeInfo.java │ │ ├── BadBytecode.java │ │ ├── BootstrapMethodsAttribute.java │ │ ├── ByteArray.java │ │ ├── ByteStream.java │ │ ├── Bytecode.java │ │ ├── ClassFile.java │ │ ├── ClassFilePrinter.java │ │ ├── ClassFileWriter.java │ │ ├── CodeAnalyzer.java │ │ ├── CodeAttribute.java │ │ ├── CodeIterator.java │ │ ├── ConstPool.java │ │ ├── ConstantAttribute.java │ │ ├── DeprecatedAttribute.java │ │ ├── Descriptor.java │ │ ├── DuplicateMemberException.java │ │ ├── EnclosingMethodAttribute.java │ │ ├── ExceptionTable.java │ │ ├── ExceptionsAttribute.java │ │ ├── FieldInfo.java │ │ ├── InnerClassesAttribute.java │ │ ├── InstructionPrinter.java │ │ ├── LineNumberAttribute.java │ │ ├── LocalVariableAttribute.java │ │ ├── LocalVariableTypeAttribute.java │ │ ├── LongVector.java │ │ ├── MethodInfo.java │ │ ├── MethodParametersAttribute.java │ │ ├── Mnemonic.java │ │ ├── Opcode.java │ │ ├── ParameterAnnotationsAttribute.java │ │ ├── SignatureAttribute.java │ │ ├── SourceFileAttribute.java │ │ ├── StackMap.java │ │ ├── StackMapTable.java │ │ ├── SyntheticAttribute.java │ │ ├── TypeAnnotationsAttribute.java │ │ ├── analysis │ │ │ ├── Analyzer.java │ │ │ ├── ControlFlow.java │ │ │ ├── Executor.java │ │ │ ├── Frame.java │ │ │ ├── FramePrinter.java │ │ │ ├── IntQueue.java │ │ │ ├── MultiArrayType.java │ │ │ ├── MultiType.java │ │ │ ├── Subroutine.java │ │ │ ├── SubroutineScanner.java │ │ │ ├── Type.java │ │ │ ├── Util.java │ │ │ └── package.html │ │ ├── annotation │ │ │ ├── Annotation.java │ │ │ ├── AnnotationImpl.java │ │ │ ├── AnnotationMemberValue.java │ │ │ ├── AnnotationsWriter.java │ │ │ ├── ArrayMemberValue.java │ │ │ ├── BooleanMemberValue.java │ │ │ ├── ByteMemberValue.java │ │ │ ├── CharMemberValue.java │ │ │ ├── ClassMemberValue.java │ │ │ ├── DoubleMemberValue.java │ │ │ ├── EnumMemberValue.java │ │ │ ├── FloatMemberValue.java │ │ │ ├── IntegerMemberValue.java │ │ │ ├── LongMemberValue.java │ │ │ ├── MemberValue.java │ │ │ ├── MemberValueVisitor.java │ │ │ ├── NoSuchClassError.java │ │ │ ├── ShortMemberValue.java │ │ │ ├── StringMemberValue.java │ │ │ ├── TypeAnnotationsWriter.java │ │ │ └── package.html │ │ ├── package.html │ │ └── stackmap │ │ │ ├── BasicBlock.java │ │ │ ├── MapMaker.java │ │ │ ├── Tracer.java │ │ │ ├── TypeData.java │ │ │ ├── TypeTag.java │ │ │ └── TypedBlock.java │ ├── compiler │ │ ├── AccessorMaker.java │ │ ├── CodeGen.java │ │ ├── CompileError.java │ │ ├── Javac.java │ │ ├── JvstCodeGen.java │ │ ├── JvstTypeChecker.java │ │ ├── KeywordTable.java │ │ ├── Lex.java │ │ ├── MemberCodeGen.java │ │ ├── MemberResolver.java │ │ ├── NoFieldException.java │ │ ├── Parser.java │ │ ├── ProceedHandler.java │ │ ├── SymbolTable.java │ │ ├── SyntaxError.java │ │ ├── TokenId.java │ │ ├── TypeChecker.java │ │ └── ast │ │ │ ├── ASTList.java │ │ │ ├── ASTree.java │ │ │ ├── ArrayInit.java │ │ │ ├── AssignExpr.java │ │ │ ├── BinExpr.java │ │ │ ├── CallExpr.java │ │ │ ├── CastExpr.java │ │ │ ├── CondExpr.java │ │ │ ├── Declarator.java │ │ │ ├── DoubleConst.java │ │ │ ├── Expr.java │ │ │ ├── FieldDecl.java │ │ │ ├── InstanceOfExpr.java │ │ │ ├── IntConst.java │ │ │ ├── Keyword.java │ │ │ ├── Member.java │ │ │ ├── MethodDecl.java │ │ │ ├── NewExpr.java │ │ │ ├── Pair.java │ │ │ ├── Stmnt.java │ │ │ ├── StringL.java │ │ │ ├── Symbol.java │ │ │ ├── Variable.java │ │ │ └── Visitor.java │ ├── convert │ │ ├── TransformAccessArrayField.java │ │ ├── TransformAfter.java │ │ ├── TransformBefore.java │ │ ├── TransformCall.java │ │ ├── TransformFieldAccess.java │ │ ├── TransformNew.java │ │ ├── TransformNewClass.java │ │ ├── TransformReadField.java │ │ ├── TransformWriteField.java │ │ └── Transformer.java │ ├── expr │ │ ├── Cast.java │ │ ├── ConstructorCall.java │ │ ├── Expr.java │ │ ├── ExprEditor.java │ │ ├── FieldAccess.java │ │ ├── Handler.java │ │ ├── Instanceof.java │ │ ├── MethodCall.java │ │ ├── NewArray.java │ │ ├── NewExpr.java │ │ └── package.html │ ├── package.html │ ├── runtime │ │ ├── Cflow.java │ │ ├── Desc.java │ │ ├── DotClass.java │ │ ├── Inner.java │ │ └── package.html │ ├── scopedpool │ │ ├── ScopedClassPool.java │ │ ├── ScopedClassPoolFactory.java │ │ ├── ScopedClassPoolFactoryImpl.java │ │ ├── ScopedClassPoolRepository.java │ │ ├── ScopedClassPoolRepositoryImpl.java │ │ ├── SoftValueHashMap.java │ │ └── package.html │ ├── tools │ │ ├── Callback.java │ │ ├── Dump.java │ │ ├── framedump.java │ │ ├── package.html │ │ ├── reflect │ │ │ ├── CannotCreateException.java │ │ │ ├── CannotInvokeException.java │ │ │ ├── CannotReflectException.java │ │ │ ├── ClassMetaobject.java │ │ │ ├── Compiler.java │ │ │ ├── Loader.java │ │ │ ├── Metalevel.java │ │ │ ├── Metaobject.java │ │ │ ├── Reflection.java │ │ │ ├── Sample.java │ │ │ └── package.html │ │ ├── rmi │ │ │ ├── AppletServer.java │ │ │ ├── ObjectImporter.java │ │ │ ├── ObjectNotFoundException.java │ │ │ ├── Proxy.java │ │ │ ├── RemoteException.java │ │ │ ├── RemoteRef.java │ │ │ ├── Sample.java │ │ │ ├── StubGenerator.java │ │ │ └── package.html │ │ └── web │ │ │ ├── BadHttpRequest.java │ │ │ ├── Viewer.java │ │ │ ├── Webserver.java │ │ │ └── package.html │ └── util │ │ ├── HotSwapper.java │ │ ├── package.html │ │ └── proxy │ │ ├── FactoryHelper.java │ │ ├── MethodFilter.java │ │ ├── MethodHandler.java │ │ ├── Proxy.java │ │ ├── ProxyFactory.java │ │ ├── ProxyObject.java │ │ ├── ProxyObjectInputStream.java │ │ ├── ProxyObjectOutputStream.java │ │ ├── RuntimeSupport.java │ │ ├── SecurityActions.java │ │ ├── SerializedProxy.java │ │ └── package.html │ ├── request │ ├── AbstractRequest.java │ └── HttpServletRequest.java │ ├── response │ └── HttpServletResponse.java │ └── utils │ ├── Cache.java │ ├── CheckStruct.java │ ├── JavassistUtil.java │ ├── Reflections.java │ └── Utils.java └── resources └── META-INF └── MANIFEST.MF /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | output/ 4 | out/ 5 | *.class 6 | 7 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | MemShellKiller 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | UTF-8 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | src/main/resources 28 | 29 | 30 | **/*.* 31 | **/*.properties 32 | **/*.fxml 33 | **/*.class 34 | **/fxml/*.fxml 35 | **/template/*.class 36 | 37 | 38 | 39 | 40 | false 41 | 42 | 43 | 44 | 45 | org.apache.maven.plugins 46 | maven-compiler-plugin 47 | 48 | 8 49 | 8 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /properties/tomcat_server.properties: -------------------------------------------------------------------------------- 1 | coreBoot=org.xxxx.core.CoreBoot 2 | genie-name=ApplicationFilterChainGenie 3 | black-list=javax.servlet.http.HttpServlet,org.apache.catalina.core.ApplicationFilterChain,org.apache.tomcat.websocket.server.WsFilter 4 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/Run.java: -------------------------------------------------------------------------------- 1 | package org.xxxx; 2 | 3 | import java.io.File; 4 | import java.lang.reflect.Method; 5 | import java.net.URL; 6 | import java.net.URLClassLoader; 7 | import java.util.List; 8 | 9 | public class Run { 10 | public static void main(String[] args) throws Exception { 11 | if (args.length == 0) 12 | return; 13 | String agentPath = args[0]; 14 | String action = args[1]; 15 | String propertiesPath = args[2]; 16 | // System.out.print(agentPath); 17 | try { 18 | // File toolsJar = new File(System.getProperty("java.home").replaceFirst("jre", "lib") + File.separator + "tools.jar"); 19 | // System.out.print(toolsJar.toURI()); 20 | URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 21 | // Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); 22 | // add.setAccessible(true); 23 | // add.invoke(classLoader, new Object[]{toolsJar.toURI().toURL()}); 24 | Class MyVirtualMachine = classLoader.loadClass("com.sun.tools.attach.VirtualMachine"); 25 | Class MyVirtualMachineDescriptor = classLoader.loadClass("com.sun.tools.attach.VirtualMachineDescriptor"); 26 | Method list = MyVirtualMachine.getDeclaredMethod("list", new Class[0]); 27 | List invoke = (List) list.invoke((Object) null, new Object[0]); 28 | for (int i = 0; i < invoke.size(); i++) { 29 | Object o = invoke.get(i); 30 | Method displayName = o.getClass().getSuperclass().getDeclaredMethod("displayName", new Class[0]); 31 | Object name = displayName.invoke(o, new Object[0]); 32 | System.out.println(String.format("find jvm process name:[[[%s]]]", new Object[]{name.toString()})); 33 | if (name.toString().contains("org.apache.catalina.startup.Bootstrap")) { 34 | Method attach = MyVirtualMachine.getDeclaredMethod("attach", new Class[]{MyVirtualMachineDescriptor}); 35 | Object machine = attach.invoke(MyVirtualMachine, new Object[]{o}); 36 | Method loadAgent = machine.getClass().getSuperclass().getSuperclass().getDeclaredMethod("loadAgent", new Class[]{String.class, String.class}); 37 | loadAgent.invoke(machine, new Object[]{agentPath, String.format("%s,%s", new Object[]{action, propertiesPath})}); 38 | Method detach = MyVirtualMachine.getDeclaredMethod("detach", new Class[0]); 39 | detach.invoke(machine, new Object[0]); 40 | break; 41 | } 42 | } 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/agent/Agent.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.agent; 2 | 3 | 4 | import java.lang.instrument.Instrumentation; 5 | 6 | public class Agent { 7 | public static String projectVersion; 8 | public static String buildTime; 9 | public static String gitCommit; 10 | 11 | public Agent() { 12 | } 13 | 14 | public static void main(String[] args) { 15 | } 16 | 17 | public static void premain(String agentArg, Instrumentation inst) { 18 | init("normal", agentArg, inst); 19 | } 20 | 21 | public static void agentmain(String agentArg, Instrumentation inst) { 22 | init("attach", agentArg, inst); 23 | } 24 | 25 | public static synchronized void init(String mode, String agentArg, Instrumentation inst) { 26 | try { 27 | String[] args = agentArg.split(","); 28 | if(args.length != 2){ 29 | System.err.println("[MemShellKiller] Failed to initialize, The format of args must be: action,configPath"); 30 | return; 31 | } 32 | String action = args[0]; 33 | String configPath = args[1]; 34 | JarFileHelper.addJarToBootstrap(inst); 35 | Config.loadConfig(configPath); 36 | if(action.equals("release")){ 37 | ModuleLoader.release(mode); 38 | } 39 | else{ 40 | ModuleLoader.load(mode, action, inst);} 41 | } catch (Throwable throwable) { 42 | System.err.println("[MemShellKiller] Failed to initialize, will continue without security protection."); 43 | throwable.printStackTrace(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/agent/JarFileHelper.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.agent; 2 | 3 | import java.io.IOException; 4 | import java.io.UnsupportedEncodingException; 5 | import java.lang.instrument.Instrumentation; 6 | import java.net.URL; 7 | import java.net.URLDecoder; 8 | import java.util.jar.JarFile; 9 | 10 | public class JarFileHelper { 11 | public JarFileHelper() { 12 | } 13 | 14 | public static void addJarToBootstrap(Instrumentation inst) throws IOException { 15 | String localJarPath = getLocalJarPath(); 16 | inst.appendToBootstrapClassLoaderSearch(new JarFile(localJarPath)); 17 | } 18 | 19 | public static String getLocalJarPath() { 20 | URL localUrl = Agent.class.getProtectionDomain().getCodeSource().getLocation(); 21 | String path = null; 22 | 23 | try { 24 | path = URLDecoder.decode(localUrl.getFile().replace("+", "%2B"), "UTF-8"); 25 | } catch (UnsupportedEncodingException exception) { 26 | System.err.println("[MemShellKiller] Failed to get jarFile path."); 27 | exception.printStackTrace(); 28 | } 29 | 30 | return path; 31 | } 32 | 33 | public static String getLocalJarParentPath() { 34 | String jarPath = getLocalJarPath(); 35 | return jarPath.substring(0, jarPath.lastIndexOf("/")); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/agent/Module.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.agent; 2 | 3 | import java.lang.instrument.Instrumentation; 4 | 5 | public abstract class Module { 6 | 7 | public abstract void start(String serverType, Instrumentation instrumentation) throws Throwable; 8 | 9 | public abstract void release(String mode) throws Throwable; 10 | } -------------------------------------------------------------------------------- /src/main/java/org/xxxx/agent/ModuleLoader.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.agent; 2 | 3 | import java.io.File; 4 | import java.io.UnsupportedEncodingException; 5 | import java.lang.instrument.Instrumentation; 6 | import java.net.URLDecoder; 7 | 8 | public class ModuleLoader { 9 | private static String coreBoot; 10 | private static Module module; 11 | public static String baseDirectory; 12 | private static ModuleLoader instance; 13 | public static ClassLoader moduleClassLoader; 14 | 15 | private ModuleLoader(String mode, Instrumentation inst) throws Throwable { 16 | Class clazz = moduleClassLoader.loadClass(coreBoot); 17 | module = (Module) clazz.newInstance(); 18 | module.start("tomcat", inst); 19 | 20 | } 21 | 22 | public static synchronized void release(String mode) { 23 | try { 24 | if (module != null) { 25 | System.out.println("[MemShellKiller] Start to release MemShellKiller"); 26 | module.release(mode); 27 | module = null; 28 | instance = null; 29 | } else { 30 | System.out.println("[MemShellKiller] Module is initialized, skipped"); 31 | } 32 | } catch (Throwable throwable) { 33 | } 34 | 35 | } 36 | 37 | public static synchronized void load(String mode, String action, Instrumentation inst) throws Throwable { 38 | if ("install".equals(action)) { 39 | if (instance == null) { 40 | try { 41 | instance = new ModuleLoader(mode, inst); 42 | } catch (Throwable var4) { 43 | instance = null; 44 | throw var4; 45 | } 46 | } else { 47 | System.out.println("[MemShellKiller] The MemShellKiller has bean initialized and cannot be initialized again"); 48 | } 49 | } else { 50 | if (!"uninstall".equals(action)) { 51 | throw new IllegalStateException("[MemShellKiller] Can not support the action: " + action); 52 | } 53 | 54 | release(mode); 55 | } 56 | 57 | } 58 | 59 | static { 60 | coreBoot = Config.properties.getProperty("coreBoot"); 61 | Class clazz; 62 | try { 63 | clazz = Class.forName("java.nio.file.FileSystems"); 64 | clazz.getMethod("getDefault").invoke((Object) null); 65 | } catch (Throwable var4) { 66 | } 67 | 68 | clazz = ModuleLoader.class; 69 | String path = clazz.getResource("/" + clazz.getName().replace(".", "/") + ".class").getPath(); 70 | if (path.startsWith("file:")) { 71 | path = path.substring(5); 72 | } 73 | 74 | if (path.contains("!")) { 75 | path = path.substring(0, path.indexOf("!")); 76 | } 77 | 78 | try { 79 | baseDirectory = URLDecoder.decode((new File(path)).getParent(), "UTF-8"); 80 | } catch (UnsupportedEncodingException var3) { 81 | baseDirectory = (new File(path)).getParent(); 82 | } 83 | 84 | ClassLoader systemClassLoader; 85 | for (systemClassLoader = ClassLoader.getSystemClassLoader(); systemClassLoader.getParent() != null && !systemClassLoader.getClass().getName().equals("sun.misc.Launcher$ExtClassLoader"); systemClassLoader = systemClassLoader.getParent()) { 86 | } 87 | 88 | moduleClassLoader = systemClassLoader; 89 | } 90 | } -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/CoreBoot.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core; 2 | 3 | import org.xxxx.agent.Config; 4 | import org.xxxx.agent.Module; 5 | import org.xxxx.agent.ModuleLoader; 6 | import org.xxxx.core.genie.GenieBase; 7 | import org.xxxx.core.trasnformer.GenieTransformer; 8 | import org.xxxx.core.trasnformer.TransformerBase; 9 | import org.xxxx.utils.Cache; 10 | import org.xxxx.utils.Reflections; 11 | 12 | import java.lang.instrument.Instrumentation; 13 | 14 | public class CoreBoot extends Module{ 15 | private String genieName; 16 | private TransformerBase transformer; 17 | private String serverType; 18 | 19 | @Override 20 | public void start(String serverType, Instrumentation instrumentation) throws Throwable { 21 | this.genieName = Config.properties.getProperty("genie-name"); 22 | this.serverType = serverType.toLowerCase(); 23 | this.initTransformer(instrumentation); 24 | } 25 | 26 | @Override 27 | public void release(String mode) throws Throwable { 28 | if(this.transformer != null){ 29 | this.transformer.release(); 30 | } 31 | } 32 | 33 | private void initTransformer(Instrumentation instrumentation) throws Throwable { 34 | Cache.initClassByteCache(); 35 | String genieType = this.serverType + "." +this.genieName; 36 | Class genieClass = ModuleLoader.moduleClassLoader.loadClass("org.xxxx.core.genie." + genieType); 37 | GenieBase genieObject = (GenieBase) genieClass.newInstance(); 38 | Reflections.setFieldValue(genieObject, "instrumentation", instrumentation); 39 | this.transformer = new GenieTransformer(instrumentation, genieObject); 40 | this.transformer.retransform(); 41 | 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/killer/AgentKiller.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.killer; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.security.ProtectionDomain; 7 | 8 | public class AgentKiller extends KillerBase { 9 | @Override 10 | public String getType() { 11 | return "agent"; 12 | } 13 | 14 | @Override 15 | public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { 16 | 17 | try { 18 | InputStream in = loader.getResourceAsStream(className + ".class"); 19 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 20 | int nRead; 21 | byte[] data = new byte[1024]; 22 | while (true) { 23 | if (!((nRead = in.read(data, 0, data.length)) != -1)) break; 24 | buffer.write(data, 0, nRead); 25 | } 26 | classfileBuffer = buffer.toByteArray(); 27 | return classfileBuffer; 28 | } catch (IOException e) { 29 | e.printStackTrace(); 30 | } 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/killer/KillerBase.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.killer; 2 | 3 | import org.xxxx.agent.ModuleLoader; 4 | import org.xxxx.javassist.ClassClassPath; 5 | import org.xxxx.javassist.ClassPool; 6 | import org.xxxx.javassist.LoaderClassPath; 7 | 8 | import java.security.ProtectionDomain; 9 | 10 | public abstract class KillerBase { 11 | 12 | public abstract String getType(); 13 | public abstract byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer); 14 | 15 | public void addLoader(ClassPool classPool, ClassLoader loader) { 16 | classPool.appendSystemPath(); 17 | classPool.appendClassPath(new ClassClassPath(ModuleLoader.class)); 18 | if (loader != null) { 19 | classPool.appendClassPath(new LoaderClassPath(loader)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/killer/TransformKiller.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.killer; 2 | 3 | import org.xxxx.javassist.*; 4 | import org.xxxx.utils.JavassistUtil; 5 | 6 | import java.io.ByteArrayInputStream; 7 | import java.io.IOException; 8 | import java.security.ProtectionDomain; 9 | 10 | public class TransformKiller extends KillerBase { 11 | 12 | @Override 13 | public String getType() { 14 | return "transformer"; 15 | } 16 | 17 | @Override 18 | public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { 19 | 20 | CtClass ctClass = null; 21 | try { 22 | ClassPool classPool = new ClassPool(); 23 | this.addLoader(classPool, loader); 24 | ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer)); 25 | // 以下包括两种kill transform的方法 26 | // String methodSrc = "String clazzName = $2.replace(\"/\", \".\"); byte[] newClassfileBuffer = (byte[])org.xxxx.utils.Cache.classByteCache.get(clazzName); if(newClassfileBuffer != null){return newClassfileBuffer; }"; 27 | // new JavassistUtil().insertBefore(ctClass, "transform", (String)null, methodSrc); 28 | // String methodSrc = "{String clazzName = $2.replace(\"/\", \".\"); byte[] newClassfileBuffer = (byte[])org.xxxx.utils.Cache.classByteCache.get(clazzName); if(newClassfileBuffer != null){return newClassfileBuffer; }return null;}"; 29 | String methodSrc = "{return null;}"; 30 | new JavassistUtil().setBody(ctClass, "transform", (String)null, methodSrc); 31 | classfileBuffer = ctClass.toBytecode(); 32 | return classfileBuffer; 33 | } catch (IOException var13) { 34 | var13.printStackTrace(); 35 | } catch (NotFoundException e) { 36 | e.printStackTrace(); 37 | } catch (CannotCompileException e) { 38 | e.printStackTrace(); 39 | } 40 | finally { 41 | if(ctClass != null){ 42 | ctClass.detach(); 43 | } 44 | } 45 | return null; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/trasnformer/ClassDumpTransformer.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.trasnformer; 2 | 3 | import org.xxxx.utils.Cache; 4 | 5 | import java.lang.instrument.ClassFileTransformer; 6 | import java.lang.instrument.IllegalClassFormatException; 7 | import java.lang.instrument.Instrumentation; 8 | import java.security.ProtectionDomain; 9 | 10 | public class ClassDumpTransformer extends TransformerBase implements ClassFileTransformer { 11 | private String className; 12 | private Instrumentation instrumentation; 13 | private byte[] out; 14 | public ClassDumpTransformer(Instrumentation instrumentation, String className){ 15 | this.className = className; 16 | this.instrumentation = instrumentation; 17 | instrumentation.addTransformer(this, true); 18 | } 19 | 20 | public void release(){ 21 | this.instrumentation.removeTransformer(this); 22 | } 23 | 24 | @Override 25 | public void retransform() throws Throwable { 26 | Class[] loadedClasses = this.instrumentation.getAllLoadedClasses(); 27 | Class[] arr = loadedClasses; 28 | int len = loadedClasses.length; 29 | 30 | for(int i = 0; i < len; ++i) { 31 | Class clazz = arr[i]; 32 | if (clazz.getName().equals(this.className) && !clazz.getName().startsWith("java.lang.invoke.LambdaForm")){ 33 | try { 34 | this.instrumentation.retransformClasses(new Class[]{clazz}); 35 | break; 36 | } catch (Throwable throwable) { 37 | throw throwable; 38 | } 39 | } 40 | } 41 | } 42 | 43 | @Override 44 | public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { 45 | 46 | String clazzName = className.replace("/", "."); 47 | if (clazzName.equals(this.className)){ 48 | this.setOut(classfileBuffer); 49 | } 50 | return null; 51 | } 52 | 53 | public byte[] getOut() { 54 | return out; 55 | } 56 | 57 | public void setOut(byte[] out) { 58 | this.out = out; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/trasnformer/ScanTransformer.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.trasnformer; 2 | 3 | import org.xxxx.agent.Config; 4 | import org.xxxx.utils.Cache; 5 | 6 | import java.lang.instrument.ClassFileTransformer; 7 | import java.lang.instrument.IllegalClassFormatException; 8 | import java.lang.instrument.Instrumentation; 9 | import java.security.ProtectionDomain; 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | 14 | public class ScanTransformer extends TransformerBase implements ClassFileTransformer { 15 | private Instrumentation instrumentation; 16 | private List scanResult; 17 | private final static String interFaceName = "java.lang.instrument.ClassFileTransformer"; 18 | private static List blackList; 19 | private static List witheTransformerNames = Config.witheTransformerNames; 20 | 21 | public ScanTransformer(Instrumentation instrumentation) { 22 | this.instrumentation = instrumentation; 23 | this.scanResult = new ArrayList(); 24 | instrumentation.addTransformer(this, true); 25 | Cache.initTransformers(); 26 | Cache.initLastBlackList(); 27 | } 28 | 29 | private void isMatchedInterFaces(Class clazz) { 30 | // skip withe name list 31 | if(witheTransformerNames.contains(clazz.getName())){ 32 | return ; 33 | } 34 | Class[] classes = clazz.getInterfaces(); 35 | for (Class c : classes) { 36 | if (c.getName().equals(interFaceName)) { 37 | Cache.addLastTransformer(clazz); 38 | return; 39 | } 40 | } 41 | } 42 | 43 | 44 | private void isMatchedBlackList(Class clazz) { 45 | String clazzName = clazz.getName(); 46 | if(blackList.contains(clazzName)){ 47 | Cache.addLastBlackList(clazz); 48 | } 49 | } 50 | 51 | private void isMatched(Class clazz){ 52 | isMatchedBlackList(clazz); 53 | isMatchedInterFaces(clazz); 54 | } 55 | 56 | @Override 57 | public void retransform() { 58 | Class[] loadedClasses = this.instrumentation.getAllLoadedClasses(); 59 | Class[] arr = loadedClasses; 60 | int len = loadedClasses.length; 61 | for (int i = 0; i < len; ++i) { 62 | Class clazz = arr[i]; 63 | isMatched(clazz); 64 | } 65 | } 66 | 67 | @Override 68 | public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { 69 | String clazzName = className.replace("/", "."); 70 | byte[] classfileBuffer1 = Cache.classByteCache.get(clazzName); 71 | if(classfileBuffer1 != null){ 72 | return classfileBuffer1; 73 | } 74 | return classfileBuffer; 75 | } 76 | 77 | @Override 78 | public void release() { 79 | this.instrumentation.removeTransformer(this); 80 | } 81 | 82 | 83 | public List getScanResult() { 84 | this.scanResult.addAll(Cache.lastTransformers); 85 | this.scanResult.addAll(Cache.lastBlackList); 86 | return this.scanResult; 87 | } 88 | 89 | static { 90 | try { 91 | String blackName = Config.properties.getProperty("black-list"); 92 | blackList = Arrays.asList(blackName.split(",")); 93 | } catch (Exception e) { 94 | blackList = new ArrayList(); 95 | } 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/core/trasnformer/TransformerBase.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.core.trasnformer; 2 | 3 | 4 | import org.xxxx.agent.ModuleLoader; 5 | import org.xxxx.javassist.ClassClassPath; 6 | import org.xxxx.javassist.ClassPool; 7 | import org.xxxx.javassist.LoaderClassPath; 8 | 9 | import java.lang.instrument.UnmodifiableClassException; 10 | 11 | public abstract class TransformerBase { 12 | public abstract void retransform() throws Throwable; 13 | 14 | public abstract void release() throws UnmodifiableClassException, ClassNotFoundException; 15 | 16 | public void addLoader(ClassPool classPool, ClassLoader loader) { 17 | classPool.appendSystemPath(); 18 | classPool.appendClassPath(new ClassClassPath(ModuleLoader.class)); 19 | if (loader != null) { 20 | classPool.appendClassPath(new LoaderClassPath(loader)); 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/ClassPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist; 18 | 19 | import java.io.InputStream; 20 | import java.net.URL; 21 | 22 | /** 23 | * ClassPath is an interface implemented by objects 24 | * representing a class search path. 25 | * ClassPool uses those objects for reading class files. 26 | * 27 | *

The users can define a class implementing this interface so that 28 | * a class file is obtained from a non-standard source. 29 | * 30 | * @see ClassPool#insertClassPath(ClassPath) 31 | * @see ClassPool#appendClassPath(ClassPath) 32 | * @see ClassPool#removeClassPath(ClassPath) 33 | */ 34 | public interface ClassPath { 35 | /** 36 | * Opens a class file. 37 | * This method may be called just to examine whether the class file 38 | * exists as well as to read the contents of the file. 39 | * 40 | *

This method can return null if the specified class file is not 41 | * found. If null is returned, the next search path is examined. 42 | * However, if an error happens, this method must throw an exception 43 | * so that the search will be terminated. 44 | * 45 | *

This method should not modify the contents of the class file. 46 | * 47 | * @param classname a fully-qualified class name 48 | * @return the input stream for reading a class file 49 | * @see Translator 50 | */ 51 | InputStream openClassfile(String classname) throws NotFoundException; 52 | 53 | /** 54 | * Returns the uniform resource locator (URL) of the class file 55 | * with the specified name. 56 | * 57 | * @param classname a fully-qualified class name. 58 | * @return null if the specified class file could not be found. 59 | */ 60 | URL find(String classname); 61 | 62 | /** 63 | * This method is invoked when the ClassPath object is 64 | * detached from the search path. It will be an empty method in most of 65 | * classes. 66 | */ 67 | void close(); 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/CtNewNestedClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist; 18 | 19 | import org.xxxx.javassist.bytecode.AccessFlag; 20 | import org.xxxx.javassist.bytecode.ClassFile; 21 | import org.xxxx.javassist.bytecode.InnerClassesAttribute; 22 | 23 | /** 24 | * A newly created public nested class. 25 | */ 26 | class CtNewNestedClass extends CtNewClass { 27 | CtNewNestedClass(String realName, ClassPool cp, boolean isInterface, 28 | CtClass superclass) { 29 | super(realName, cp, isInterface, superclass); 30 | } 31 | 32 | /** 33 | * This method does not change the STATIC bit. The original value is kept. 34 | */ 35 | public void setModifiers(int mod) { 36 | mod = mod & ~Modifier.STATIC; 37 | super.setModifiers(mod); 38 | updateInnerEntry(mod, getName(), this, true); 39 | } 40 | 41 | private static void updateInnerEntry(int mod, String name, CtClass clazz, boolean outer) { 42 | ClassFile cf = clazz.getClassFile2(); 43 | InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute( 44 | InnerClassesAttribute.tag); 45 | if (ica == null) 46 | return; 47 | 48 | int n = ica.tableLength(); 49 | for (int i = 0; i < n; i++) 50 | if (name.equals(ica.innerClass(i))) { 51 | int acc = ica.accessFlags(i) & AccessFlag.STATIC; 52 | ica.setAccessFlags(i, mod | acc); 53 | String outName = ica.outerClass(i); 54 | if (outName != null && outer) 55 | try { 56 | CtClass parent = clazz.getClassPool().get(outName); 57 | updateInnerEntry(mod, name, parent, false); 58 | } 59 | catch (NotFoundException e) { 60 | throw new RuntimeException("cannot find the declaring class: " 61 | + outName); 62 | } 63 | 64 | break; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/LoaderClassPath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist; 18 | 19 | import java.io.InputStream; 20 | import java.lang.ref.WeakReference; 21 | import java.net.URL; 22 | 23 | /** 24 | * A class search-path representing a class loader. 25 | * 26 | *

It is used for obtaining a class file from the given 27 | * class loader by getResourceAsStream(). 28 | * The LoaderClassPath refers to the class loader through 29 | * WeakReference. If the class loader is garbage collected, 30 | * the other search pathes are examined. 31 | * 32 | *

The given class loader must have both getResourceAsStream() 33 | * and getResource(). 34 | * 35 | * @author Bill Burke 36 | * @author Shigeru Chiba 37 | * 38 | * @see ClassPool#insertClassPath(ClassPath) 39 | * @see ClassPool#appendClassPath(ClassPath) 40 | * @see ClassClassPath 41 | */ 42 | public class LoaderClassPath implements ClassPath { 43 | private WeakReference clref; 44 | 45 | /** 46 | * Creates a search path representing a class loader. 47 | */ 48 | public LoaderClassPath(ClassLoader cl) { 49 | clref = new WeakReference(cl); 50 | } 51 | 52 | public String toString() { 53 | Object cl = null; 54 | if (clref != null) 55 | cl = clref.get(); 56 | 57 | return cl == null ? "" : cl.toString(); 58 | } 59 | 60 | /** 61 | * Obtains a class file from the class loader. 62 | * This method calls getResourceAsStream(String) 63 | * on the class loader. 64 | */ 65 | public InputStream openClassfile(String classname) { 66 | String cname = classname.replace('.', '/') + ".class"; 67 | ClassLoader cl = (ClassLoader)clref.get(); 68 | if (cl == null) 69 | return null; // not found 70 | else 71 | return cl.getResourceAsStream(cname); 72 | } 73 | 74 | /** 75 | * Obtains the URL of the specified class file. 76 | * This method calls getResource(String) 77 | * on the class loader. 78 | * 79 | * @return null if the class file could not be found. 80 | */ 81 | public URL find(String classname) { 82 | String cname = classname.replace('.', '/') + ".class"; 83 | ClassLoader cl = (ClassLoader)clref.get(); 84 | if (cl == null) 85 | return null; // not found 86 | else 87 | return cl.getResource(cname); 88 | } 89 | 90 | /** 91 | * Closes this class path. 92 | */ 93 | public void close() { 94 | clref = null; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/NotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist; 18 | 19 | /** 20 | * Signals that something could not be found. 21 | */ 22 | public class NotFoundException extends Exception { 23 | public NotFoundException(String msg) { 24 | super(msg); 25 | } 26 | 27 | public NotFoundException(String msg, Exception e) { 28 | super(msg + " because of " + e.toString()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/Translator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist; 18 | 19 | /** 20 | * An observer of Loader. 21 | * The users can define a class implementing this 22 | * interface and attach an instance of that class to a 23 | * Loader object so that it can translate a class file 24 | * when the class file is loaded into the JVM. 25 | * 26 | * @see Loader#addTranslator(ClassPool, Translator) 27 | */ 28 | public interface Translator { 29 | /** 30 | * Is invoked by a Loader for initialization 31 | * when the object is attached to the Loader object. 32 | * This method can be used for getting (for caching) some 33 | * CtClass objects that will be accessed 34 | * in onLoad() in Translator. 35 | * 36 | * @param pool the ClassPool that this translator 37 | * should use. 38 | * @see Loader 39 | * @throws NotFoundException if a CtClass cannot be found. 40 | * @throws CannotCompileException if the initialization by this method 41 | * fails. 42 | */ 43 | void start(ClassPool pool) 44 | throws NotFoundException, CannotCompileException; 45 | 46 | /** 47 | * Is invoked by a Loader for notifying that 48 | * a class is loaded. The Loader calls 49 | * 50 | *

51 |      * pool.get(classname).toBytecode()
52 | * 53 | * to read the class file after onLoad() returns. 54 | * 55 | *

classname may be the name of a class 56 | * that has not been created yet. 57 | * If so, onLoad() must create that class so that 58 | * the Loader can read it after onLoad() 59 | * returns. 60 | * 61 | * @param pool the ClassPool that this translator 62 | * should use. 63 | * @param classname the name of the class being loaded. 64 | * @see Loader 65 | * @throws NotFoundException if a CtClass cannot be found. 66 | * @throws CannotCompileException if the code transformation 67 | * by this method fails. 68 | */ 69 | void onLoad(ClassPool pool, String classname) 70 | throws NotFoundException, CannotCompileException; 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/BadBytecode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | /** 20 | * Signals that a bad bytecode sequence has been found. 21 | */ 22 | public class BadBytecode extends Exception { 23 | public BadBytecode(int opcode) { 24 | super("bytecode " + opcode); 25 | } 26 | 27 | public BadBytecode(String msg) { 28 | super(msg); 29 | } 30 | 31 | public BadBytecode(String msg, Throwable cause) { 32 | super(msg, cause); 33 | } 34 | 35 | public BadBytecode(MethodInfo minfo, Throwable cause) { 36 | super(minfo.toString() + " in " 37 | + minfo.getConstPool().getClassName() 38 | + ": " + cause.getMessage(), cause); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/ByteArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | /** 20 | * A collection of static methods for reading and writing a byte array. 21 | */ 22 | public class ByteArray { 23 | /** 24 | * Reads an unsigned 16bit integer at the index. 25 | */ 26 | public static int readU16bit(byte[] code, int index) { 27 | return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff); 28 | } 29 | 30 | /** 31 | * Reads a signed 16bit integer at the index. 32 | */ 33 | public static int readS16bit(byte[] code, int index) { 34 | return (code[index] << 8) | (code[index + 1] & 0xff); 35 | } 36 | 37 | /** 38 | * Writes a 16bit integer at the index. 39 | */ 40 | public static void write16bit(int value, byte[] code, int index) { 41 | code[index] = (byte)(value >>> 8); 42 | code[index + 1] = (byte)value; 43 | } 44 | 45 | /** 46 | * Reads a 32bit integer at the index. 47 | */ 48 | public static int read32bit(byte[] code, int index) { 49 | return (code[index] << 24) | ((code[index + 1] & 0xff) << 16) 50 | | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff); 51 | } 52 | 53 | /** 54 | * Writes a 32bit integer at the index. 55 | */ 56 | public static void write32bit(int value, byte[] code, int index) { 57 | code[index] = (byte)(value >>> 24); 58 | code[index + 1] = (byte)(value >>> 16); 59 | code[index + 2] = (byte)(value >>> 8); 60 | code[index + 3] = (byte)value; 61 | } 62 | 63 | /** 64 | * Copies a 32bit integer. 65 | * 66 | * @param src the source byte array. 67 | * @param isrc the index into the source byte array. 68 | * @param dest the destination byte array. 69 | * @param idest the index into the destination byte array. 70 | */ 71 | static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) { 72 | dest[idest] = src[isrc]; 73 | dest[idest + 1] = src[isrc + 1]; 74 | dest[idest + 2] = src[isrc + 2]; 75 | dest[idest + 3] = src[isrc + 3]; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/ConstantAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | /** 24 | * ConstantValue_attribute. 25 | */ 26 | public class ConstantAttribute extends AttributeInfo { 27 | /** 28 | * The name of this attribute "ConstantValue". 29 | */ 30 | public static final String tag = "ConstantValue"; 31 | 32 | ConstantAttribute(ConstPool cp, int n, DataInputStream in) 33 | throws IOException 34 | { 35 | super(cp, n, in); 36 | } 37 | 38 | /** 39 | * Constructs a ConstantValue attribute. 40 | * 41 | * @param cp a constant pool table. 42 | * @param index constantvalue_index 43 | * of ConstantValue_attribute. 44 | */ 45 | public ConstantAttribute(ConstPool cp, int index) { 46 | super(cp, tag); 47 | byte[] bvalue = new byte[2]; 48 | bvalue[0] = (byte)(index >>> 8); 49 | bvalue[1] = (byte)index; 50 | set(bvalue); 51 | } 52 | 53 | /** 54 | * Returns constantvalue_index. 55 | */ 56 | public int getConstantValue() { 57 | return ByteArray.readU16bit(get(), 0); 58 | } 59 | 60 | /** 61 | * Makes a copy. Class names are replaced according to the 62 | * given Map object. 63 | * 64 | * @param newCp the constant pool table used by the new copy. 65 | * @param classnames pairs of replaced and substituted 66 | * class names. 67 | */ 68 | public AttributeInfo copy(ConstPool newCp, Map classnames) { 69 | int index = getConstPool().copy(getConstantValue(), newCp, 70 | classnames); 71 | return new ConstantAttribute(newCp, index); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/DeprecatedAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | /** 24 | * Deprecated_attribute. 25 | */ 26 | public class DeprecatedAttribute extends AttributeInfo { 27 | /** 28 | * The name of this attribute "Deprecated". 29 | */ 30 | public static final String tag = "Deprecated"; 31 | 32 | DeprecatedAttribute(ConstPool cp, int n, DataInputStream in) 33 | throws IOException 34 | { 35 | super(cp, n, in); 36 | } 37 | 38 | /** 39 | * Constructs a Deprecated attribute. 40 | * 41 | * @param cp a constant pool table. 42 | */ 43 | public DeprecatedAttribute(ConstPool cp) { 44 | super(cp, tag, new byte[0]); 45 | } 46 | 47 | /** 48 | * Makes a copy. 49 | * 50 | * @param newCp the constant pool table used by the new copy. 51 | * @param classnames should be null. 52 | */ 53 | public AttributeInfo copy(ConstPool newCp, Map classnames) { 54 | return new DeprecatedAttribute(newCp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/DuplicateMemberException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import org.xxxx.javassist.CannotCompileException; 20 | 21 | /** 22 | * An exception thrown when adding a duplicate member is requested. 23 | * 24 | * @see ClassFile#addMethod(MethodInfo) 25 | * @see ClassFile#addField(FieldInfo) 26 | */ 27 | public class DuplicateMemberException extends CannotCompileException { 28 | public DuplicateMemberException(String msg) { 29 | super(msg); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/LocalVariableTypeAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | /** 24 | * LocalVariableTypeTable_attribute. 25 | * 26 | * @since 3.11 27 | */ 28 | public class LocalVariableTypeAttribute extends LocalVariableAttribute { 29 | /** 30 | * The name of the attribute "LocalVariableTypeTable". 31 | */ 32 | public static final String tag = LocalVariableAttribute.typeTag; 33 | 34 | /** 35 | * Constructs an empty LocalVariableTypeTable. 36 | */ 37 | public LocalVariableTypeAttribute(ConstPool cp) { 38 | super(cp, tag, new byte[2]); 39 | ByteArray.write16bit(0, info, 0); 40 | } 41 | 42 | LocalVariableTypeAttribute(ConstPool cp, int n, DataInputStream in) 43 | throws IOException 44 | { 45 | super(cp, n, in); 46 | } 47 | 48 | private LocalVariableTypeAttribute(ConstPool cp, byte[] dest) { 49 | super(cp, tag, dest); 50 | } 51 | 52 | String renameEntry(String desc, String oldname, String newname) { 53 | return SignatureAttribute.renameClass(desc, oldname, newname); 54 | } 55 | 56 | String renameEntry(String desc, Map classnames) { 57 | return SignatureAttribute.renameClass(desc, classnames); 58 | } 59 | 60 | LocalVariableAttribute makeThisAttr(ConstPool cp, byte[] dest) { 61 | return new LocalVariableTypeAttribute(cp, dest); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/LongVector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | final class LongVector { 20 | static final int ASIZE = 128; 21 | static final int ABITS = 7; // ASIZE = 2^ABITS 22 | static final int VSIZE = 8; 23 | private ConstInfo[][] objects; 24 | private int elements; 25 | 26 | public LongVector() { 27 | objects = new ConstInfo[VSIZE][]; 28 | elements = 0; 29 | } 30 | 31 | public LongVector(int initialSize) { 32 | int vsize = ((initialSize >> ABITS) & ~(VSIZE - 1)) + VSIZE; 33 | objects = new ConstInfo[vsize][]; 34 | elements = 0; 35 | } 36 | 37 | public int size() { return elements; } 38 | 39 | public int capacity() { return objects.length * ASIZE; } 40 | 41 | public ConstInfo elementAt(int i) { 42 | if (i < 0 || elements <= i) 43 | return null; 44 | 45 | return objects[i >> ABITS][i & (ASIZE - 1)]; 46 | } 47 | 48 | public void addElement(ConstInfo value) { 49 | int nth = elements >> ABITS; 50 | int offset = elements & (ASIZE - 1); 51 | int len = objects.length; 52 | if (nth >= len) { 53 | ConstInfo[][] newObj = new ConstInfo[len + VSIZE][]; 54 | System.arraycopy(objects, 0, newObj, 0, len); 55 | objects = newObj; 56 | } 57 | 58 | if (objects[nth] == null) 59 | objects[nth] = new ConstInfo[ASIZE]; 60 | 61 | objects[nth][offset] = value; 62 | elements++; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/MethodParametersAttribute.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.javassist.bytecode; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.IOException; 5 | import java.util.Map; 6 | 7 | /** 8 | * MethodParameters_attribute. 9 | */ 10 | public class MethodParametersAttribute extends AttributeInfo { 11 | /** 12 | * The name of this attribute "MethodParameters". 13 | */ 14 | public static final String tag = "MethodParameters"; 15 | 16 | MethodParametersAttribute(ConstPool cp, int n, DataInputStream in) 17 | throws IOException 18 | { 19 | super(cp, n, in); 20 | } 21 | 22 | /** 23 | * Constructs an attribute. 24 | * 25 | * @param cp a constant pool table. 26 | * @param names an array of parameter names. 27 | * The i-th element is the name of the i-th parameter. 28 | * @param flags an array of parameter access flags. 29 | */ 30 | public MethodParametersAttribute(ConstPool cp, String[] names, int[] flags) { 31 | super(cp, tag); 32 | byte[] data = new byte[names.length * 4 + 1]; 33 | data[0] = (byte)names.length; 34 | for (int i = 0; i < names.length; i++) { 35 | ByteArray.write16bit(cp.addUtf8Info(names[i]), data, i * 4 + 1); 36 | ByteArray.write16bit(flags[i], data, i * 4 + 3); 37 | } 38 | 39 | set(data); 40 | } 41 | 42 | /** 43 | * Returns parameters_count, which is the number of 44 | * parameters. 45 | */ 46 | public int size() { 47 | return info[0] & 0xff; 48 | } 49 | 50 | /** 51 | * Returns the value of name_index of the i-th element of parameters. 52 | * 53 | * @param i the position of the parameter. 54 | */ 55 | public int name(int i) { 56 | return ByteArray.readU16bit(info, i * 4 + 1); 57 | } 58 | 59 | /** 60 | * Returns the value of access_flags of the i-th element of parameters. 61 | * 62 | * @param i the position of the parameter. 63 | * @see AccessFlag 64 | */ 65 | public int accessFlags(int i) { 66 | return ByteArray.readU16bit(info, i * 4 + 3); 67 | } 68 | 69 | /** 70 | * Makes a copy. 71 | * 72 | * @param newCp the constant pool table used by the new copy. 73 | * @param classnames ignored. 74 | */ 75 | public AttributeInfo copy(ConstPool newCp, Map classnames) { 76 | int s = size(); 77 | ConstPool cp = getConstPool(); 78 | String[] names = new String[s]; 79 | int[] flags = new int[s]; 80 | for (int i = 0; i < s; i++) { 81 | names[i] = cp.getUtf8Info(name(i)); 82 | flags[i] = accessFlags(i); 83 | } 84 | 85 | return new MethodParametersAttribute(newCp, names, flags); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/SourceFileAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | /** 24 | * SourceFile_attribute. 25 | */ 26 | public class SourceFileAttribute extends AttributeInfo { 27 | /** 28 | * The name of this attribute "SourceFile". 29 | */ 30 | public static final String tag = "SourceFile"; 31 | 32 | SourceFileAttribute(ConstPool cp, int n, DataInputStream in) 33 | throws IOException 34 | { 35 | super(cp, n, in); 36 | } 37 | 38 | /** 39 | * Constructs a SourceFile attribute. 40 | * 41 | * @param cp a constant pool table. 42 | * @param filename the name of the source file. 43 | */ 44 | public SourceFileAttribute(ConstPool cp, String filename) { 45 | super(cp, tag); 46 | int index = cp.addUtf8Info(filename); 47 | byte[] bvalue = new byte[2]; 48 | bvalue[0] = (byte)(index >>> 8); 49 | bvalue[1] = (byte)index; 50 | set(bvalue); 51 | } 52 | 53 | /** 54 | * Returns the file name indicated by sourcefile_index. 55 | */ 56 | public String getFileName() { 57 | return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0)); 58 | } 59 | 60 | /** 61 | * Makes a copy. Class names are replaced according to the 62 | * given Map object. 63 | * 64 | * @param newCp the constant pool table used by the new copy. 65 | * @param classnames pairs of replaced and substituted 66 | * class names. 67 | */ 68 | public AttributeInfo copy(ConstPool newCp, Map classnames) { 69 | return new SourceFileAttribute(newCp, getFileName()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/SyntheticAttribute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode; 18 | 19 | import java.io.DataInputStream; 20 | import java.io.IOException; 21 | import java.util.Map; 22 | 23 | /** 24 | * Synthetic_attribute. 25 | */ 26 | public class SyntheticAttribute extends AttributeInfo { 27 | /** 28 | * The name of this attribute "Synthetic". 29 | */ 30 | public static final String tag = "Synthetic"; 31 | 32 | SyntheticAttribute(ConstPool cp, int n, DataInputStream in) 33 | throws IOException 34 | { 35 | super(cp, n, in); 36 | } 37 | 38 | /** 39 | * Constructs a Synthetic attribute. 40 | * 41 | * @param cp a constant pool table. 42 | */ 43 | public SyntheticAttribute(ConstPool cp) { 44 | super(cp, tag, new byte[0]); 45 | } 46 | 47 | /** 48 | * Makes a copy. 49 | * 50 | * @param newCp the constant pool table used by the new copy. 51 | * @param classnames should be null. 52 | */ 53 | public AttributeInfo copy(ConstPool newCp, Map classnames) { 54 | return new SyntheticAttribute(newCp); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/analysis/IntQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.analysis; 17 | 18 | import java.util.NoSuchElementException; 19 | 20 | class IntQueue { 21 | private static class Entry { 22 | private Entry next; 23 | private int value; 24 | private Entry(int value) { 25 | this.value = value; 26 | } 27 | } 28 | private Entry head; 29 | 30 | private Entry tail; 31 | 32 | void add(int value) { 33 | Entry entry = new Entry(value); 34 | if (tail != null) 35 | tail.next = entry; 36 | tail = entry; 37 | 38 | if (head == null) 39 | head = entry; 40 | } 41 | 42 | boolean isEmpty() { 43 | return head == null; 44 | } 45 | 46 | int take() { 47 | if (head == null) 48 | throw new NoSuchElementException(); 49 | 50 | int value = head.value; 51 | head = head.next; 52 | if (head == null) 53 | tail = null; 54 | 55 | return value; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/analysis/Subroutine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.analysis; 17 | 18 | import java.util.*; 19 | 20 | /** 21 | * Represents a nested method subroutine (marked by JSR and RET). 22 | * 23 | * @author Jason T. Greene 24 | */ 25 | public class Subroutine { 26 | //private Set callers = new HashSet(); 27 | private List callers = new ArrayList(); 28 | private Set access = new HashSet(); 29 | private int start; 30 | 31 | public Subroutine(int start, int caller) { 32 | this.start = start; 33 | callers.add(new Integer(caller)); 34 | } 35 | 36 | public void addCaller(int caller) { 37 | callers.add(new Integer(caller)); 38 | } 39 | 40 | public int start() { 41 | return start; 42 | } 43 | 44 | public void access(int index) { 45 | access.add(new Integer(index)); 46 | } 47 | 48 | public boolean isAccessed(int index) { 49 | return access.contains(new Integer(index)); 50 | } 51 | 52 | public Collection accessed() { 53 | return access; 54 | } 55 | 56 | public Collection callers() { 57 | return callers; 58 | } 59 | 60 | public String toString() { 61 | return "start = " + start + " callers = " + callers.toString(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/analysis/Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.analysis; 17 | 18 | import org.xxxx.javassist.bytecode.CodeIterator; 19 | import org.xxxx.javassist.bytecode.Opcode; 20 | 21 | /** 22 | * A set of common utility methods. 23 | * 24 | * @author Jason T. Greene 25 | */ 26 | public class Util implements Opcode { 27 | public static int getJumpTarget(int pos, CodeIterator iter) { 28 | int opcode = iter.byteAt(pos); 29 | pos += (opcode == JSR_W || opcode == GOTO_W) ? iter.s32bitAt(pos + 1) : iter.s16bitAt(pos + 1); 30 | return pos; 31 | } 32 | 33 | public static boolean isJumpInstruction(int opcode) { 34 | return (opcode >= IFEQ && opcode <= JSR) || opcode == IFNULL || opcode == IFNONNULL || opcode == JSR_W || opcode == GOTO_W; 35 | } 36 | 37 | public static boolean isGoto(int opcode) { 38 | return opcode == GOTO || opcode == GOTO_W; 39 | } 40 | 41 | public static boolean isJsr(int opcode) { 42 | return opcode == JSR || opcode == JSR_W; 43 | } 44 | 45 | public static boolean isReturn(int opcode) { 46 | return (opcode >= IRETURN && opcode <= RETURN); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/analysis/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bytecode Analysis API. 4 | 5 |

This package provides an API for performing data-flow analysis on a method's bytecode. 6 | This allows the user to determine the type state of the stack and local variable table 7 | at the start of every instruction. In addition this API can be used to validate 8 | bytecode, find dead bytecode, and identify unnecessary checkcasts. 9 | Look at ControlFlow class first for details. 10 | 11 |

The users of this package must know the specifications of 12 | class file and Java bytecode. For more details, read this book: 13 | 14 |

    Tim Lindholm and Frank Yellin, 15 | "The Java Virtual Machine Specification 2nd Ed.", 16 | Addison-Wesley, 1999. 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/AnnotationMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.annotation; 17 | 18 | import org.xxxx.javassist.ClassPool; 19 | import org.xxxx.javassist.bytecode.ConstPool; 20 | 21 | import java.io.IOException; 22 | import java.lang.reflect.Method; 23 | 24 | /** 25 | * Nested annotation. 26 | * 27 | * @author Bill Burke 28 | * @author Shigeru Chiba 29 | */ 30 | public class AnnotationMemberValue extends MemberValue { 31 | Annotation value; 32 | 33 | /** 34 | * Constructs an annotation member. The initial value is not specified. 35 | */ 36 | public AnnotationMemberValue(ConstPool cp) { 37 | this(null, cp); 38 | } 39 | 40 | /** 41 | * Constructs an annotation member. The initial value is specified by 42 | * the first parameter. 43 | */ 44 | public AnnotationMemberValue(Annotation a, ConstPool cp) { 45 | super('@', cp); 46 | value = a; 47 | } 48 | 49 | Object getValue(ClassLoader cl, ClassPool cp, Method m) 50 | throws ClassNotFoundException 51 | { 52 | return AnnotationImpl.make(cl, getType(cl), cp, value); 53 | } 54 | 55 | Class getType(ClassLoader cl) throws ClassNotFoundException { 56 | if (value == null) 57 | throw new ClassNotFoundException("no type specified"); 58 | else 59 | return loadClass(cl, value.getTypeName()); 60 | } 61 | 62 | /** 63 | * Obtains the value. 64 | */ 65 | public Annotation getValue() { 66 | return value; 67 | } 68 | 69 | /** 70 | * Sets the value of this member. 71 | */ 72 | public void setValue(Annotation newValue) { 73 | value = newValue; 74 | } 75 | 76 | /** 77 | * Obtains the string representation of this object. 78 | */ 79 | public String toString() { 80 | return value.toString(); 81 | } 82 | 83 | /** 84 | * Writes the value. 85 | */ 86 | public void write(AnnotationsWriter writer) throws IOException { 87 | writer.annotationValue(); 88 | value.write(writer); 89 | } 90 | 91 | /** 92 | * Accepts a visitor. 93 | */ 94 | public void accept(MemberValueVisitor visitor) { 95 | visitor.visitAnnotationMemberValue(this); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/BooleanMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.annotation; 17 | 18 | import org.xxxx.javassist.ClassPool; 19 | import org.xxxx.javassist.bytecode.ConstPool; 20 | 21 | import java.io.IOException; 22 | import java.lang.reflect.Method; 23 | 24 | /** 25 | * Boolean constant value. 26 | * 27 | * @author Bill Burke 28 | * @author Shigeru Chiba 29 | */ 30 | public class BooleanMemberValue extends MemberValue { 31 | int valueIndex; 32 | 33 | /** 34 | * Constructs a boolean constant value. The initial value is specified 35 | * by the constant pool entry at the given index. 36 | * 37 | * @param index the index of a CONSTANT_Integer_info structure. 38 | */ 39 | public BooleanMemberValue(int index, ConstPool cp) { 40 | super('Z', cp); 41 | this.valueIndex = index; 42 | } 43 | 44 | /** 45 | * Constructs a boolean constant value. 46 | * 47 | * @param b the initial value. 48 | */ 49 | public BooleanMemberValue(boolean b, ConstPool cp) { 50 | super('Z', cp); 51 | setValue(b); 52 | } 53 | 54 | /** 55 | * Constructs a boolean constant value. The initial value is false. 56 | */ 57 | public BooleanMemberValue(ConstPool cp) { 58 | super('Z', cp); 59 | setValue(false); 60 | } 61 | 62 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 63 | return new Boolean(getValue()); 64 | } 65 | 66 | Class getType(ClassLoader cl) { 67 | return boolean.class; 68 | } 69 | 70 | /** 71 | * Obtains the value of the member. 72 | */ 73 | public boolean getValue() { 74 | return cp.getIntegerInfo(valueIndex) != 0; 75 | } 76 | 77 | /** 78 | * Sets the value of the member. 79 | */ 80 | public void setValue(boolean newValue) { 81 | valueIndex = cp.addIntegerInfo(newValue ? 1 : 0); 82 | } 83 | 84 | /** 85 | * Obtains the string representation of this object. 86 | */ 87 | public String toString() { 88 | return getValue() ? "true" : "false"; 89 | } 90 | 91 | /** 92 | * Writes the value. 93 | */ 94 | public void write(AnnotationsWriter writer) throws IOException { 95 | writer.constValueIndex(getValue()); 96 | } 97 | 98 | /** 99 | * Accepts a visitor. 100 | */ 101 | public void accept(MemberValueVisitor visitor) { 102 | visitor.visitBooleanMemberValue(this); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/ByteMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.bytecode.annotation; 17 | 18 | import org.xxxx.javassist.ClassPool; 19 | import org.xxxx.javassist.bytecode.ConstPool; 20 | 21 | import java.io.IOException; 22 | import java.lang.reflect.Method; 23 | 24 | /** 25 | * Byte constant value. 26 | * 27 | * @author Bill Burke 28 | * @author Shigeru Chiba 29 | */ 30 | public class ByteMemberValue extends MemberValue { 31 | int valueIndex; 32 | 33 | /** 34 | * Constructs a byte constant value. The initial value is specified 35 | * by the constant pool entry at the given index. 36 | * 37 | * @param index the index of a CONSTANT_Integer_info structure. 38 | */ 39 | public ByteMemberValue(int index, ConstPool cp) { 40 | super('B', cp); 41 | this.valueIndex = index; 42 | } 43 | 44 | /** 45 | * Constructs a byte constant value. 46 | * 47 | * @param b the initial value. 48 | */ 49 | public ByteMemberValue(byte b, ConstPool cp) { 50 | super('B', cp); 51 | setValue(b); 52 | } 53 | 54 | /** 55 | * Constructs a byte constant value. The initial value is 0. 56 | */ 57 | public ByteMemberValue(ConstPool cp) { 58 | super('B', cp); 59 | setValue((byte)0); 60 | } 61 | 62 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 63 | return new Byte(getValue()); 64 | } 65 | 66 | Class getType(ClassLoader cl) { 67 | return byte.class; 68 | } 69 | 70 | /** 71 | * Obtains the value of the member. 72 | */ 73 | public byte getValue() { 74 | return (byte)cp.getIntegerInfo(valueIndex); 75 | } 76 | 77 | /** 78 | * Sets the value of the member. 79 | */ 80 | public void setValue(byte newValue) { 81 | valueIndex = cp.addIntegerInfo(newValue); 82 | } 83 | 84 | /** 85 | * Obtains the string representation of this object. 86 | */ 87 | public String toString() { 88 | return Byte.toString(getValue()); 89 | } 90 | 91 | /** 92 | * Writes the value. 93 | */ 94 | public void write(AnnotationsWriter writer) throws IOException { 95 | writer.constValueIndex(getValue()); 96 | } 97 | 98 | /** 99 | * Accepts a visitor. 100 | */ 101 | public void accept(MemberValueVisitor visitor) { 102 | visitor.visitByteMemberValue(this); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/CharMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * Char constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | */ 31 | public class CharMemberValue extends MemberValue { 32 | int valueIndex; 33 | 34 | /** 35 | * Constructs a char constant value. The initial value is specified 36 | * by the constant pool entry at the given index. 37 | * 38 | * @param index the index of a CONSTANT_Integer_info structure. 39 | */ 40 | public CharMemberValue(int index, ConstPool cp) { 41 | super('C', cp); 42 | this.valueIndex = index; 43 | } 44 | 45 | /** 46 | * Constructs a char constant value. 47 | * 48 | * @param c the initial value. 49 | */ 50 | public CharMemberValue(char c, ConstPool cp) { 51 | super('C', cp); 52 | setValue(c); 53 | } 54 | 55 | /** 56 | * Constructs a char constant value. The initial value is '\0'. 57 | */ 58 | public CharMemberValue(ConstPool cp) { 59 | super('C', cp); 60 | setValue('\0'); 61 | } 62 | 63 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 64 | return new Character(getValue()); 65 | } 66 | 67 | Class getType(ClassLoader cl) { 68 | return char.class; 69 | } 70 | 71 | /** 72 | * Obtains the value of the member. 73 | */ 74 | public char getValue() { 75 | return (char)cp.getIntegerInfo(valueIndex); 76 | } 77 | 78 | /** 79 | * Sets the value of the member. 80 | */ 81 | public void setValue(char newValue) { 82 | valueIndex = cp.addIntegerInfo(newValue); 83 | } 84 | 85 | /** 86 | * Obtains the string representation of this object. 87 | */ 88 | public String toString() { 89 | return Character.toString(getValue()); 90 | } 91 | 92 | /** 93 | * Writes the value. 94 | */ 95 | public void write(AnnotationsWriter writer) throws IOException { 96 | writer.constValueIndex(getValue()); 97 | } 98 | 99 | /** 100 | * Accepts a visitor. 101 | */ 102 | public void accept(MemberValueVisitor visitor) { 103 | visitor.visitCharMemberValue(this); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/DoubleMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * Double floating-point number constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | * @version $Revision: 1.7 $ 31 | */ 32 | public class DoubleMemberValue extends MemberValue { 33 | int valueIndex; 34 | 35 | /** 36 | * Constructs a double constant value. The initial value is specified 37 | * by the constant pool entry at the given index. 38 | * 39 | * @param index the index of a CONSTANT_Double_info structure. 40 | */ 41 | public DoubleMemberValue(int index, ConstPool cp) { 42 | super('D', cp); 43 | this.valueIndex = index; 44 | } 45 | 46 | /** 47 | * Constructs a double constant value. 48 | * 49 | * @param d the initial value. 50 | */ 51 | public DoubleMemberValue(double d, ConstPool cp) { 52 | super('D', cp); 53 | setValue(d); 54 | } 55 | 56 | /** 57 | * Constructs a double constant value. The initial value is 0.0. 58 | */ 59 | public DoubleMemberValue(ConstPool cp) { 60 | super('D', cp); 61 | setValue(0.0); 62 | } 63 | 64 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 65 | return new Double(getValue()); 66 | } 67 | 68 | Class getType(ClassLoader cl) { 69 | return double.class; 70 | } 71 | 72 | /** 73 | * Obtains the value of the member. 74 | */ 75 | public double getValue() { 76 | return cp.getDoubleInfo(valueIndex); 77 | } 78 | 79 | /** 80 | * Sets the value of the member. 81 | */ 82 | public void setValue(double newValue) { 83 | valueIndex = cp.addDoubleInfo(newValue); 84 | } 85 | 86 | /** 87 | * Obtains the string representation of this object. 88 | */ 89 | public String toString() { 90 | return Double.toString(getValue()); 91 | } 92 | 93 | /** 94 | * Writes the value. 95 | */ 96 | public void write(AnnotationsWriter writer) throws IOException { 97 | writer.constValueIndex(getValue()); 98 | } 99 | 100 | /** 101 | * Accepts a visitor. 102 | */ 103 | public void accept(MemberValueVisitor visitor) { 104 | visitor.visitDoubleMemberValue(this); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/FloatMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * Floating-point number constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | * @version $Revision: 1.7 $ 31 | */ 32 | public class FloatMemberValue extends MemberValue { 33 | int valueIndex; 34 | 35 | /** 36 | * Constructs a float constant value. The initial value is specified 37 | * by the constant pool entry at the given index. 38 | * 39 | * @param index the index of a CONSTANT_Float_info structure. 40 | */ 41 | public FloatMemberValue(int index, ConstPool cp) { 42 | super('F', cp); 43 | this.valueIndex = index; 44 | } 45 | 46 | /** 47 | * Constructs a float constant value. 48 | * 49 | * @param f the initial value. 50 | */ 51 | public FloatMemberValue(float f, ConstPool cp) { 52 | super('F', cp); 53 | setValue(f); 54 | } 55 | 56 | /** 57 | * Constructs a float constant value. The initial value is 0.0. 58 | */ 59 | public FloatMemberValue(ConstPool cp) { 60 | super('F', cp); 61 | setValue(0.0F); 62 | } 63 | 64 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 65 | return new Float(getValue()); 66 | } 67 | 68 | Class getType(ClassLoader cl) { 69 | return float.class; 70 | } 71 | 72 | /** 73 | * Obtains the value of the member. 74 | */ 75 | public float getValue() { 76 | return cp.getFloatInfo(valueIndex); 77 | } 78 | 79 | /** 80 | * Sets the value of the member. 81 | */ 82 | public void setValue(float newValue) { 83 | valueIndex = cp.addFloatInfo(newValue); 84 | } 85 | 86 | /** 87 | * Obtains the string representation of this object. 88 | */ 89 | public String toString() { 90 | return Float.toString(getValue()); 91 | } 92 | 93 | /** 94 | * Writes the value. 95 | */ 96 | public void write(AnnotationsWriter writer) throws IOException { 97 | writer.constValueIndex(getValue()); 98 | } 99 | 100 | /** 101 | * Accepts a visitor. 102 | */ 103 | public void accept(MemberValueVisitor visitor) { 104 | visitor.visitFloatMemberValue(this); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/LongMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * Long integer constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | */ 31 | public class LongMemberValue extends MemberValue { 32 | int valueIndex; 33 | 34 | /** 35 | * Constructs a long constant value. The initial value is specified 36 | * by the constant pool entry at the given index. 37 | * 38 | * @param index the index of a CONSTANT_Long_info structure. 39 | */ 40 | public LongMemberValue(int index, ConstPool cp) { 41 | super('J', cp); 42 | this.valueIndex = index; 43 | } 44 | 45 | /** 46 | * Constructs a long constant value. 47 | * 48 | * @param j the initial value. 49 | */ 50 | public LongMemberValue(long j, ConstPool cp) { 51 | super('J', cp); 52 | setValue(j); 53 | } 54 | 55 | /** 56 | * Constructs a long constant value. The initial value is 0. 57 | */ 58 | public LongMemberValue(ConstPool cp) { 59 | super('J', cp); 60 | setValue(0L); 61 | } 62 | 63 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 64 | return new Long(getValue()); 65 | } 66 | 67 | Class getType(ClassLoader cl) { 68 | return long.class; 69 | } 70 | 71 | /** 72 | * Obtains the value of the member. 73 | */ 74 | public long getValue() { 75 | return cp.getLongInfo(valueIndex); 76 | } 77 | 78 | /** 79 | * Sets the value of the member. 80 | */ 81 | public void setValue(long newValue) { 82 | valueIndex = cp.addLongInfo(newValue); 83 | } 84 | 85 | /** 86 | * Obtains the string representation of this object. 87 | */ 88 | public String toString() { 89 | return Long.toString(getValue()); 90 | } 91 | 92 | /** 93 | * Writes the value. 94 | */ 95 | public void write(AnnotationsWriter writer) throws IOException { 96 | writer.constValueIndex(getValue()); 97 | } 98 | 99 | /** 100 | * Accepts a visitor. 101 | */ 102 | public void accept(MemberValueVisitor visitor) { 103 | visitor.visitLongMemberValue(this); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/MemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | import org.xxxx.javassist.bytecode.Descriptor; 22 | 23 | import java.io.IOException; 24 | import java.lang.reflect.Method; 25 | 26 | /** 27 | * The value of a member declared in an annotation. 28 | * 29 | * @see Annotation#getMemberValue(String) 30 | * @author Bill Burke 31 | * @author Shigeru Chiba 32 | */ 33 | public abstract class MemberValue { 34 | ConstPool cp; 35 | char tag; 36 | 37 | MemberValue(char tag, ConstPool cp) { 38 | this.cp = cp; 39 | this.tag = tag; 40 | } 41 | 42 | /** 43 | * Returns the value. If the value type is a primitive type, the 44 | * returned value is boxed. 45 | */ 46 | abstract Object getValue(ClassLoader cl, ClassPool cp, Method m) 47 | throws ClassNotFoundException; 48 | 49 | abstract Class getType(ClassLoader cl) throws ClassNotFoundException; 50 | 51 | static Class loadClass(ClassLoader cl, String classname) 52 | throws ClassNotFoundException, NoSuchClassError 53 | { 54 | try { 55 | return Class.forName(convertFromArray(classname), true, cl); 56 | } 57 | catch (LinkageError e) { 58 | throw new NoSuchClassError(classname, e); 59 | } 60 | } 61 | 62 | private static String convertFromArray(String classname) 63 | { 64 | int index = classname.indexOf("[]"); 65 | if (index != -1) { 66 | String rawType = classname.substring(0, index); 67 | StringBuffer sb = new StringBuffer(Descriptor.of(rawType)); 68 | while (index != -1) { 69 | sb.insert(0, "["); 70 | index = classname.indexOf("[]", index + 1); 71 | } 72 | return sb.toString().replace('/', '.'); 73 | } 74 | return classname; 75 | } 76 | 77 | /** 78 | * Accepts a visitor. 79 | */ 80 | public abstract void accept(MemberValueVisitor visitor); 81 | 82 | /** 83 | * Writes the value. 84 | */ 85 | public abstract void write(AnnotationsWriter w) throws IOException; 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/MemberValueVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | /** 20 | * Visitor for traversing member values included in an annotation. 21 | * 22 | * @see MemberValue#accept(MemberValueVisitor) 23 | * @author Bill Burke 24 | */ 25 | public interface MemberValueVisitor { 26 | public void visitAnnotationMemberValue(AnnotationMemberValue node); 27 | public void visitArrayMemberValue(ArrayMemberValue node); 28 | public void visitBooleanMemberValue(BooleanMemberValue node); 29 | public void visitByteMemberValue(ByteMemberValue node); 30 | public void visitCharMemberValue(CharMemberValue node); 31 | public void visitDoubleMemberValue(DoubleMemberValue node); 32 | public void visitEnumMemberValue(EnumMemberValue node); 33 | public void visitFloatMemberValue(FloatMemberValue node); 34 | public void visitIntegerMemberValue(IntegerMemberValue node); 35 | public void visitLongMemberValue(LongMemberValue node); 36 | public void visitShortMemberValue(ShortMemberValue node); 37 | public void visitStringMemberValue(StringMemberValue node); 38 | public void visitClassMemberValue(ClassMemberValue node); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/NoSuchClassError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | /** 20 | * Thrown if the linkage fails. 21 | * It keeps the name of the class that caused this error. 22 | */ 23 | public class NoSuchClassError extends Error { 24 | private String className; 25 | 26 | /** 27 | * Constructs an exception. 28 | */ 29 | public NoSuchClassError(String className, Error cause) { 30 | super(cause.toString(), cause); 31 | this.className = className; 32 | } 33 | 34 | /** 35 | * Returns the name of the class not found. 36 | */ 37 | public String getClassName() { 38 | return className; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/ShortMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * Short integer constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | */ 31 | public class ShortMemberValue extends MemberValue { 32 | int valueIndex; 33 | 34 | /** 35 | * Constructs a short constant value. The initial value is specified 36 | * by the constant pool entry at the given index. 37 | * 38 | * @param index the index of a CONSTANT_Integer_info structure. 39 | */ 40 | public ShortMemberValue(int index, ConstPool cp) { 41 | super('S', cp); 42 | this.valueIndex = index; 43 | } 44 | 45 | /** 46 | * Constructs a short constant value. 47 | * 48 | * @param s the initial value. 49 | */ 50 | public ShortMemberValue(short s, ConstPool cp) { 51 | super('S', cp); 52 | setValue(s); 53 | } 54 | 55 | /** 56 | * Constructs a short constant value. The initial value is 0. 57 | */ 58 | public ShortMemberValue(ConstPool cp) { 59 | super('S', cp); 60 | setValue((short)0); 61 | } 62 | 63 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 64 | return new Short(getValue()); 65 | } 66 | 67 | Class getType(ClassLoader cl) { 68 | return short.class; 69 | } 70 | 71 | /** 72 | * Obtains the value of the member. 73 | */ 74 | public short getValue() { 75 | return (short)cp.getIntegerInfo(valueIndex); 76 | } 77 | 78 | /** 79 | * Sets the value of the member. 80 | */ 81 | public void setValue(short newValue) { 82 | valueIndex = cp.addIntegerInfo(newValue); 83 | } 84 | 85 | /** 86 | * Obtains the string representation of this object. 87 | */ 88 | public String toString() { 89 | return Short.toString(getValue()); 90 | } 91 | 92 | /** 93 | * Writes the value. 94 | */ 95 | public void write(AnnotationsWriter writer) throws IOException { 96 | writer.constValueIndex(getValue()); 97 | } 98 | 99 | /** 100 | * Accepts a visitor. 101 | */ 102 | public void accept(MemberValueVisitor visitor) { 103 | visitor.visitShortMemberValue(this); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/StringMemberValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 2004 Bill Burke. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.annotation; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | import org.xxxx.javassist.bytecode.ConstPool; 21 | 22 | import java.io.IOException; 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * String constant value. 27 | * 28 | * @author Bill Burke 29 | * @author Shigeru Chiba 30 | */ 31 | public class StringMemberValue extends MemberValue { 32 | int valueIndex; 33 | 34 | /** 35 | * Constructs a string constant value. The initial value is specified 36 | * by the constant pool entry at the given index. 37 | * 38 | * @param index the index of a CONSTANT_Utf8_info structure. 39 | */ 40 | public StringMemberValue(int index, ConstPool cp) { 41 | super('s', cp); 42 | this.valueIndex = index; 43 | } 44 | 45 | /** 46 | * Constructs a string constant value. 47 | * 48 | * @param str the initial value. 49 | */ 50 | public StringMemberValue(String str, ConstPool cp) { 51 | super('s', cp); 52 | setValue(str); 53 | } 54 | 55 | /** 56 | * Constructs a string constant value. The initial value is "". 57 | */ 58 | public StringMemberValue(ConstPool cp) { 59 | super('s', cp); 60 | setValue(""); 61 | } 62 | 63 | Object getValue(ClassLoader cl, ClassPool cp, Method m) { 64 | return getValue(); 65 | } 66 | 67 | Class getType(ClassLoader cl) { 68 | return String.class; 69 | } 70 | 71 | /** 72 | * Obtains the value of the member. 73 | */ 74 | public String getValue() { 75 | return cp.getUtf8Info(valueIndex); 76 | } 77 | 78 | /** 79 | * Sets the value of the member. 80 | */ 81 | public void setValue(String newValue) { 82 | valueIndex = cp.addUtf8Info(newValue); 83 | } 84 | 85 | /** 86 | * Obtains the string representation of this object. 87 | */ 88 | public String toString() { 89 | return "\"" + getValue() + "\""; 90 | } 91 | 92 | /** 93 | * Writes the value. 94 | */ 95 | public void write(AnnotationsWriter writer) throws IOException { 96 | writer.constValueIndex(getValue()); 97 | } 98 | 99 | /** 100 | * Accepts a visitor. 101 | */ 102 | public void accept(MemberValueVisitor visitor) { 103 | visitor.visitStringMemberValue(this); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/annotation/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bytecode-level Annotations API. 4 | 5 |

This package provides low-level API for editing annotations attributes. 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bytecode-level API. 4 | 5 |

This package provides low-level API for editing a raw class file. 6 | It allows the users to read and modify a constant pool entry, a single 7 | bytecode instruction, and so on. 8 | 9 |

The users of this package must know the specifications of 10 | class file and Java bytecode. For more details, read this book: 11 | 12 |

    Tim Lindholm and Frank Yellin, 13 | "The Java Virtual Machine Specification 2nd Ed.", 14 | Addison-Wesley, 1999. 15 |
16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/bytecode/stackmap/TypeTag.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.bytecode.stackmap; 18 | 19 | import org.xxxx.javassist.bytecode.StackMapTable; 20 | 21 | public interface TypeTag { 22 | String TOP_TYPE = "*top*"; 23 | TypeData TOP = new TypeData.BasicType(TOP_TYPE, StackMapTable.TOP); 24 | TypeData INTEGER = new TypeData.BasicType("int", StackMapTable.INTEGER); 25 | TypeData FLOAT = new TypeData.BasicType("float", StackMapTable.FLOAT); 26 | TypeData DOUBLE = new TypeData.BasicType("double", StackMapTable.DOUBLE); 27 | TypeData LONG = new TypeData.BasicType("long", StackMapTable.LONG); 28 | 29 | // and NULL, THIS, OBJECT, UNINIT 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/CompileError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | import org.xxxx.javassist.CannotCompileException; 20 | import org.xxxx.javassist.NotFoundException; 21 | 22 | public class CompileError extends Exception { 23 | private Lex lex; 24 | private String reason; 25 | 26 | public CompileError(String s, Lex l) { 27 | reason = s; 28 | lex = l; 29 | } 30 | 31 | public CompileError(String s) { 32 | reason = s; 33 | lex = null; 34 | } 35 | 36 | public CompileError(CannotCompileException e) { 37 | this(e.getReason()); 38 | } 39 | 40 | public CompileError(NotFoundException e) { 41 | this("cannot find " + e.getMessage()); 42 | } 43 | 44 | public Lex getLex() { return lex; } 45 | 46 | public String getMessage() { 47 | return reason; 48 | } 49 | 50 | public String toString() { 51 | return "compile error: " + reason; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/KeywordTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | public final class KeywordTable extends java.util.HashMap { 20 | public KeywordTable() { super(); } 21 | 22 | public int lookup(String name) { 23 | Object found = get(name); 24 | if (found == null) 25 | return -1; 26 | else 27 | return ((Integer)found).intValue(); 28 | } 29 | 30 | public void append(String name, int t) { 31 | put(name, new Integer(t)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/NoFieldException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | import org.xxxx.javassist.compiler.ast.ASTree; 20 | 21 | public class NoFieldException extends CompileError { 22 | private String fieldName; 23 | private ASTree expr; 24 | 25 | /* NAME must be JVM-internal representation. 26 | */ 27 | public NoFieldException(String name, ASTree e) { 28 | super("no such field: " + name); 29 | fieldName = name; 30 | expr = e; 31 | } 32 | 33 | /* The returned name should be JVM-internal representation. 34 | */ 35 | public String getField() { return fieldName; } 36 | 37 | /* Returns the expression where this exception is thrown. 38 | */ 39 | public ASTree getExpr() { return expr; } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ProceedHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | import org.xxxx.javassist.bytecode.Bytecode; 20 | import org.xxxx.javassist.compiler.ast.ASTList; 21 | 22 | /** 23 | * An interface to an object for implementing $proceed(). 24 | * 25 | * @see JvstCodeGen#setProceedHandler(ProceedHandler, String) 26 | */ 27 | public interface ProceedHandler { 28 | void doit(JvstCodeGen gen, Bytecode b, ASTList args) throws CompileError; 29 | void setReturnType(JvstTypeChecker c, ASTList args) throws CompileError; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/SymbolTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | import org.xxxx.javassist.compiler.ast.Declarator; 20 | 21 | import java.util.HashMap; 22 | 23 | public final class SymbolTable extends HashMap { 24 | private SymbolTable parent; 25 | 26 | public SymbolTable() { this(null); } 27 | 28 | public SymbolTable(SymbolTable p) { 29 | super(); 30 | parent = p; 31 | } 32 | 33 | public SymbolTable getParent() { return parent; } 34 | 35 | public Declarator lookup(String name) { 36 | Declarator found = (Declarator)get(name); 37 | if (found == null && parent != null) 38 | return parent.lookup(name); 39 | else 40 | return found; 41 | } 42 | 43 | public void append(String name, Declarator value) { 44 | put(name, value); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/SyntaxError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler; 18 | 19 | public class SyntaxError extends CompileError { 20 | public SyntaxError(Lex lexer) { 21 | super("syntax error near \"" + lexer.getTextAround() + "\"", lexer); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/ASTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * Abstract Syntax Tree. An ASTree object represents a node of 25 | * a binary tree. If the node is a leaf node, both getLeft() 26 | * and getRight() returns null. 27 | */ 28 | public abstract class ASTree implements Serializable { 29 | public ASTree getLeft() { return null; } 30 | 31 | public ASTree getRight() { return null; } 32 | 33 | public void setLeft(ASTree _left) {} 34 | 35 | public void setRight(ASTree _right) {} 36 | 37 | /** 38 | * Is a method for the visitor pattern. It calls 39 | * atXXX() on the given visitor, where 40 | * XXX is the class name of the node object. 41 | */ 42 | public abstract void accept(Visitor v) throws CompileError; 43 | 44 | public String toString() { 45 | StringBuffer sbuf = new StringBuffer(); 46 | sbuf.append('<'); 47 | sbuf.append(getTag()); 48 | sbuf.append('>'); 49 | return sbuf.toString(); 50 | } 51 | 52 | /** 53 | * Returns the type of this node. This method is used by 54 | * toString(). 55 | */ 56 | protected String getTag() { 57 | String name = getClass().getName(); 58 | return name.substring(name.lastIndexOf('.') + 1); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/ArrayInit.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Array initializer such as { 1, 2, 3 }. 23 | */ 24 | public class ArrayInit extends ASTList { 25 | public ArrayInit(ASTree firstElement) { 26 | super(firstElement); 27 | } 28 | 29 | public void accept(Visitor v) throws CompileError { v.atArrayInit(this); } 30 | 31 | public String getTag() { return "array"; } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/AssignExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Assignment expression. 23 | */ 24 | public class AssignExpr extends Expr { 25 | /* operator must be either of: 26 | * =, %=, &=, *=, +=, -=, /=, ^=, |=, <<=, >>=, >>>= 27 | */ 28 | 29 | private AssignExpr(int op, ASTree _head, ASTList _tail) { 30 | super(op, _head, _tail); 31 | } 32 | 33 | public static AssignExpr makeAssign(int op, ASTree oprand1, 34 | ASTree oprand2) { 35 | return new AssignExpr(op, oprand1, new ASTList(oprand2)); 36 | } 37 | 38 | public void accept(Visitor v) throws CompileError { 39 | v.atAssignExpr(this); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/BinExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Binary expression. 23 | * 24 | *

If the operator is +, the right node might be null. 25 | * See TypeChecker.atBinExpr(). 26 | */ 27 | public class BinExpr extends Expr { 28 | /* operator must be either of: 29 | * ||, &&, |, ^, &, ==, !=, <=, >=, <, >, 30 | * <<, >>, >>>, +, -, *, /, % 31 | */ 32 | 33 | private BinExpr(int op, ASTree _head, ASTList _tail) { 34 | super(op, _head, _tail); 35 | } 36 | 37 | public static BinExpr makeBin(int op, ASTree oprand1, ASTree oprand2) { 38 | return new BinExpr(op, oprand1, new ASTList(oprand2)); 39 | } 40 | 41 | public void accept(Visitor v) throws CompileError { v.atBinExpr(this); } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/CallExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.MemberResolver; 21 | 22 | /** 23 | * Method call expression. 24 | */ 25 | public class CallExpr extends Expr { 26 | private MemberResolver.Method method; // cached result of lookupMethod() 27 | 28 | private CallExpr(ASTree _head, ASTList _tail) { 29 | super(CALL, _head, _tail); 30 | method = null; 31 | } 32 | 33 | public void setMethod(MemberResolver.Method m) { 34 | method = m; 35 | } 36 | 37 | public MemberResolver.Method getMethod() { 38 | return method; 39 | } 40 | 41 | public static CallExpr makeCall(ASTree target, ASTree args) { 42 | return new CallExpr(target, new ASTList(args)); 43 | } 44 | 45 | public void accept(Visitor v) throws CompileError { v.atCallExpr(this); } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/CastExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.TokenId; 21 | 22 | /** 23 | * Cast expression. 24 | */ 25 | public class CastExpr extends ASTList implements TokenId { 26 | protected int castType; 27 | protected int arrayDim; 28 | 29 | public CastExpr(ASTList className, int dim, ASTree expr) { 30 | super(className, new ASTList(expr)); 31 | castType = CLASS; 32 | arrayDim = dim; 33 | } 34 | 35 | public CastExpr(int type, int dim, ASTree expr) { 36 | super(null, new ASTList(expr)); 37 | castType = type; 38 | arrayDim = dim; 39 | } 40 | 41 | /* Returns CLASS, BOOLEAN, INT, or ... 42 | */ 43 | public int getType() { return castType; } 44 | 45 | public int getArrayDim() { return arrayDim; } 46 | 47 | public ASTList getClassName() { return (ASTList)getLeft(); } 48 | 49 | public ASTree getOprand() { return getRight().getLeft(); } 50 | 51 | public void setOprand(ASTree t) { getRight().setLeft(t); } 52 | 53 | public String getTag() { return "cast:" + castType + ":" + arrayDim; } 54 | 55 | public void accept(Visitor v) throws CompileError { v.atCastExpr(this); } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/CondExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Conditional expression. 23 | */ 24 | public class CondExpr extends ASTList { 25 | public CondExpr(ASTree cond, ASTree thenp, ASTree elsep) { 26 | super(cond, new ASTList(thenp, new ASTList(elsep))); 27 | } 28 | 29 | public ASTree condExpr() { return head(); } 30 | 31 | public void setCond(ASTree t) { setHead(t); } 32 | 33 | public ASTree thenExpr() { return tail().head(); } 34 | 35 | public void setThen(ASTree t) { tail().setHead(t); } 36 | 37 | public ASTree elseExpr() { return tail().tail().head(); } 38 | 39 | public void setElse(ASTree t) { tail().tail().setHead(t); } 40 | 41 | public String getTag() { return "?:"; } 42 | 43 | public void accept(Visitor v) throws CompileError { v.atCondExpr(this); } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/DoubleConst.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.TokenId; 21 | 22 | /** 23 | * Double constant. 24 | */ 25 | public class DoubleConst extends ASTree { 26 | protected double value; 27 | protected int type; 28 | 29 | public DoubleConst(double v, int tokenId) { value = v; type = tokenId; } 30 | 31 | public double get() { return value; } 32 | 33 | public void set(double v) { value = v; } 34 | 35 | /* Returns DoubleConstant or FloatConstant 36 | */ 37 | public int getType() { return type; } 38 | 39 | public String toString() { return Double.toString(value); } 40 | 41 | public void accept(Visitor v) throws CompileError { 42 | v.atDoubleConst(this); 43 | } 44 | 45 | public ASTree compute(int op, ASTree right) { 46 | if (right instanceof IntConst) 47 | return compute0(op, (IntConst)right); 48 | else if (right instanceof DoubleConst) 49 | return compute0(op, (DoubleConst)right); 50 | else 51 | return null; 52 | } 53 | 54 | private DoubleConst compute0(int op, DoubleConst right) { 55 | int newType; 56 | if (this.type == TokenId.DoubleConstant 57 | || right.type == TokenId.DoubleConstant) 58 | newType = TokenId.DoubleConstant; 59 | else 60 | newType = TokenId.FloatConstant; 61 | 62 | return compute(op, this.value, right.value, newType); 63 | } 64 | 65 | private DoubleConst compute0(int op, IntConst right) { 66 | return compute(op, this.value, (double)right.value, this.type); 67 | } 68 | 69 | private static DoubleConst compute(int op, double value1, double value2, 70 | int newType) 71 | { 72 | double newValue; 73 | switch (op) { 74 | case '+' : 75 | newValue = value1 + value2; 76 | break; 77 | case '-' : 78 | newValue = value1 - value2; 79 | break; 80 | case '*' : 81 | newValue = value1 * value2; 82 | break; 83 | case '/' : 84 | newValue = value1 / value2; 85 | break; 86 | case '%' : 87 | newValue = value1 % value2; 88 | break; 89 | default : 90 | return null; 91 | } 92 | 93 | return new DoubleConst(newValue, newType); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Expr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.TokenId; 21 | 22 | /** 23 | * Expression. 24 | */ 25 | public class Expr extends ASTList implements TokenId { 26 | /* operator must be either of: 27 | * (unary) +, (unary) -, ++, --, !, ~, 28 | * ARRAY, . (dot), MEMBER (static member access). 29 | * Otherwise, the object should be an instance of a subclass. 30 | */ 31 | 32 | protected int operatorId; 33 | 34 | Expr(int op, ASTree _head, ASTList _tail) { 35 | super(_head, _tail); 36 | operatorId = op; 37 | } 38 | 39 | Expr(int op, ASTree _head) { 40 | super(_head); 41 | operatorId = op; 42 | } 43 | 44 | public static Expr make(int op, ASTree oprand1, ASTree oprand2) { 45 | return new Expr(op, oprand1, new ASTList(oprand2)); 46 | } 47 | 48 | public static Expr make(int op, ASTree oprand1) { 49 | return new Expr(op, oprand1); 50 | } 51 | 52 | public int getOperator() { return operatorId; } 53 | 54 | public void setOperator(int op) { operatorId = op; } 55 | 56 | public ASTree oprand1() { return getLeft(); } 57 | 58 | public void setOprand1(ASTree expr) { 59 | setLeft(expr); 60 | } 61 | 62 | public ASTree oprand2() { return getRight().getLeft(); } 63 | 64 | public void setOprand2(ASTree expr) { 65 | getRight().setLeft(expr); 66 | } 67 | 68 | public void accept(Visitor v) throws CompileError { v.atExpr(this); } 69 | 70 | public String getName() { 71 | int id = operatorId; 72 | if (id < 128) 73 | return String.valueOf((char)id); 74 | else if (NEQ <= id && id <= ARSHIFT_E) 75 | return opNames[id - NEQ]; 76 | else if (id == INSTANCEOF) 77 | return "instanceof"; 78 | else 79 | return String.valueOf(id); 80 | } 81 | 82 | protected String getTag() { 83 | return "op:" + getName(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/FieldDecl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | public class FieldDecl extends ASTList { 22 | public FieldDecl(ASTree _head, ASTList _tail) { 23 | super(_head, _tail); 24 | } 25 | 26 | public ASTList getModifiers() { return (ASTList)getLeft(); } 27 | 28 | public Declarator getDeclarator() { return (Declarator)tail().head(); } 29 | 30 | public ASTree getInit() { return (ASTree)sublist(2).head(); } 31 | 32 | public void accept(Visitor v) throws CompileError { 33 | v.atFieldDecl(this); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/InstanceOfExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Instanceof expression. 23 | */ 24 | public class InstanceOfExpr extends CastExpr { 25 | public InstanceOfExpr(ASTList className, int dim, ASTree expr) { 26 | super(className, dim, expr); 27 | } 28 | 29 | public InstanceOfExpr(int type, int dim, ASTree expr) { 30 | super(type, dim, expr); 31 | } 32 | 33 | public String getTag() { 34 | return "instanceof:" + castType + ":" + arrayDim; 35 | } 36 | 37 | public void accept(Visitor v) throws CompileError { 38 | v.atInstanceOfExpr(this); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Keyword.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Keyword. 23 | */ 24 | public class Keyword extends ASTree { 25 | protected int tokenId; 26 | 27 | public Keyword(int token) { 28 | tokenId = token; 29 | } 30 | 31 | public int get() { return tokenId; } 32 | 33 | public String toString() { return "id:" + tokenId; } 34 | 35 | public void accept(Visitor v) throws CompileError { v.atKeyword(this); } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Member.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.CtField; 20 | import org.xxxx.javassist.compiler.CompileError; 21 | 22 | /** 23 | * Member name. 24 | */ 25 | public class Member extends Symbol { 26 | // cache maintained by fieldAccess() in TypeChecker. 27 | // this is used to obtain the value of a static final field. 28 | private CtField field; 29 | 30 | public Member(String name) { 31 | super(name); 32 | field = null; 33 | } 34 | 35 | public void setField(CtField f) { field = f; } 36 | 37 | public CtField getField() { return field; } 38 | 39 | public void accept(Visitor v) throws CompileError { v.atMember(this); } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/MethodDecl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | public class MethodDecl extends ASTList { 22 | public static final String initName = ""; 23 | 24 | public MethodDecl(ASTree _head, ASTList _tail) { 25 | super(_head, _tail); 26 | } 27 | 28 | public boolean isConstructor() { 29 | Symbol sym = getReturn().getVariable(); 30 | return sym != null && initName.equals(sym.get()); 31 | } 32 | 33 | public ASTList getModifiers() { return (ASTList)getLeft(); } 34 | 35 | public Declarator getReturn() { return (Declarator)tail().head(); } 36 | 37 | public ASTList getParams() { return (ASTList)sublist(2).head(); } 38 | 39 | public ASTList getThrows() { return (ASTList)sublist(3).head(); } 40 | 41 | public Stmnt getBody() { return (Stmnt)sublist(4).head(); } 42 | 43 | public void accept(Visitor v) throws CompileError { 44 | v.atMethodDecl(this); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/NewExpr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.TokenId; 21 | 22 | /** 23 | * New Expression. 24 | */ 25 | public class NewExpr extends ASTList implements TokenId { 26 | protected boolean newArray; 27 | protected int arrayType; 28 | 29 | public NewExpr(ASTList className, ASTList args) { 30 | super(className, new ASTList(args)); 31 | newArray = false; 32 | arrayType = CLASS; 33 | } 34 | 35 | public NewExpr(int type, ASTList arraySize, ArrayInit init) { 36 | super(null, new ASTList(arraySize)); 37 | newArray = true; 38 | arrayType = type; 39 | if (init != null) 40 | append(this, init); 41 | } 42 | 43 | public static NewExpr makeObjectArray(ASTList className, 44 | ASTList arraySize, ArrayInit init) { 45 | NewExpr e = new NewExpr(className, arraySize); 46 | e.newArray = true; 47 | if (init != null) 48 | append(e, init); 49 | 50 | return e; 51 | } 52 | 53 | public boolean isArray() { return newArray; } 54 | 55 | /* TokenId.CLASS, TokenId.INT, ... 56 | */ 57 | public int getArrayType() { return arrayType; } 58 | 59 | public ASTList getClassName() { return (ASTList)getLeft(); } 60 | 61 | public ASTList getArguments() { return (ASTList)getRight().getLeft(); } 62 | 63 | public ASTList getArraySize() { return getArguments(); } 64 | 65 | public ArrayInit getInitializer() { 66 | ASTree t = getRight().getRight(); 67 | if (t == null) 68 | return null; 69 | else 70 | return (ArrayInit)t.getLeft(); 71 | } 72 | 73 | public void accept(Visitor v) throws CompileError { v.atNewExpr(this); } 74 | 75 | protected String getTag() { 76 | return newArray ? "new[]" : "new"; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Pair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * A node of a a binary tree. This class provides concrete methods 23 | * overriding abstract methods in ASTree. 24 | */ 25 | public class Pair extends ASTree { 26 | protected ASTree left, right; 27 | 28 | public Pair(ASTree _left, ASTree _right) { 29 | left = _left; 30 | right = _right; 31 | } 32 | 33 | public void accept(Visitor v) throws CompileError { v.atPair(this); } 34 | 35 | public String toString() { 36 | StringBuffer sbuf = new StringBuffer(); 37 | sbuf.append("( "); 38 | sbuf.append(left == null ? "" : left.toString()); 39 | sbuf.append(" . "); 40 | sbuf.append(right == null ? "" : right.toString()); 41 | sbuf.append(')'); 42 | return sbuf.toString(); 43 | } 44 | 45 | public ASTree getLeft() { return left; } 46 | 47 | public ASTree getRight() { return right; } 48 | 49 | public void setLeft(ASTree _left) { left = _left; } 50 | 51 | public void setRight(ASTree _right) { right = _right; } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Stmnt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | import org.xxxx.javassist.compiler.TokenId; 21 | 22 | /** 23 | * Statement. 24 | */ 25 | public class Stmnt extends ASTList implements TokenId { 26 | protected int operatorId; 27 | 28 | public Stmnt(int op, ASTree _head, ASTList _tail) { 29 | super(_head, _tail); 30 | operatorId = op; 31 | } 32 | 33 | public Stmnt(int op, ASTree _head) { 34 | super(_head); 35 | operatorId = op; 36 | } 37 | 38 | public Stmnt(int op) { 39 | this(op, null); 40 | } 41 | 42 | public static Stmnt make(int op, ASTree oprand1, ASTree oprand2) { 43 | return new Stmnt(op, oprand1, new ASTList(oprand2)); 44 | } 45 | 46 | public static Stmnt make(int op, ASTree op1, ASTree op2, ASTree op3) { 47 | return new Stmnt(op, op1, new ASTList(op2, new ASTList(op3))); 48 | } 49 | 50 | public void accept(Visitor v) throws CompileError { v.atStmnt(this); } 51 | 52 | public int getOperator() { return operatorId; } 53 | 54 | protected String getTag() { 55 | if (operatorId < 128) 56 | return "stmnt:" + (char)operatorId; 57 | else 58 | return "stmnt:" + operatorId; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/StringL.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * String literal. 23 | */ 24 | public class StringL extends ASTree { 25 | protected String text; 26 | 27 | public StringL(String t) { 28 | text = t; 29 | } 30 | 31 | public String get() { return text; } 32 | 33 | public String toString() { return "\"" + text + "\""; } 34 | 35 | public void accept(Visitor v) throws CompileError { v.atStringL(this); } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Symbol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Identifier. 23 | */ 24 | public class Symbol extends ASTree { 25 | protected String identifier; 26 | 27 | public Symbol(String sym) { 28 | identifier = sym; 29 | } 30 | 31 | public String get() { return identifier; } 32 | 33 | public String toString() { return identifier; } 34 | 35 | public void accept(Visitor v) throws CompileError { v.atSymbol(this); } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Variable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * Variable. 23 | */ 24 | public class Variable extends Symbol { 25 | protected Declarator declarator; 26 | 27 | public Variable(String sym, Declarator d) { 28 | super(sym); 29 | declarator = d; 30 | } 31 | 32 | public Declarator getDeclarator() { return declarator; } 33 | 34 | public String toString() { 35 | return identifier + ":" + declarator.getType(); 36 | } 37 | 38 | public void accept(Visitor v) throws CompileError { v.atVariable(this); } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/compiler/ast/Visitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.compiler.ast; 18 | 19 | import org.xxxx.javassist.compiler.CompileError; 20 | 21 | /** 22 | * The visitor pattern. 23 | * 24 | * @see ASTree#accept(Visitor) 25 | */ 26 | public class Visitor { 27 | public void atASTList(ASTList n) throws CompileError {} 28 | public void atPair(Pair n) throws CompileError {} 29 | 30 | public void atFieldDecl(FieldDecl n) throws CompileError {} 31 | public void atMethodDecl(MethodDecl n) throws CompileError {} 32 | public void atStmnt(Stmnt n) throws CompileError {} 33 | public void atDeclarator(Declarator n) throws CompileError {} 34 | 35 | public void atAssignExpr(AssignExpr n) throws CompileError {} 36 | public void atCondExpr(CondExpr n) throws CompileError {} 37 | public void atBinExpr(BinExpr n) throws CompileError {} 38 | public void atExpr(Expr n) throws CompileError {} 39 | public void atCallExpr(CallExpr n) throws CompileError {} 40 | public void atCastExpr(CastExpr n) throws CompileError {} 41 | public void atInstanceOfExpr(InstanceOfExpr n) throws CompileError {} 42 | public void atNewExpr(NewExpr n) throws CompileError {} 43 | 44 | public void atSymbol(Symbol n) throws CompileError {} 45 | public void atMember(Member n) throws CompileError {} 46 | public void atVariable(Variable n) throws CompileError {} 47 | public void atKeyword(Keyword n) throws CompileError {} 48 | public void atStringL(StringL n) throws CompileError {} 49 | public void atIntConst(IntConst n) throws CompileError {} 50 | public void atDoubleConst(DoubleConst n) throws CompileError {} 51 | public void atArrayInit(ArrayInit n) throws CompileError {} 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/convert/TransformAfter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.convert; 18 | 19 | import org.xxxx.javassist.CtMethod; 20 | import org.xxxx.javassist.NotFoundException; 21 | import org.xxxx.javassist.bytecode.*; 22 | 23 | public class TransformAfter extends TransformBefore { 24 | public TransformAfter(Transformer next, 25 | CtMethod origMethod, CtMethod afterMethod) 26 | throws NotFoundException 27 | { 28 | super(next, origMethod, afterMethod); 29 | } 30 | 31 | protected int match2(int pos, CodeIterator iterator) throws BadBytecode { 32 | iterator.move(pos); 33 | iterator.insert(saveCode); 34 | iterator.insert(loadCode); 35 | int p = iterator.insertGap(3); 36 | iterator.setMark(p); 37 | iterator.insert(loadCode); 38 | pos = iterator.next(); 39 | p = iterator.getMark(); 40 | iterator.writeByte(iterator.byteAt(pos), p); 41 | iterator.write16bit(iterator.u16bitAt(pos + 1), p + 1); 42 | iterator.writeByte(INVOKESTATIC, pos); 43 | iterator.write16bit(newIndex, pos + 1); 44 | iterator.move(p); 45 | return iterator.next(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/convert/TransformFieldAccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.convert; 18 | 19 | import org.xxxx.javassist.CtClass; 20 | import org.xxxx.javassist.CtField; 21 | import org.xxxx.javassist.Modifier; 22 | import org.xxxx.javassist.bytecode.*; 23 | 24 | final public class TransformFieldAccess extends Transformer { 25 | private String newClassname, newFieldname; 26 | private String fieldname; 27 | private CtClass fieldClass; 28 | private boolean isPrivate; 29 | 30 | /* cache */ 31 | private int newIndex; 32 | private ConstPool constPool; 33 | 34 | public TransformFieldAccess(Transformer next, CtField field, 35 | String newClassname, String newFieldname) 36 | { 37 | super(next); 38 | this.fieldClass = field.getDeclaringClass(); 39 | this.fieldname = field.getName(); 40 | this.isPrivate = Modifier.isPrivate(field.getModifiers()); 41 | this.newClassname = newClassname; 42 | this.newFieldname = newFieldname; 43 | this.constPool = null; 44 | } 45 | 46 | public void initialize(ConstPool cp, CodeAttribute attr) { 47 | if (constPool != cp) 48 | newIndex = 0; 49 | } 50 | 51 | /** 52 | * Modify GETFIELD, GETSTATIC, PUTFIELD, and PUTSTATIC so that 53 | * a different field is accessed. The new field must be declared 54 | * in a superclass of the class in which the original field is 55 | * declared. 56 | */ 57 | public int transform(CtClass clazz, int pos, 58 | CodeIterator iterator, ConstPool cp) 59 | { 60 | int c = iterator.byteAt(pos); 61 | if (c == GETFIELD || c == GETSTATIC 62 | || c == PUTFIELD || c == PUTSTATIC) { 63 | int index = iterator.u16bitAt(pos + 1); 64 | String typedesc 65 | = TransformReadField.isField(clazz.getClassPool(), cp, 66 | fieldClass, fieldname, isPrivate, index); 67 | if (typedesc != null) { 68 | if (newIndex == 0) { 69 | int nt = cp.addNameAndTypeInfo(newFieldname, 70 | typedesc); 71 | newIndex = cp.addFieldrefInfo( 72 | cp.addClassInfo(newClassname), nt); 73 | constPool = cp; 74 | } 75 | 76 | iterator.write16bit(newIndex, pos + 1); 77 | } 78 | } 79 | 80 | return pos; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/convert/TransformNewClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.convert; 18 | 19 | import org.xxxx.javassist.CannotCompileException; 20 | import org.xxxx.javassist.CtClass; 21 | import org.xxxx.javassist.bytecode.*; 22 | 23 | final public class TransformNewClass extends Transformer { 24 | private int nested; 25 | private String classname, newClassName; 26 | private int newClassIndex, newMethodNTIndex, newMethodIndex; 27 | 28 | public TransformNewClass(Transformer next, 29 | String classname, String newClassName) { 30 | super(next); 31 | this.classname = classname; 32 | this.newClassName = newClassName; 33 | } 34 | 35 | public void initialize(ConstPool cp, CodeAttribute attr) { 36 | nested = 0; 37 | newClassIndex = newMethodNTIndex = newMethodIndex = 0; 38 | } 39 | 40 | /** 41 | * Modifies a sequence of 42 | * NEW classname 43 | * DUP 44 | * ... 45 | * INVOKESPECIAL classname:method 46 | */ 47 | public int transform(CtClass clazz, int pos, CodeIterator iterator, 48 | ConstPool cp) throws CannotCompileException 49 | { 50 | int index; 51 | int c = iterator.byteAt(pos); 52 | if (c == NEW) { 53 | index = iterator.u16bitAt(pos + 1); 54 | if (cp.getClassInfo(index).equals(classname)) { 55 | if (iterator.byteAt(pos + 3) != DUP) 56 | throw new CannotCompileException( 57 | "NEW followed by no DUP was found"); 58 | 59 | if (newClassIndex == 0) 60 | newClassIndex = cp.addClassInfo(newClassName); 61 | 62 | iterator.write16bit(newClassIndex, pos + 1); 63 | ++nested; 64 | } 65 | } 66 | else if (c == INVOKESPECIAL) { 67 | index = iterator.u16bitAt(pos + 1); 68 | int typedesc = cp.isConstructor(classname, index); 69 | if (typedesc != 0 && nested > 0) { 70 | int nt = cp.getMethodrefNameAndType(index); 71 | if (newMethodNTIndex != nt) { 72 | newMethodNTIndex = nt; 73 | newMethodIndex = cp.addMethodrefInfo(newClassIndex, nt); 74 | } 75 | 76 | iterator.write16bit(newMethodIndex, pos + 1); 77 | --nested; 78 | } 79 | } 80 | 81 | return pos; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/convert/TransformWriteField.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.convert; 18 | 19 | import org.xxxx.javassist.CtClass; 20 | import org.xxxx.javassist.CtField; 21 | import org.xxxx.javassist.bytecode.*; 22 | 23 | final public class TransformWriteField extends TransformReadField { 24 | public TransformWriteField(Transformer next, CtField field, 25 | String methodClassname, String methodName) 26 | { 27 | super(next, field, methodClassname, methodName); 28 | } 29 | 30 | public int transform(CtClass tclazz, int pos, CodeIterator iterator, 31 | ConstPool cp) throws BadBytecode 32 | { 33 | int c = iterator.byteAt(pos); 34 | if (c == PUTFIELD || c == PUTSTATIC) { 35 | int index = iterator.u16bitAt(pos + 1); 36 | String typedesc = isField(tclazz.getClassPool(), cp, 37 | fieldClass, fieldname, isPrivate, index); 38 | if (typedesc != null) { 39 | if (c == PUTSTATIC) { 40 | CodeAttribute ca = iterator.get(); 41 | iterator.move(pos); 42 | char c0 = typedesc.charAt(0); 43 | if (c0 == 'J' || c0 == 'D') { // long or double 44 | // insertGap() may insert 4 bytes. 45 | pos = iterator.insertGap(3); 46 | iterator.writeByte(ACONST_NULL, pos); 47 | iterator.writeByte(DUP_X2, pos + 1); 48 | iterator.writeByte(POP, pos + 2); 49 | ca.setMaxStack(ca.getMaxStack() + 2); 50 | } 51 | else { 52 | // insertGap() may insert 4 bytes. 53 | pos = iterator.insertGap(2); 54 | iterator.writeByte(ACONST_NULL, pos); 55 | iterator.writeByte(SWAP, pos + 1); 56 | ca.setMaxStack(ca.getMaxStack() + 1); 57 | } 58 | 59 | pos = iterator.next(); 60 | } 61 | 62 | int mi = cp.addClassInfo(methodClassname); 63 | String type = "(Ljava/lang/Object;" + typedesc + ")V"; 64 | int methodref = cp.addMethodrefInfo(mi, methodName, type); 65 | iterator.writeByte(INVOKESTATIC, pos); 66 | iterator.write16bit(methodref, pos + 1); 67 | } 68 | } 69 | 70 | return pos; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/convert/Transformer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.convert; 18 | 19 | import org.xxxx.javassist.CannotCompileException; 20 | import org.xxxx.javassist.CtClass; 21 | import org.xxxx.javassist.bytecode.*; 22 | 23 | /** 24 | * Transformer and its subclasses are used for executing 25 | * code transformation specified by CodeConverter. 26 | * 27 | * @see org.xxxx.javassist.CodeConverter 28 | */ 29 | public abstract class Transformer implements Opcode { 30 | private Transformer next; 31 | 32 | public Transformer(Transformer t) { 33 | next = t; 34 | } 35 | 36 | public Transformer getNext() { return next; } 37 | 38 | public void initialize(ConstPool cp, CodeAttribute attr) {} 39 | 40 | public void initialize(ConstPool cp, CtClass clazz, MethodInfo minfo) throws CannotCompileException { 41 | initialize(cp, minfo.getCodeAttribute()); 42 | } 43 | 44 | public void clean() {} 45 | 46 | public abstract int transform(CtClass clazz, int pos, CodeIterator it, 47 | ConstPool cp) throws CannotCompileException, BadBytecode; 48 | 49 | public int extraLocals() { return 0; } 50 | 51 | public int extraStack() { return 0; } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/expr/ConstructorCall.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.expr; 18 | 19 | import org.xxxx.javassist.CtClass; 20 | import org.xxxx.javassist.CtConstructor; 21 | import org.xxxx.javassist.CtMethod; 22 | import org.xxxx.javassist.NotFoundException; 23 | import org.xxxx.javassist.bytecode.CodeIterator; 24 | import org.xxxx.javassist.bytecode.MethodInfo; 25 | 26 | /** 27 | * Constructor call such as this() and super() 28 | * within a constructor body. 29 | * 30 | * @see NewExpr 31 | */ 32 | public class ConstructorCall extends MethodCall { 33 | /** 34 | * Undocumented constructor. Do not use; internal-use only. 35 | */ 36 | protected ConstructorCall(int pos, CodeIterator i, CtClass decl, MethodInfo m) { 37 | super(pos, i, decl, m); 38 | } 39 | 40 | /** 41 | * Returns "super" or ""this". 42 | */ 43 | public String getMethodName() { 44 | return isSuper() ? "super" : "this"; 45 | } 46 | 47 | /** 48 | * Always throws a NotFoundException. 49 | * 50 | * @see #getConstructor() 51 | */ 52 | public CtMethod getMethod() throws NotFoundException { 53 | throw new NotFoundException("this is a constructor call. Call getConstructor()."); 54 | } 55 | 56 | /** 57 | * Returns the called constructor. 58 | */ 59 | public CtConstructor getConstructor() throws NotFoundException { 60 | return getCtClass().getConstructor(getSignature()); 61 | } 62 | 63 | /** 64 | * Returns true if the called constructor is not this() 65 | * but super() (a constructor declared in the super class). 66 | */ 67 | public boolean isSuper() { 68 | return super.isSuper(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/expr/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

This package contains the classes for modifying a method body. 5 | See ExprEditor (expression editor) for more details. 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | The Javassist Core API. 4 | 5 |

Javassist (Java programming assistant) makes bytecode 6 | engineering simple. It is a class library for editing 7 | bytecode in Java; it enables Java programs to define a new class at 8 | runtime and to modify a given class file when the JVM loads it. 9 | 10 |

The most significant class of this package is CtClass. 11 | See the description of this class first. 12 | 13 |

To know the version number of this package, type the following command: 14 | 15 |

    16 | java -jar org.xxxx.javassist.jar
    17 | 
18 | 19 |

It prints the version number on the console. 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/runtime/Cflow.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.runtime; 18 | 19 | /** 20 | * A support class for implementing $cflow. 21 | * This support class is required at runtime 22 | * only if $cflow is used. 23 | * 24 | * @see org.xxxx.javassist.CtBehavior#useCflow(String) 25 | */ 26 | public class Cflow extends ThreadLocal { 27 | private static class Depth { 28 | private int depth; 29 | Depth() { depth = 0; } 30 | int get() { return depth; } 31 | void inc() { ++depth; } 32 | void dec() { --depth; } 33 | } 34 | 35 | protected synchronized Object initialValue() { 36 | return new Depth(); 37 | } 38 | 39 | /** 40 | * Increments the counter. 41 | */ 42 | public void enter() { ((Depth)get()).inc(); } 43 | 44 | /** 45 | * Decrements the counter. 46 | */ 47 | public void exit() { ((Depth)get()).dec(); } 48 | 49 | /** 50 | * Returns the value of the counter. 51 | */ 52 | public int value() { return ((Depth)get()).get(); } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/runtime/DotClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.runtime; 18 | 19 | /** 20 | * A support class for implementing .class notation. 21 | * This is required at runtime 22 | * only if .class notation is used in source code given 23 | * to the Javassist compiler. 24 | */ 25 | public class DotClass { 26 | public static NoClassDefFoundError fail(ClassNotFoundException e) { 27 | return new NoClassDefFoundError(e.getMessage()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/runtime/Inner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.runtime; 18 | 19 | /** 20 | * A support class for compiling a method declared in an inner class. 21 | * This support class is required at runtime 22 | * only if the method calls a private constructor in the enclosing class. 23 | */ 24 | public class Inner { 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/runtime/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Runtime support classes required by modified bytecode. 4 | 5 |

This package includes support classes that may be required by 6 | classes modified with Javassist. Note that most of the modified 7 | classes do not require these support classes. See the documentation 8 | of every support class to know which kind of modification needs 9 | a support class. 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/scopedpool/ScopedClassPoolFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.scopedpool; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | 21 | /** 22 | * A factory interface. 23 | * 24 | * @author Kabir Khan 25 | * @version $Revision: 1.4 $ 26 | */ 27 | public interface ScopedClassPoolFactory { 28 | /** 29 | * Makes an instance. 30 | */ 31 | ScopedClassPool create(ClassLoader cl, ClassPool src, 32 | ScopedClassPoolRepository repository); 33 | 34 | /** 35 | * Makes an instance. 36 | */ 37 | ScopedClassPool create(ClassPool src, 38 | ScopedClassPoolRepository repository); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/scopedpool/ScopedClassPoolFactoryImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.scopedpool; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | 21 | /** 22 | * An implementation of factory. 23 | * 24 | * @author Kabir Khan 25 | * @version $Revision: 1.5 $ 26 | */ 27 | public class ScopedClassPoolFactoryImpl implements ScopedClassPoolFactory { 28 | /** 29 | * Makes an instance. 30 | */ 31 | public ScopedClassPool create(ClassLoader cl, ClassPool src, 32 | ScopedClassPoolRepository repository) { 33 | return new ScopedClassPool(cl, src, repository, false); 34 | } 35 | 36 | /** 37 | * Makes an instance. 38 | */ 39 | public ScopedClassPool create(ClassPool src, 40 | ScopedClassPoolRepository repository) { 41 | return new ScopedClassPool(null, src, repository, true); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/scopedpool/ScopedClassPoolRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.scopedpool; 18 | 19 | import org.xxxx.javassist.ClassPool; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | * An interface to ScopedClassPoolRepositoryImpl. 25 | * 26 | * @author Kabir Khan 27 | * @version $Revision: 1.4 $ 28 | */ 29 | public interface ScopedClassPoolRepository { 30 | /** 31 | * Records a factory. 32 | */ 33 | void setClassPoolFactory(ScopedClassPoolFactory factory); 34 | 35 | /** 36 | * Obtains the recorded factory. 37 | */ 38 | ScopedClassPoolFactory getClassPoolFactory(); 39 | 40 | /** 41 | * Returns whether or not the class pool is pruned. 42 | * 43 | * @return the prune. 44 | */ 45 | boolean isPrune(); 46 | 47 | /** 48 | * Sets the prune flag. 49 | * 50 | * @param prune a new value. 51 | */ 52 | void setPrune(boolean prune); 53 | 54 | /** 55 | * Create a scoped classpool. 56 | * 57 | * @param cl the classloader. 58 | * @param src the original classpool. 59 | * @return the classpool. 60 | */ 61 | ScopedClassPool createScopedClassPool(ClassLoader cl, ClassPool src); 62 | 63 | /** 64 | * Finds a scoped classpool registered under the passed in classloader. 65 | * 66 | * @param cl the classloader. 67 | * @return the classpool. 68 | */ 69 | ClassPool findClassPool(ClassLoader cl); 70 | 71 | /** 72 | * Register a classloader. 73 | * 74 | * @param ucl the classloader. 75 | * @return the classpool. 76 | */ 77 | ClassPool registerClassLoader(ClassLoader ucl); 78 | 79 | /** 80 | * Get the registered classloaders. 81 | * 82 | * @return the registered classloaders. 83 | */ 84 | Map getRegisteredCLs(); 85 | 86 | /** 87 | * This method will check to see if a register classloader has been 88 | * undeployed (as in JBoss). 89 | */ 90 | void clearUnregisteredClassLoaders(); 91 | 92 | /** 93 | * Unregisters a classpool and unregisters its classloader. 94 | * 95 | * @param cl the classloader the pool is stored under. 96 | */ 97 | void unregisterClassLoader(ClassLoader cl); 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/scopedpool/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

A custom class pool for several JBoss products. 4 | It is not part of Javassist. 5 |

6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/Dump.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools; 18 | 19 | import org.xxxx.javassist.bytecode.ClassFile; 20 | import org.xxxx.javassist.bytecode.ClassFilePrinter; 21 | 22 | import java.io.DataInputStream; 23 | import java.io.FileInputStream; 24 | import java.io.PrintWriter; 25 | 26 | /** 27 | * Dump is a tool for viewing the class definition in the given 28 | * class file. Unlike the JDK javap tool, Dump works even if 29 | * the class file is broken. 30 | * 31 | *

For example, 32 | *

% java org.xxxx.javassist.tools.Dump foo.class
33 | * 34 | *

prints the contents of the constant pool and the list of methods 35 | * and fields. 36 | */ 37 | public class Dump { 38 | private Dump() {} 39 | 40 | /** 41 | * Main method. 42 | * 43 | * @param args args[0] is the class file name. 44 | */ 45 | public static void main(String[] args) throws Exception { 46 | if (args.length != 1) { 47 | System.err.println("Usage: java Dump "); 48 | return; 49 | } 50 | 51 | DataInputStream in = new DataInputStream( 52 | new FileInputStream(args[0])); 53 | ClassFile w = new ClassFile(in); 54 | PrintWriter out = new PrintWriter(System.out, true); 55 | out.println("*** constant pool ***"); 56 | w.getConstPool().print(out); 57 | out.println(); 58 | out.println("*** members ***"); 59 | ClassFilePrinter.print(w, out); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/framedump.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | package org.xxxx.javassist.tools; 17 | 18 | import org.xxxx.javassist.ClassPool; 19 | import org.xxxx.javassist.CtClass; 20 | import org.xxxx.javassist.bytecode.analysis.FramePrinter; 21 | 22 | /** 23 | * framedump is a tool for viewing a merged combination of the instructions and frame state 24 | * of all methods in a class. 25 | * 26 | *

For example, 27 | *

% java org.xxxx.javassist.tools.framedump foo.class
28 | */ 29 | public class framedump { 30 | private framedump() {} 31 | 32 | /** 33 | * Main method. 34 | * 35 | * @param args args[0] is the class file name. 36 | */ 37 | public static void main(String[] args) throws Exception { 38 | if (args.length != 1) { 39 | System.err.println("Usage: java org.xxxx.javassist.tools.framedump "); 40 | return; 41 | } 42 | 43 | ClassPool pool = ClassPool.getDefault(); 44 | CtClass clazz = pool.get(args[0]); 45 | System.out.println("Frame Dump of " + clazz.getName() + ":"); 46 | FramePrinter.print(clazz, System.out); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Covenient tools. 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/CannotCreateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.reflect; 18 | 19 | /** 20 | * Signals that ClassMetaobject.newInstance() fails. 21 | */ 22 | public class CannotCreateException extends Exception { 23 | public CannotCreateException(String s) { 24 | super(s); 25 | } 26 | 27 | public CannotCreateException(Exception e) { 28 | super("by " + e.toString()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/CannotInvokeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.reflect; 18 | 19 | import java.lang.reflect.InvocationTargetException; 20 | 21 | /** 22 | * Thrown when method invocation using the reflection API has thrown 23 | * an exception. 24 | * 25 | * @see Metaobject#trapMethodcall(int, Object[]) 26 | * @see ClassMetaobject#trapMethodcall(int, Object[]) 27 | * @see ClassMetaobject#invoke(Object, int, Object[]) 28 | */ 29 | public class CannotInvokeException extends RuntimeException { 30 | 31 | private Throwable err = null; 32 | 33 | /** 34 | * Returns the cause of this exception. It may return null. 35 | */ 36 | public Throwable getReason() { return err; } 37 | 38 | /** 39 | * Constructs a CannotInvokeException with an error message. 40 | */ 41 | public CannotInvokeException(String reason) { 42 | super(reason); 43 | } 44 | 45 | /** 46 | * Constructs a CannotInvokeException with an InvocationTargetException. 47 | */ 48 | public CannotInvokeException(InvocationTargetException e) { 49 | super("by " + e.getTargetException().toString()); 50 | err = e.getTargetException(); 51 | } 52 | 53 | /** 54 | * Constructs a CannotInvokeException with an IllegalAccessException. 55 | */ 56 | public CannotInvokeException(IllegalAccessException e) { 57 | super("by " + e.toString()); 58 | err = e; 59 | } 60 | 61 | /** 62 | * Constructs a CannotInvokeException with an ClassNotFoundException. 63 | */ 64 | public CannotInvokeException(ClassNotFoundException e) { 65 | super("by " + e.toString()); 66 | err = e; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/CannotReflectException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.reflect; 18 | 19 | import org.xxxx.javassist.CannotCompileException; 20 | 21 | /** 22 | * Thrown by makeReflective() in Reflection 23 | * when there is an attempt to reflect 24 | * a class that is either an interface or a subclass of 25 | * either ClassMetaobject or Metaobject. 26 | * 27 | * @author Brett Randall 28 | * @see Reflection#makeReflective(CtClass,CtClass,CtClass) 29 | * @see CannotCompileException 30 | */ 31 | public class CannotReflectException extends CannotCompileException { 32 | public CannotReflectException(String msg) { 33 | super(msg); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/Metalevel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.reflect; 18 | 19 | /** 20 | * An interface to access a metaobject and a class metaobject. 21 | * This interface is implicitly implemented by the reflective 22 | * class. 23 | */ 24 | public interface Metalevel { 25 | /** 26 | * Obtains the class metaobject associated with this object. 27 | */ 28 | ClassMetaobject _getClass(); 29 | 30 | /** 31 | * Obtains the metaobject associated with this object. 32 | */ 33 | Metaobject _getMetaobject(); 34 | 35 | /** 36 | * Changes the metaobject associated with this object. 37 | */ 38 | void _setMetaobject(Metaobject m); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/Sample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.reflect; 18 | 19 | /** 20 | * A template used for defining a reflective class. 21 | */ 22 | public class Sample { 23 | private Metaobject _metaobject; 24 | private static ClassMetaobject _classobject; 25 | 26 | public Object trap(Object[] args, int identifier) throws Throwable { 27 | Metaobject mobj; 28 | mobj = _metaobject; 29 | if (mobj == null) 30 | return ClassMetaobject.invoke(this, identifier, args); 31 | else 32 | return mobj.trapMethodcall(identifier, args); 33 | } 34 | 35 | public static Object trapStatic(Object[] args, int identifier) 36 | throws Throwable 37 | { 38 | return _classobject.trapMethodcall(identifier, args); 39 | } 40 | 41 | public static Object trapRead(Object[] args, String name) { 42 | if (args[0] == null) 43 | return _classobject.trapFieldRead(name); 44 | else 45 | return ((Metalevel)args[0])._getMetaobject().trapFieldRead(name); 46 | } 47 | 48 | public static Object trapWrite(Object[] args, String name) { 49 | Metalevel base = (Metalevel)args[0]; 50 | if (base == null) 51 | _classobject.trapFieldWrite(name, args[1]); 52 | else 53 | base._getMetaobject().trapFieldWrite(name, args[1]); 54 | 55 | return null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/reflect/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Runtime Behavioral Reflection. 4 | 5 |

(also recently known as interceptors or AOP?) 6 | 7 |

This package enables a metaobject to trap method calls and field 8 | accesses on a regular Java object. It provides a class 9 | Reflection, which is a main module for implementing 10 | runtime behavioral reflection. 11 | It also provides 12 | a class Loader and Compiler 13 | as utilities for dynamically or statically 14 | translating a regular class into a reflective class. 15 | 16 |

An instance of the reflective class is associated with 17 | a runtime metaobject and a runtime class metaobject, which control 18 | the behavior of that instance. 19 | The runtime 20 | metaobject is created for every (base-level) instance but the 21 | runtime class metaobject is created for every (base-level) class. 22 | Metaobject is the root class of the runtime 23 | metaobject and ClassMetaobject is the root class 24 | of the runtime class metaobject. 25 | 26 |

This package is provided as a sample implementation of the 27 | reflection mechanism with Javassist. All the programs in this package 28 | uses only the regular Javassist API; they never call any hidden 29 | methods. 30 | 31 |

The most significant class in this package is Reflection. 32 | See the description of this class first. 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/ObjectNotFoundException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.rmi; 18 | 19 | public class ObjectNotFoundException extends Exception { 20 | public ObjectNotFoundException(String name) { 21 | super(name + " is not exported"); 22 | } 23 | 24 | public ObjectNotFoundException(String name, Exception e) { 25 | super(name + " because of " + e.toString()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/Proxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.rmi; 18 | 19 | /** 20 | * An interface implemented by proxy classes. 21 | * 22 | * @see StubGenerator 23 | */ 24 | public interface Proxy { 25 | int _getObjectId(); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/RemoteException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.rmi; 18 | 19 | /** 20 | * RemoteException represents any exception thrown 21 | * during remote method invocation. 22 | */ 23 | public class RemoteException extends RuntimeException { 24 | public RemoteException(String msg) { 25 | super(msg); 26 | } 27 | 28 | public RemoteException(Exception e) { 29 | super("by " + e.toString()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/RemoteRef.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.rmi; 18 | 19 | /** 20 | * Remote reference. This class is internally used for sending a remote 21 | * reference through a network stream. 22 | */ 23 | public class RemoteRef implements java.io.Serializable { 24 | public int oid; 25 | public String classname; 26 | 27 | public RemoteRef(int i) { 28 | oid = i; 29 | classname = null; 30 | } 31 | 32 | public RemoteRef(int i, String name) { 33 | oid = i; 34 | classname = name; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/Sample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.rmi; 18 | 19 | /** 20 | * A template used for defining a proxy class. 21 | * The class file of this class is read by the StubGenerator 22 | * class. 23 | */ 24 | public class Sample { 25 | private ObjectImporter importer; 26 | private int objectId; 27 | 28 | public Object forward(Object[] args, int identifier) { 29 | return importer.call(objectId, identifier, args); 30 | } 31 | 32 | public static Object forwardStatic(Object[] args, int identifier) 33 | throws RemoteException 34 | { 35 | throw new RemoteException("cannot call a static method."); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/rmi/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sample implementation of remote method invocation. 4 | 5 |

This package enables applets to access remote objects 6 | running on the web server with regular Java syntax. 7 | It is provided as a sample implementation with Javassist. 8 | All the programs in this package uses only the regular 9 | Javassist API; they never call any hidden methods. 10 | 11 |

The most significant class of this package is 12 | ObjectImporter. 13 | See the description of this class first. 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/web/BadHttpRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.tools.web; 18 | 19 | /** 20 | * Thrown when receiving an invalid HTTP request. 21 | */ 22 | public class BadHttpRequest extends Exception { 23 | private Exception e; 24 | 25 | public BadHttpRequest() { e = null; } 26 | 27 | public BadHttpRequest(Exception _e) { e = _e; } 28 | 29 | public String toString() { 30 | if (e == null) 31 | return super.toString(); 32 | else 33 | return e.toString(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/tools/web/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Simple web server for running sample code. 4 | 5 |

This package provides a simple web server for sample packages. 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Utility classes. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/MethodFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.util.proxy; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | /** 22 | * Selector of the methods implemented by a handler. 23 | * 24 | * @see ProxyFactory#setFilter(MethodFilter) 25 | */ 26 | public interface MethodFilter { 27 | /** 28 | * Returns true if the given method is implemented by a handler. 29 | */ 30 | boolean isHandled(Method m); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/MethodHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.util.proxy; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | /** 22 | * The interface implemented by the invocation handler of a proxy 23 | * instance. 24 | * 25 | * @see Proxy#setHandler(MethodHandler) 26 | */ 27 | public interface MethodHandler { 28 | /** 29 | * Is called when a method is invoked on a proxy instance associated 30 | * with this handler. This method must process that method invocation. 31 | * 32 | * @param self the proxy instance. 33 | * @param thisMethod the overridden method declared in the super 34 | * class or interface. 35 | * @param proceed the forwarder method for invoking the overridden 36 | * method. It is null if the overridden method is 37 | * abstract or declared in the interface. 38 | * @param args an array of objects containing the values of 39 | * the arguments passed in the method invocation 40 | * on the proxy instance. If a parameter type is 41 | * a primitive type, the type of the array element 42 | * is a wrapper class. 43 | * @return the resulting value of the method invocation. 44 | * 45 | * @throws Throwable if the method invocation fails. 46 | */ 47 | Object invoke(Object self, Method thisMethod, Method proceed, 48 | Object[] args) throws Throwable; 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/Proxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.util.proxy; 18 | 19 | /** 20 | * The interface implemented by proxy classes. 21 | * This interface only provides a setter method. 22 | * To obtain a handler, call {@link ProxyFactory#getHandler(Proxy)}. 23 | * 24 | * @see ProxyFactory 25 | * @since 3.16 26 | */ 27 | public interface Proxy { 28 | /** 29 | * Sets a handler. It can be used for changing handlers 30 | * during runtime. 31 | */ 32 | void setHandler(MethodHandler mi); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/ProxyObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.util.proxy; 18 | 19 | /** 20 | * The interface implemented by proxy classes. 21 | * This interface is available only if the super class of the proxy object 22 | * does not have a getHandler() method. If the super class 23 | * has getHandler, then Proxy interface is 24 | * available. 25 | * 26 | * @see ProxyFactory 27 | * @see Proxy 28 | */ 29 | public interface ProxyObject extends Proxy { 30 | /** 31 | * Sets a handler. It can be used for changing handlers 32 | * during runtime. 33 | */ 34 | void setHandler(MethodHandler mi); 35 | 36 | /** 37 | * Get the handler. 38 | * This can be used to access the underlying MethodHandler 39 | * or to serialize it properly. 40 | * 41 | * @see ProxyFactory#getHandler(Proxy) 42 | */ 43 | MethodHandler getHandler(); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/ProxyObjectOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Javassist, a Java-bytecode translator toolkit. 3 | * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 | * 5 | * The contents of this file are subject to the Mozilla Public License Version 6 | * 1.1 (the "License"); you may not use this file except in compliance with 7 | * the License. Alternatively, the contents of this file may be used under 8 | * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 | * or the Apache License Version 2.0. 10 | * 11 | * Software distributed under the License is distributed on an "AS IS" basis, 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 | * for the specific language governing rights and limitations under the 14 | * License. 15 | */ 16 | 17 | package org.xxxx.javassist.util.proxy; 18 | 19 | import java.io.IOException; 20 | import java.io.ObjectOutputStream; 21 | import java.io.ObjectStreamClass; 22 | import java.io.OutputStream; 23 | 24 | /** 25 | * An input stream class which knows how to serialize proxies created via {@link ProxyFactory}. It must 26 | * be used when serialising proxies created from a proxy factory configured with 27 | * {@link ProxyFactory#useWriteReplace} set to false. Subsequent deserialization of the serialized data 28 | * must employ a {@link ProxyObjectInputStream} 29 | * 30 | * @author Andrew Dinn 31 | */ 32 | public class ProxyObjectOutputStream extends ObjectOutputStream 33 | { 34 | /** 35 | * create an output stream which can be used to serialize an object graph which includes proxies created 36 | * using class ProxyFactory 37 | * @param out 38 | * @throws IOException whenever ObjectOutputStream would also do so 39 | * @throws SecurityException whenever ObjectOutputStream would also do so 40 | * @throws NullPointerException if out is null 41 | */ 42 | public ProxyObjectOutputStream(OutputStream out) throws IOException 43 | { 44 | super(out); 45 | } 46 | 47 | protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { 48 | Class cl = desc.forClass(); 49 | if (ProxyFactory.isProxyClass(cl)) { 50 | writeBoolean(true); 51 | Class superClass = cl.getSuperclass(); 52 | Class[] interfaces = cl.getInterfaces(); 53 | byte[] signature = ProxyFactory.getFilterSignature(cl); 54 | String name = superClass.getName(); 55 | writeObject(name); 56 | // we don't write the marker interface ProxyObject 57 | writeInt(interfaces.length - 1); 58 | for (int i = 0; i < interfaces.length; i++) { 59 | Class interfaze = interfaces[i]; 60 | if (interfaze != ProxyObject.class && interfaze != Proxy.class) { 61 | name = interfaces[i].getName(); 62 | writeObject(name); 63 | } 64 | } 65 | writeInt(signature.length); 66 | write(signature); 67 | } else { 68 | writeBoolean(false); 69 | super.writeClassDescriptor(desc); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/javassist/util/proxy/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dynamic proxy (similar to Enhancer of cglib). 4 | See ProxyFactory for more details. 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/utils/Cache.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class Cache { 9 | public static List lastTransformers; 10 | public static List lastBlackList; 11 | public static Map classByteCache; 12 | 13 | public Cache(){ 14 | 15 | } 16 | 17 | public static void initTransformers(){ 18 | lastTransformers = new ArrayList(); 19 | } 20 | public static void initLastBlackList(){ 21 | lastBlackList = new ArrayList(); 22 | } 23 | 24 | public static void initClassByteCache(){ 25 | classByteCache = new HashMap(); 26 | } 27 | 28 | public static void addLastTransformer(Class clazz){ 29 | lastTransformers.add(clazz); 30 | } 31 | public static void addLastBlackList(Class clazz){ 32 | lastBlackList.add(clazz); 33 | } 34 | 35 | public static void clearLastBlackList(){ 36 | lastBlackList.clear(); 37 | } 38 | public static void clearLastTransformers(){ 39 | lastTransformers.clear(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/utils/CheckStruct.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.utils; 2 | 3 | import java.util.HashMap; 4 | 5 | public class CheckStruct { 6 | static HashMap> memShells = new HashMap>(); 7 | 8 | public static boolean set(String className, HashMap info) throws Exception { 9 | String id = Utils.getMD5(className); 10 | if (memShells.containsKey(id)){ 11 | System.out.println(String.format("%s already exists !!!", id)); 12 | return false; 13 | } 14 | if (memShells.put(id, info) != null){ 15 | return true; 16 | } 17 | else { 18 | return false; 19 | } 20 | } 21 | 22 | public static Object get(String id, String key) { 23 | try{ 24 | id = Utils.getMD5(id); 25 | return memShells.get(id).get(key); 26 | }catch (Exception e){ 27 | e.printStackTrace(); 28 | } 29 | return null; 30 | } 31 | 32 | public static Object getMemShells(){ 33 | return memShells; 34 | } 35 | 36 | public static HashMap newMemShellInfo(Object name,Object pattern,Object classC,Object ClassLoader,Object filePath,Object type, Object killType){ 37 | HashMap memShellInfo = new HashMap(); 38 | memShellInfo.put("name", name); 39 | memShellInfo.put("pattern", pattern); 40 | memShellInfo.put("class", classC); 41 | memShellInfo.put("classloader", ClassLoader); 42 | memShellInfo.put("filePath", filePath); 43 | memShellInfo.put("type", type); 44 | memShellInfo.put("killType", killType); 45 | return memShellInfo; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/xxxx/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package org.xxxx.utils; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.security.MessageDigest; 7 | import java.security.NoSuchAlgorithmException; 8 | 9 | public class Utils { 10 | 11 | public static String getMD5(String input) { 12 | try { 13 | // 创建一个MD5算法实例 14 | MessageDigest md = MessageDigest.getInstance("MD5"); 15 | 16 | // 计算MD5哈希值 17 | byte[] messageDigest = md.digest(input.getBytes()); 18 | 19 | // 将字节数组转换为十六进制字符串 20 | StringBuilder hexString = new StringBuilder(); 21 | for (byte b : messageDigest) { 22 | String hex = Integer.toHexString(0xff & b); 23 | if (hex.length() == 1) hexString.append('0'); 24 | hexString.append(hex); 25 | } 26 | 27 | return hexString.toString(); 28 | } catch (NoSuchAlgorithmException e) { 29 | throw new RuntimeException(e); 30 | } 31 | } 32 | 33 | public static byte[] getClassByte(String className, ClassLoader loader){ 34 | try { 35 | byte[] classfileBuffer; 36 | InputStream in = loader.getResourceAsStream(className + ".class"); 37 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 38 | int nRead; 39 | byte[] data = new byte[1024]; 40 | while (true) { 41 | if (!((nRead = in.read(data, 0, data.length)) != -1)) break; 42 | buffer.write(data, 0, nRead); 43 | } 44 | classfileBuffer = buffer.toByteArray(); 45 | return classfileBuffer; 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } 49 | return null; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Agent-Class: org.xxxx.agent.Agent 3 | Premain-Class: org.xxxx.agent.Agent 4 | Created-By: xxxx 4.2.1 5 | Main-Class: org.xxxx.Run 6 | Can-Retransform-Classes: true 7 | Can-Redefine-Classes: true 8 | --------------------------------------------------------------------------------