├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── description.html ├── encodings.xml ├── misc.xml ├── modules.xml ├── project-template.xml ├── uiDesigner.xml ├── vcs.xml └── workspace.xml ├── README.md ├── Stone.iml ├── data ├── array.stone ├── class.stone ├── classfib.stone ├── closure.stone ├── fun.stone ├── interpreter.stone ├── lex.stone ├── nativefun.stone ├── parser.stone ├── testfun.stone ├── typecheck.stone ├── typefun.stone └── 闭包.sai ├── out └── production │ └── Stone │ └── com │ └── misakimei │ └── stone │ ├── ASTLeaf.class │ ├── ASTList.class │ ├── ASTree.class │ ├── BasicEnv.class │ ├── BasicParser.class │ ├── BinaryExpr.class │ ├── BlockStment.class │ ├── Environment.class │ ├── IdToken.class │ ├── IfStmnt.class │ ├── Lexer.class │ ├── LexerRunner.class │ ├── NULLStmnt.class │ ├── Name.class │ ├── NegativeExpr.class │ ├── NumToken.class │ ├── NumberLiteral.class │ ├── ParseException.class │ ├── PrimaryExpr.class │ ├── StoneExcetion.class │ ├── StrToken.class │ ├── StringLiteral.class │ ├── Token$1.class │ ├── Token.class │ └── WhileStmnt.class └── src ├── com └── misakimei │ └── stone │ ├── ASTLeaf.java │ ├── ASTList.java │ ├── ASTree.java │ ├── Arguments.java │ ├── ArrayEnv.java │ ├── ArrayLiteral.java │ ├── ArrayRef.java │ ├── BasicEnv.java │ ├── BasicParser.java │ ├── BinaryExpr.java │ ├── BlockStment.java │ ├── ClassBody.java │ ├── ClassInfo.java │ ├── ClassParse.java │ ├── ClassStmnt.java │ ├── ClosureParse.java │ ├── DefStmnt.java │ ├── Dot.java │ ├── Environment.java │ ├── Fun.java │ ├── FuncParse.java │ ├── Function.java │ ├── IdToken.java │ ├── IfStmnt.java │ ├── Lexer.java │ ├── LexerRunner.java │ ├── MemberSymbols.java │ ├── NULLStmnt.java │ ├── Name.java │ ├── Native.java │ ├── NativeFunction.java │ ├── NegativeExpr.java │ ├── NestEnv.java │ ├── NumToken.java │ ├── NumberLiteral.java │ ├── OptClassInfo.java │ ├── OptFunction.java │ ├── OptMethod.java │ ├── OptStoneObject.java │ ├── ParamterList.java │ ├── ParseException.java │ ├── Postfix.java │ ├── PrimaryExpr.java │ ├── ResizableArrayEnv.java │ ├── StoneExcetion.java │ ├── StoneObject.java │ ├── StrToken.java │ ├── StringLiteral.java │ ├── SymbolThis.java │ ├── Symbols.java │ ├── Token.java │ ├── WhileStmnt.java │ ├── parser │ └── Parser.java │ ├── run │ ├── ArrayInterpreter.java │ ├── BasicInterpreter.java │ ├── ClassInterpreter.java │ ├── ClosureInterpretre.java │ ├── EnvOptInterpreter.java │ ├── FuncInterpreter.java │ ├── NativeInterpreter.java │ ├── ObjOpInterpreter.java │ ├── ParserRunner.java │ ├── TypedInterpreter.java │ └── VMInterpreter.java │ ├── tool │ └── Log.java │ ├── type │ ├── JavaFunction.java │ ├── JavaLoader.java │ ├── Runtime.java │ ├── ToJava.java │ ├── TypeEnv.java │ ├── TypeException.java │ ├── TypeInfo.java │ ├── TypeTag.java │ ├── TypedNatives.java │ ├── TypedParse.java │ ├── VarStmnt.java │ ├── currenttime.java │ ├── length.java │ ├── print.java │ ├── read.java │ └── toInt.java │ └── vm │ ├── Code.java │ ├── EnvEx.java │ ├── HeapMemory.java │ ├── Opcode.java │ ├── StoneVM.java │ ├── StoneVMEnv.java │ └── VMFunction.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 /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | *.class 4 | 5 | # Mobile Tools for Java (J2ME) 6 | .mtj.tmp/ 7 | 8 | # Package Files # 9 | *.jar 10 | *.war 11 | *.ear 12 | 13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 14 | hs_err_pid* 15 | ### Windows template 16 | # Windows image file caches 17 | Thumbs.db 18 | ehthumbs.db 19 | 20 | # Folder config file 21 | Desktop.ini 22 | 23 | # Recycle Bin used on file shares 24 | $RECYCLE.BIN/ 25 | 26 | # Windows Installer files 27 | *.cab 28 | *.msi 29 | *.msm 30 | *.msp 31 | 32 | # Windows shortcuts 33 | *.lnk 34 | ### JetBrains template 35 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 36 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 37 | 38 | # User-specific stuff: 39 | .idea/workspace.xml 40 | .idea/tasks.xml 41 | .idea/dictionaries 42 | .idea/vcs.xml 43 | .idea/jsLibraryMappings.xml 44 | 45 | # Sensitive or high-churn files: 46 | .idea/dataSources.ids 47 | .idea/dataSources.xml 48 | .idea/dataSources.local.xml 49 | .idea/sqlDataSources.xml 50 | .idea/dynamic.xml 51 | .idea/uiDesigner.xml 52 | 53 | # Gradle: 54 | .idea/gradle.xml 55 | .idea/libraries 56 | 57 | # Mongo Explorer plugin: 58 | .idea/mongoSettings.xml 59 | 60 | ## File-based project format: 61 | *.iws 62 | 63 | ## Plugin-specific files: 64 | 65 | # IntelliJ 66 | /out/ 67 | 68 | # mpeltonen/sbt-idea plugin 69 | .idea_modules/ 70 | 71 | # JIRA plugin 72 | atlassian-ide-plugin.xml 73 | 74 | # Crashlytics plugin (for Android Studio and IntelliJ) 75 | com_crashlytics_export_strings.xml 76 | crashlytics.properties 77 | crashlytics-build.properties 78 | fabric.properties 79 | 80 | 81 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dust 2 | 自制编译器 3 | 4 | Dust是一个根据<<两周自制脚本语言>>来完成的基于JAVA的编译器 5 | -------------------------------------------------------------------------------- /Stone.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /data/array.stone: -------------------------------------------------------------------------------- 1 | class Position{ 2 | x=y=0 3 | def move(nx,ny){ 4 | x=nx 5 | y=ny 6 | } 7 | } 8 | p=Position.new 9 | p.move(3,4) 10 | //p.x=10 11 | print p.x 12 | 13 | a=[10,"我",30] 14 | print a[1] -------------------------------------------------------------------------------- /data/class.stone: -------------------------------------------------------------------------------- 1 | class Position{ 2 | x=y=0 3 | def move(nx,ny){ 4 | x=nx 5 | y=ny 6 | } 7 | } 8 | p=Position.new 9 | p.move(3,4) 10 | //p.x=10 11 | print p.x 12 | -------------------------------------------------------------------------------- /data/classfib.stone: -------------------------------------------------------------------------------- 1 | class Fib{ 2 | fib0=0 3 | fib1=1 4 | def fib(n){ 5 | if n==0{ 6 | fib0 7 | }else{ 8 | if n==1{ 9 | this.fib1 10 | }else{ 11 | fib(n-1)+this.fib(n-2) 12 | } 13 | } 14 | } 15 | } 16 | t=currentTime() 17 | f=Fib.new 18 | print (f.fib(33)) 19 | print currentTime()-t+" 毫秒" -------------------------------------------------------------------------------- /data/closure.stone: -------------------------------------------------------------------------------- 1 | def counter(){ 2 | count = 0 3 | fun (){ 4 | count =count+1 5 | } 6 | } 7 | 8 | c1=counter() 9 | c2=counter() 10 | c1() 11 | c1() 12 | c2() 13 | 14 | //闭包原理见Arguments.java 和./data/闭包.sai -------------------------------------------------------------------------------- /data/fun.stone: -------------------------------------------------------------------------------- 1 | def fib(n){ 2 | if n<2{ 3 | n 4 | }else{ 5 | fib(n-1)+fib(n-2) 6 | } 7 | } 8 | t=currentTime() 9 | fib(33) 10 | print (currentTime()-t+"毫秒") 11 | -------------------------------------------------------------------------------- /data/interpreter.stone: -------------------------------------------------------------------------------- 1 | sum=0 2 | i=-1 3 | while i<10{ 4 | sum=sum+i 5 | i=i+1 6 | } 7 | sum -------------------------------------------------------------------------------- /data/lex.stone: -------------------------------------------------------------------------------- 1 | while i<10{ 2 | sum=sum+i 3 | i=i+1 4 | } 5 | sum 6 | 100+100 7 | "dfdfdfdf\\ \nddfdfd" -------------------------------------------------------------------------------- /data/nativefun.stone: -------------------------------------------------------------------------------- 1 | def fib(n){ 2 | if n<2{ 3 | n 4 | }else{ 5 | fib(n-1)+fib(n-2) 6 | } 7 | } 8 | t=currentTime() 9 | fib 30 10 | print currentTime()-t+" msec" -------------------------------------------------------------------------------- /data/parser.stone: -------------------------------------------------------------------------------- 1 | even = 0 2 | odd = 0 3 | i = 1 4 | while i < 10 { 5 | if i % 2 == 0 { //even number? 6 | even = even + i 7 | } else { 8 | odd = odd +i 9 | } 10 | i = i + 1 11 | } 12 | even + odd 13 | -------------------------------------------------------------------------------- /data/testfun.stone: -------------------------------------------------------------------------------- 1 | n=10 2 | def t(){ 3 | n=n+1 4 | } 5 | 6 | t() 7 | print n -------------------------------------------------------------------------------- /data/typecheck.stone: -------------------------------------------------------------------------------- 1 | def f(n:Int):Int{ 2 | n+1 3 | } 4 | f(10) -------------------------------------------------------------------------------- /data/typefun.stone: -------------------------------------------------------------------------------- 1 | def fib(n:Int):Int{ 2 | if n<2{ 3 | n 4 | }else{ 5 | fib(n-1)+fib(n-2) 6 | } 7 | } 8 | fib(33) -------------------------------------------------------------------------------- /data/闭包.sai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/data/闭包.sai -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/ASTLeaf.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/ASTLeaf.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/ASTList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/ASTList.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/ASTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/ASTree.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/BasicEnv.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/BasicEnv.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/BasicParser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/BasicParser.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/BinaryExpr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/BinaryExpr.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/BlockStment.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/BlockStment.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/Environment.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/Environment.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/IdToken.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/IdToken.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/IfStmnt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/IfStmnt.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/Lexer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/Lexer.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/LexerRunner.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/LexerRunner.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/NULLStmnt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/NULLStmnt.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/Name.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/Name.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/NegativeExpr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/NegativeExpr.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/NumToken.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/NumToken.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/NumberLiteral.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/NumberLiteral.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/ParseException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/ParseException.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/PrimaryExpr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/PrimaryExpr.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/StoneExcetion.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/StoneExcetion.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/StrToken.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/StrToken.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/StringLiteral.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/StringLiteral.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/Token$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/Token$1.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/Token.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/Token.class -------------------------------------------------------------------------------- /out/production/Stone/com/misakimei/stone/WhileStmnt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodgear/Dust/11dad37ea5d6acf421d276cfffc691085dbb6a00/out/production/Stone/com/misakimei/stone/WhileStmnt.class -------------------------------------------------------------------------------- /src/com/misakimei/stone/ASTLeaf.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/27. 8 | */ 9 | public class ASTLeaf extends ASTree { 10 | private static ArrayListempty=new ArrayList<>(); 11 | private Token token; 12 | public ASTLeaf(Token t){ 13 | token=t; 14 | } 15 | 16 | @Override 17 | public ASTree child(int i) { 18 | throw new IndexOutOfBoundsException(); 19 | } 20 | 21 | @Override 22 | public int numChildren() { 23 | return 0; 24 | } 25 | 26 | @Override 27 | public Iterator children() { 28 | return empty.iterator(); 29 | } 30 | 31 | @Override 32 | public String location() { 33 | return "第"+token.getLineNumber()+"行"; 34 | } 35 | 36 | @Override 37 | public Object eval(Environment env) { 38 | throw new StoneExcetion("无法执行 eval "+toString(),this); 39 | } 40 | 41 | public Token token(){return token;} 42 | 43 | @Override 44 | public String toString() { 45 | return token.getText(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ASTList.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.vm.Code; 4 | 5 | import java.util.Iterator; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by 18754 on 2016/7/27. 10 | */ 11 | public class ASTList extends ASTree { 12 | protected Listchildren; 13 | 14 | public ASTList(Listlis){children=lis;} 15 | 16 | 17 | @Override 18 | public ASTree child(int i) { 19 | return children.get(i); 20 | } 21 | 22 | @Override 23 | public int numChildren() { 24 | return children.size(); 25 | } 26 | 27 | @Override 28 | public Iterator children() { 29 | return children.iterator(); 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | StringBuilder sb=new StringBuilder(); 35 | sb.append('('); 36 | String seq=""; 37 | for (ASTree t:children){ 38 | sb.append(seq); 39 | seq=" "; 40 | sb.append(t.toString()); 41 | } 42 | return sb.append(')').toString(); 43 | } 44 | 45 | @Override 46 | public String location() { 47 | for (ASTree t:children){ 48 | String s=t.location(); 49 | if (s!=null){ 50 | return s; 51 | } 52 | } 53 | return null; 54 | } 55 | @Override 56 | public Object eval(Environment env) { 57 | throw new StoneExcetion("无法执行 eval "+toString(),this); 58 | } 59 | 60 | @Override 61 | public void lookup(Symbols symbol) { 62 | for (ASTree t:this){ 63 | t.lookup(symbol); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ASTree.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.vm.Code; 7 | 8 | import java.util.Iterator; 9 | 10 | /** 11 | * Created by 18754 on 2016/7/27. 12 | */ 13 | public abstract class ASTree implements Iterable{ 14 | public abstract ASTree child(int i); 15 | public abstract int numChildren(); 16 | public abstract Iterator children(); 17 | public abstract String location(); 18 | public abstract Object eval(Environment env); 19 | public Iterator iterator(){ 20 | return children(); 21 | } 22 | public void lookup(Symbols symbol){} 23 | 24 | public TypeInfo typecheck(TypeEnv tenv)throws TypeException { 25 | return null; 26 | } 27 | public String translate(TypeInfo res){return "";} 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Arguments.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.tool.Log; 4 | import com.misakimei.stone.type.*; 5 | import com.misakimei.stone.vm.Code; 6 | import com.misakimei.stone.vm.EnvEx; 7 | import com.misakimei.stone.vm.StoneVM; 8 | import com.misakimei.stone.vm.VMFunction; 9 | import com.sun.org.apache.xerces.internal.impl.dv.dtd.ENTITYDatatypeValidator; 10 | 11 | import java.util.List; 12 | 13 | import static com.misakimei.stone.type.ToJava.ENV; 14 | import static com.misakimei.stone.type.ToJava.transloateExpr; 15 | import static com.misakimei.stone.vm.Opcode.*; 16 | 17 | /** 18 | * Created by 18754 on 2016/7/29. 19 | */ 20 | public class Arguments extends Postfix{ 21 | 22 | protected TypeInfo[]argTypes; 23 | protected TypeInfo.FuncitonType funcitonType; 24 | 25 | public Arguments(List lis) { 26 | super(lis); 27 | } 28 | public int size(){return numChildren();} 29 | 30 | public Object eval(Environment env,Object val) { 31 | if (!(val instanceof JavaFunction)){ 32 | throw new StoneExcetion("无法解析成函数类型"); 33 | } 34 | JavaFunction function= (JavaFunction) val; 35 | Object[]args=new Object[numChildren()+1]; 36 | args[0]=env; 37 | int num=1; 38 | for (ASTree a:this){ 39 | args[num++]=a.eval(env); 40 | } 41 | return function.invoke(args); 42 | } 43 | 44 | @Override 45 | public TypeInfo typecheck(TypeEnv tenv, TypeInfo target) throws TypeException { 46 | if (!(target instanceof TypeInfo.FuncitonType)){ 47 | throw new TypeException("函数 错误",this); 48 | } 49 | funcitonType= (TypeInfo.FuncitonType) target; 50 | TypeInfo[]params=funcitonType.parameterTypes; 51 | if (size()!=params.length){ 52 | throw new TypeException("函数 参数数量错误",this); 53 | } 54 | argTypes=new TypeInfo[params.length]; 55 | int num=0; 56 | for (ASTree a:this){ 57 | TypeInfo t=argTypes[num]=a.typecheck(tenv); 58 | t.assertSubtypeOf(params[num++],tenv,this); 59 | } 60 | return funcitonType.returntype; 61 | } 62 | 63 | @Override 64 | public String translate(String expr) { 65 | StringBuilder code=new StringBuilder(expr); 66 | code.append("(").append(ENV); 67 | for (int i=0;i lis) { 10 | super(lis); 11 | } 12 | 13 | public int size(){ 14 | return numChildren(); 15 | } 16 | 17 | @Override 18 | public Object eval(Environment env) { 19 | int size=numChildren(); 20 | Object[]res=new Object[size]; 21 | int i=0; 22 | for (ASTree ast:this){ 23 | res[i++]=ast.eval(env); 24 | } 25 | return res; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ArrayRef.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | 7 | import java.util.List; 8 | import java.util.Objects; 9 | 10 | /** 11 | * Created by 18754 on 2016/7/31. 12 | */ 13 | public class ArrayRef extends Postfix { 14 | public ArrayRef(Listc){ 15 | super(c); 16 | } 17 | public ASTree index(){ 18 | return child(0); 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return "["+index()+"]"; 24 | } 25 | 26 | @Override 27 | public Object eval(Environment env, Object value) { 28 | if (value instanceof Object[]){ 29 | Object index=index().eval(env); 30 | if (index instanceof Integer){ 31 | return ((Object[])value)[(Integer) index]; 32 | } 33 | } 34 | throw new StoneExcetion("无法访问此数组"); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/BasicEnv.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.JavaLoader; 4 | 5 | import java.util.HashMap; 6 | 7 | /** 8 | * Created by 18754 on 2016/7/28. 9 | * 最外部的作用域 10 | */ 11 | public class BasicEnv implements Environment { 12 | 13 | protected HashMapvalues; 14 | public BasicEnv(){ 15 | values=new HashMap<>(); 16 | } 17 | @Override 18 | public void put(String name, Object value) { 19 | values.put(name,value); 20 | } 21 | 22 | @Override 23 | public Object get(String name) { 24 | return values.get(name); 25 | } 26 | 27 | @Override 28 | public void putNew(String name, Object value) { 29 | put(name,value); 30 | } 31 | 32 | @Override 33 | public Environment where(String name) { 34 | if (get(name)!=null){ 35 | return this; 36 | } 37 | // throw new StoneExcetion("在最外层作用域也找不到 "+name+" 的定义"); 38 | return null; 39 | } 40 | 41 | @Override 42 | public void setOuter(Environment env) { 43 | throw new StoneExcetion("无法为最外层作用域设置外层作用域"); 44 | } 45 | 46 | @Override 47 | public Symbols symbol() { 48 | return null; 49 | } 50 | 51 | @Override 52 | public void put(int nest, int index, Object val) { 53 | 54 | } 55 | 56 | @Override 57 | public Object get(int nest, int index) { 58 | return null; 59 | } 60 | 61 | @Override 62 | public JavaLoader javaLoader() { 63 | return null; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ClassBody.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/31. 8 | */ 9 | public class ClassBody extends ASTList { 10 | public ClassBody(List lis) { 11 | super(lis); 12 | } 13 | 14 | @Override 15 | public Object eval(Environment env) { 16 | for (ASTree t:this){ 17 | if (!(t instanceof DefStmnt)){ 18 | t.eval(env); 19 | } 20 | } 21 | return null; 22 | } 23 | 24 | 25 | public void lookup(Symbols newsyms, Symbols methodNames, Symbols fieldName, ArrayList methods) { 26 | for (ASTree t:this){ 27 | if (t instanceof DefStmnt){ 28 | DefStmnt def= (DefStmnt) t; 29 | int oldsize=methodNames.size(); 30 | int i=methodNames.putNew(def.name()); 31 | if (i>=oldsize){ 32 | methods.add(def); 33 | }else { 34 | methods.set(i,def); 35 | } 36 | def.lookupAsMethod(fieldName); 37 | }else { 38 | t.lookup(newsyms); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ClassInfo.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/31. 5 | */ 6 | public class ClassInfo { 7 | 8 | private ClassStmnt define; 9 | private Environment environment; 10 | private ClassInfo superclass; 11 | 12 | public ClassInfo(ClassStmnt classStmnt, Environment env) { 13 | this.define=classStmnt; 14 | this.environment=env; 15 | 16 | Object ss=env.get(define.superClass()); 17 | if (ss instanceof ClassInfo){ 18 | superclass= (ClassInfo) ss; 19 | }else if (ss==null){ 20 | superclass=null; 21 | }else { 22 | throw new StoneExcetion("无法确认父类 "+define.superClass()); 23 | } 24 | } 25 | 26 | public String name(){ 27 | return define.name(); 28 | } 29 | 30 | public Environment enviroment() { 31 | return environment; 32 | } 33 | 34 | public ClassInfo supperClass() { 35 | return superclass; 36 | } 37 | 38 | public ClassBody body() { 39 | return define.body(); 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return "< class "+name()+" >"; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ClassParse.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | import com.misakimei.stone.parser.Parser; 3 | 4 | import static com.misakimei.stone.parser.Parser.rule; 5 | /** 6 | * Created by 18754 on 2016/7/31. 7 | */ 8 | public class ClassParse extends ClosureParse { 9 | 10 | /** 11 | * member : def|simple 12 | * class_body : "{" [member] {(";"|EOL) [member]} "}" 13 | * def_class : "class" IDENTIFIER ["extends" IDENTIFIER] class_body 14 | * postfix : "." IDENTIFIER | "(" [args] ")" 15 | * program : [defclass | def |statement] (";"|EOL) 16 | * 17 | */ 18 | 19 | Parser member=rule().or(def,simple); 20 | Parser classbody=rule(ClassBody.class) 21 | .sep("{") 22 | .option(member) 23 | .repeat(rule().sep(";",Token.EOL).option(member)) 24 | .sep("}"); 25 | Parser defclass=rule(ClassStmnt.class) 26 | .sep("class") 27 | .identifier(reserved) 28 | .option(rule().sep("extends").identifier(reserved)) 29 | .ast(classbody); 30 | 31 | public ClassParse(){ 32 | postfix.insertChoice(rule(Dot.class).sep(".").identifier(reserved)); 33 | program.insertChoice(defclass); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ClassStmnt.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/31. 8 | */ 9 | public class ClassStmnt extends ASTList { 10 | public ClassStmnt(List lis) { 11 | super(lis); 12 | } 13 | 14 | public String name(){ 15 | return ((ASTLeaf)child(0)).token().getText(); 16 | } 17 | 18 | public String superClass(){ 19 | if (numChildren()<3){ 20 | return null; 21 | } 22 | else{ 23 | return ((ASTLeaf)child(1)).token().getText(); 24 | } 25 | } 26 | public ClassBody body(){ 27 | return (ClassBody) child(numChildren()-1); 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | String parent=superClass(); 33 | if (parent==null){ 34 | parent="*"; 35 | } 36 | return "class "+name()+ " "+parent+" "+body()+" )"; 37 | } 38 | 39 | @Override 40 | public Object eval(Environment env) { 41 | //一个类可以说是有一些变量和一些方法组成的 42 | Symbols methodNames=new MemberSymbols(env.symbol(),MemberSymbols.METHOD); 43 | Symbols fieldName=new MemberSymbols(methodNames,MemberSymbols.FIELD); 44 | OptClassInfo ci=new OptClassInfo(this,env,methodNames,fieldName); 45 | env.put(name(),ci); 46 | 47 | ArrayListmethods=new ArrayList<>(); 48 | if (ci.supperClass() !=null){ 49 | ci.supperClass().copyTo(fieldName,methodNames,methods); 50 | } 51 | Symbols newsyms=new SymbolThis(fieldName); 52 | body().lookup(newsyms,methodNames,fieldName,methods);//blockstmnt 将真正填充 53 | ci.setMethods(methods); 54 | return name(); 55 | } 56 | 57 | @Override 58 | public void lookup(Symbols symbol) {} 59 | } 60 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ClosureParse.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | import static com.misakimei.stone.parser.Parser.rule; 3 | /** 4 | * Created by 18754 on 2016/7/29. 5 | * 支持闭包的语法分析器 6 | */ 7 | public class ClosureParse extends FuncParse { 8 | public ClosureParse(){ 9 | primary.insertChoice(rule(Fun.class).sep("fun").ast(paramList).ast(block)); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Dot.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by 18754 on 2016/7/31. 7 | */ 8 | public class Dot extends Postfix { 9 | 10 | protected OptClassInfo classInfo=null; 11 | protected boolean isField; 12 | protected int index; 13 | 14 | 15 | public Dot(List lis) { 16 | super(lis); 17 | } 18 | 19 | public String name() { 20 | return ((ASTLeaf) child(0)).token().getText(); 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "." + name(); 26 | } 27 | 28 | 29 | @Override 30 | public Object eval(Environment env, Object value) { 31 | String member = name(); 32 | if (value instanceof OptClassInfo) { 33 | if ("new".equals(member))//使用new 来构造 34 | { 35 | OptClassInfo ci = (OptClassInfo) value; 36 | ArrayEnv nestEnv = new ArrayEnv(1,ci.enviroment()); 37 | OptStoneObject so = new OptStoneObject(ci,ci.size()); 38 | nestEnv.put(0,0, so);//类的环境中 this 指向代表这个类的StoneObject 39 | initObject(ci,so, nestEnv); 40 | return so; 41 | } 42 | } else if (value instanceof OptStoneObject)//普通的变量读取 或者类的方法调用 43 | { 44 | OptStoneObject target= (OptStoneObject) value; 45 | if (target.classInfo()!=classInfo){ 46 | updateCache(target); 47 | } 48 | if (isField){ 49 | return target.read(index); 50 | }else { 51 | return target.method(index); 52 | } 53 | } 54 | return null; 55 | } 56 | 57 | private void updateCache(OptStoneObject target) { 58 | String member=name(); 59 | classInfo=target.classInfo(); 60 | Integer i=classInfo.fieldIndex(member); 61 | if (i!=null){ 62 | isField=true; 63 | index=i; 64 | return; 65 | } 66 | 67 | i=classInfo.methodIndex(member); 68 | if (i!=null){ 69 | isField=false; 70 | index=i; 71 | return; 72 | } 73 | 74 | throw new StoneExcetion("无法访问 "+member,this); 75 | 76 | } 77 | 78 | 79 | //类的初始化 先初始化父类 80 | private void initObject(OptClassInfo ci,OptStoneObject obj, Environment env) { 81 | if (ci.supperClass() != null) { 82 | initObject(ci.supperClass(),obj, env); 83 | } 84 | ci.body().eval(env); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Environment.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.JavaLoader; 4 | 5 | import java.util.Objects; 6 | 7 | /** 8 | * Created by 18754 on 2016/7/28. 9 | */ 10 | public interface Environment { 11 | void put(String name, Object value); 12 | 13 | Object get(String name); 14 | 15 | void putNew(String name, Object value);//因为在嵌套作用域下有可能重名 所以有一个putNew 16 | 17 | Environment where(String name);//作用域链来检测name在哪里 18 | 19 | void setOuter(Environment env);//设置外部作用域 20 | 21 | Symbols symbol(); 22 | 23 | void put(int nest, int index, Object val); 24 | 25 | Object get(int nest, int index); 26 | 27 | JavaLoader javaLoader(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Fun.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by 18754 on 2016/7/29. 7 | */ 8 | public class Fun extends ASTList { 9 | 10 | protected int size=-1; 11 | 12 | public Fun(List lis) { 13 | super(lis); 14 | } 15 | public ParamterList paramters(){ 16 | return (ParamterList) child(0); 17 | } 18 | public BlockStment body(){ 19 | return (BlockStment) child(1); 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "(fun "+paramters()+body()+")"; 25 | } 26 | 27 | @Override 28 | public void lookup(Symbols symbol) { 29 | size=lookup(symbol,paramters(),body()); 30 | } 31 | 32 | 33 | //创建函数内部环境 34 | public static int lookup(Symbols symbol, ParamterList paramters, BlockStment body) { 35 | Symbols sym=new Symbols(symbol);//函数符号表 36 | paramters.lookup(sym); 37 | body.lookup(sym); 38 | return sym.size(); 39 | } 40 | 41 | 42 | @Override 43 | public Object eval(Environment env) { 44 | return new OptFunction(paramters(),body(),env, size);//这时就能看出使用修改器的好处了 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/FuncParse.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | import com.misakimei.stone.parser.Parser; 3 | 4 | import static com.misakimei.stone.parser.Parser.rule; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/29. 8 | */ 9 | public class FuncParse extends BasicParser { 10 | /** 11 | * param : IDENTIFER 12 | * params : param {",",param} 13 | * param_list : "(" [param] ")" 14 | * def : "def" IDENTIFER param_list block 15 | * args : expr {"," expr} 16 | * postfix : "(" [args] ")" 17 | * primary : ( "(" expr ")" |NUMBER|IDENTIFER|STRING) {postfix} 18 | * simple : expr [args] 19 | * program : [def|statement](";"|EOL) 20 | */ 21 | protected Parser param=rule().identifier(reserved); 22 | Parser params=rule(ParamterList.class) 23 | .ast(param).repeat(rule().sep(",").ast(param)); 24 | protected Parser paramList =rule().sep("(").maybe(params).sep(")"); 25 | protected Parser def=rule(DefStmnt.class) 26 | .sep("def").identifier(reserved).ast(paramList).ast(block); 27 | Parser args=rule(Arguments.class) 28 | .ast(expr).repeat(rule().sep(",").ast(expr)); 29 | Parser postfix=rule().sep("(").maybe(args).sep(")"); 30 | 31 | 32 | Parser elements=rule(ArrayLiteral.class).ast(expr).repeat(rule().sep(",").ast(expr)); 33 | public FuncParse(){ 34 | reserved.add(")"); 35 | primary.repeat(postfix); 36 | simple.option(args); 37 | program.insertChoice(def); 38 | 39 | reserved.add("]"); 40 | primary.insertChoice(rule().sep("[").maybe(elements).sep("]")); 41 | postfix.insertChoice(rule(ArrayRef.class).sep("[").ast(expr).sep("]")); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Function.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/29. 5 | */ 6 | public class Function { 7 | 8 | protected ParamterList paramters; 9 | protected BlockStment body; 10 | protected Environment env; 11 | 12 | public Function(ParamterList paramters, BlockStment body, Environment env) { 13 | this.paramters = paramters; 14 | this.body = body; 15 | this.env = env; 16 | } 17 | 18 | public ParamterList getParamters() { 19 | return paramters; 20 | } 21 | 22 | public BlockStment getBody() { 23 | return body; 24 | } 25 | 26 | public Environment makeEnv(){ 27 | return new NestEnv(env); 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "< function: "+hashCode()+" >"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/IdToken.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/27. 5 | */ 6 | public class IdToken extends Token { 7 | private String text; 8 | public IdToken(int lineNo, String eol) { 9 | super(lineNo); 10 | text=eol; 11 | } 12 | 13 | @Override 14 | public boolean isIdentifier() { 15 | return true; 16 | } 17 | 18 | @Override 19 | public String getText() { 20 | return text; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/LexerRunner.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.tool.Log; 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileReader; 8 | 9 | /** 10 | * Created by 18754 on 2016/7/27. 11 | */ 12 | public class LexerRunner { 13 | public static void main(String[] args) { 14 | File code=new File("data/lex.stone"); 15 | Lexer lexer; 16 | try { 17 | lexer=new Lexer(new FileReader(code)); 18 | for (Token t;(t=lexer.read())!=Token.EOF;){ 19 | Log.d("==>"+t.getText()); 20 | } 21 | 22 | } catch (FileNotFoundException e) { 23 | Log.d("找不到文件 "+code.getAbsolutePath()); 24 | Log.d("not find"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/MemberSymbols.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/4. 5 | */ 6 | public class MemberSymbols extends Symbols { 7 | public static int METHOD=-1; 8 | public static int FIELD=-2; 9 | protected int type; 10 | public MemberSymbols(Symbols outer,int type){ 11 | super(outer); 12 | this.type=type; 13 | } 14 | 15 | @Override 16 | protected Location get(String key, int nest) { 17 | Integer index=table.get(key); 18 | if (index==null){ 19 | if (outer==null){ 20 | return null; 21 | }else { 22 | return outer.get(key,nest); 23 | } 24 | }else { 25 | return new Location(type,index); 26 | } 27 | } 28 | 29 | @Override 30 | public Location put(String key) { 31 | Location loc=get(key,0); 32 | if (loc==null){ 33 | return new Location(type,add(key)); 34 | }else { 35 | return loc; 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NULLStmnt.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by 18754 on 2016/7/27. 7 | */ 8 | public class NULLStmnt extends ASTList { 9 | public NULLStmnt(List lis) { 10 | super(lis); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Native.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | /** 6 | * Created by 18754 on 2016/7/30. 7 | */ 8 | public class Native { 9 | public Environment environment(Environment env) { 10 | appendNative(env); 11 | return env; 12 | } 13 | 14 | protected void appendNative(Environment env) { 15 | append(env, "print", Native.class, "print", Object.class); 16 | append(env, "length", Native.class, "length", String.class); 17 | append(env, "toInt", Native.class, "toInt", Object.class); 18 | append(env, "currentTime", Native.class, "currentTime"); 19 | } 20 | 21 | //具体的增加原生函数的方法 22 | //将clazz类中的名字叫name参数是params的函数加到env中 名字是methodname 23 | protected void append(Environment env, String name, Class clazz, String methodname, Class... params) { 24 | Method m = null; 25 | try { 26 | m = clazz.getMethod(methodname, params); 27 | 28 | } catch (NoSuchMethodException e) { 29 | e.printStackTrace(); 30 | } 31 | env.put(name, new NativeFunction(methodname, m));//预先将NativeFunction加入环境中 32 | } 33 | 34 | public static int print(Object obj) { 35 | System.out.println("==>"+obj.toString()); 36 | return 0; 37 | } 38 | 39 | public static int length(String s) { 40 | return s.length(); 41 | } 42 | 43 | public static int toInt(Object val) { 44 | if (val instanceof String) { 45 | return Integer.parseInt((String) val); 46 | } else if (val instanceof Integer) { 47 | return ((Integer) val).intValue(); 48 | } else { 49 | throw new NumberFormatException(val.toString()); 50 | } 51 | } 52 | 53 | 54 | public static int currentTime() { 55 | return (int) (System.currentTimeMillis()); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NativeFunction.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.sun.org.apache.regexp.internal.RE; 4 | 5 | import java.lang.reflect.InvocationTargetException; 6 | import java.lang.reflect.Method; 7 | 8 | /** 9 | * Created by 18754 on 2016/7/30. 10 | */ 11 | public class NativeFunction{ 12 | 13 | protected String name; 14 | protected Method method; 15 | protected int numparam; 16 | public NativeFunction(String name, Method m){ 17 | this.name=name; 18 | this.method=m; 19 | numparam=m.getParameterTypes().length;//方法所需的参数的长度 20 | } 21 | public int numParams() { 22 | return numparam; 23 | } 24 | 25 | //实际上arguments并没有什么卵用 26 | public Object invoke(Object[] arg, Arguments arguments) { 27 | try { 28 | return method.invoke(null,arg); 29 | } catch (Exception e){ 30 | throw new StoneExcetion("调用原生JAVA函数"+name+"失败",arguments); 31 | } 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return ""; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NegativeExpr.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.vm.Code; 7 | 8 | import java.util.List; 9 | 10 | import static com.misakimei.stone.vm.Opcode.NEG; 11 | import static com.misakimei.stone.vm.Opcode.encodeRegister; 12 | 13 | /** 14 | * Created by 18754 on 2016/7/27. 15 | */ 16 | public class NegativeExpr extends ASTList { 17 | 18 | public NegativeExpr(List lis) { 19 | super(lis); 20 | } 21 | public ASTree operand(){ 22 | return child(0); 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "-"+operand(); 28 | } 29 | 30 | @Override 31 | public Object eval(Environment env) { 32 | Object v=operand().eval(env); 33 | if (v instanceof Integer){ 34 | return new Integer(-((Integer) v).intValue()); 35 | } 36 | else 37 | throw new StoneExcetion("无法对此类型使用-号",this); 38 | } 39 | 40 | @Override 41 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 42 | TypeInfo t=operand().typecheck(tenv); 43 | t.assertSubtypeOf(TypeInfo.INT,tenv,this); 44 | return TypeInfo.INT; 45 | } 46 | 47 | @Override 48 | public String translate(TypeInfo res) { 49 | return "-"+operand().translate(null); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NestEnv.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.JavaLoader; 4 | import com.sun.istack.internal.Nullable; 5 | import jdk.nashorn.internal.objects.annotations.Where; 6 | 7 | import java.util.HashMap; 8 | 9 | /** 10 | * Created by 18754 on 2016/7/29. 11 | */ 12 | public class NestEnv implements Environment { 13 | protected HashMap values; 14 | private Environment outer; 15 | 16 | public NestEnv(Environment out) { 17 | values=new HashMap<>(); 18 | this.outer = out; 19 | } 20 | 21 | //可以更换外部作用域 22 | public void setOuter(Environment out){ 23 | this.outer=out; 24 | } 25 | 26 | @Override 27 | public Symbols symbol() { 28 | return null; 29 | } 30 | 31 | @Override 32 | public void put(int nest, int index, Object val) { 33 | 34 | } 35 | 36 | @Override 37 | public Object get(int nest, int index) { 38 | return null; 39 | } 40 | 41 | @Override 42 | public JavaLoader javaLoader() { 43 | return null; 44 | } 45 | 46 | public NestEnv() { 47 | this(null); 48 | } 49 | 50 | 51 | @Override 52 | public void put(String name, Object value) { 53 | Environment e= where(name); 54 | if (e==null){ 55 | e=this; 56 | } 57 | e.putNew(name,value); 58 | } 59 | 60 | //检测变量在什么环境中 61 | @Override 62 | public Environment where(String name) { 63 | if (values.get(name)!=null){ 64 | return this; 65 | }else if (outer!=null){ 66 | return outer.where(name); 67 | } 68 | return null; 69 | } 70 | 71 | public void putNew(String name,Object value){ 72 | values.put(name,value); 73 | } 74 | 75 | @Nullable 76 | @Override 77 | public Object get(String name) { 78 | Object v= values.get(name); 79 | //内部环境找不到去外部找 外部找不到 就return null 80 | if (v==null&&outer!=null){ 81 | return outer.get(name); 82 | }else { 83 | return v; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NumToken.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/27. 5 | */ 6 | public class NumToken extends Token { 7 | private int value; 8 | public NumToken(int lineNo, int val) { 9 | super(lineNo); 10 | value=val; 11 | } 12 | 13 | @Override 14 | public boolean isNumber() { 15 | return true; 16 | } 17 | 18 | @Override 19 | public String getText() { 20 | return value+""; 21 | } 22 | public int getValue(){return value;} 23 | } 24 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/NumberLiteral.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.vm.Code; 7 | 8 | import static com.misakimei.stone.vm.Opcode.BCONST; 9 | import static com.misakimei.stone.vm.Opcode.ICONST; 10 | import static com.misakimei.stone.vm.Opcode.encodeRegister; 11 | 12 | /** 13 | * Created by 18754 on 2016/7/27. 14 | */ 15 | public class NumberLiteral extends ASTLeaf { 16 | public NumberLiteral(Token t) { 17 | super(t); 18 | } 19 | 20 | public int value() { 21 | return ((NumToken) token()).getValue(); 22 | } 23 | 24 | @Override 25 | public Object eval(Environment env) { 26 | return value(); 27 | } 28 | 29 | @Override 30 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 31 | return TypeInfo.INT; 32 | } 33 | 34 | @Override 35 | public String translate(TypeInfo res) { 36 | return Integer.toString(value()); 37 | } 38 | } -------------------------------------------------------------------------------- /src/com/misakimei/stone/OptClassInfo.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Created by 18754 on 2016/8/2. 7 | */ 8 | public class OptClassInfo extends ClassInfo { 9 | protected Symbols methods,field; 10 | protected DefStmnt[]methodDefs; 11 | public OptClassInfo(ClassStmnt classStmnt, Environment env,Symbols method,Symbols field) { 12 | super(classStmnt, env); 13 | this.methods=method; 14 | this.field=field; 15 | this.methodDefs=null; 16 | } 17 | public int size(){ 18 | return field.size(); 19 | } 20 | 21 | @Override 22 | public OptClassInfo supperClass() { 23 | return (OptClassInfo) super.supperClass(); 24 | } 25 | //将父类的 field method 拷贝到子类中去 26 | public void copyTo(Symbols f, Symbols m, ArrayListmlist){ 27 | f.append(field); 28 | m.append(methods); 29 | for (DefStmnt def:methodDefs){ 30 | mlist.add(def); 31 | } 32 | } 33 | 34 | public Integer fieldIndex(String name){ 35 | return field.find(name); 36 | } 37 | 38 | 39 | public Integer methodIndex(String name){ 40 | return methods.find(name); 41 | } 42 | public Object method(OptStoneObject self,int index){ 43 | DefStmnt def=methodDefs[index]; 44 | return new OptMethod(def.paramters(),def.body(),enviroment(),def.locals(),self); 45 | } 46 | 47 | public void setMethods(ArrayList methods){ 48 | methodDefs=methods.toArray(new DefStmnt[methods.size()]); 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/OptFunction.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/2. 5 | */ 6 | public class OptFunction extends Function { 7 | protected int size; 8 | 9 | public OptFunction(ParamterList paramters, BlockStment body, Environment env,int size) { 10 | super(paramters, body, env); 11 | this.size=size; 12 | } 13 | 14 | @Override 15 | public Environment makeEnv() { 16 | return new ArrayEnv(size,env); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/OptMethod.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/2. 5 | */ 6 | public class OptMethod extends OptFunction{ 7 | OptStoneObject self; 8 | public OptMethod(ParamterList paramters, BlockStment body, Environment enviroment, int size, OptStoneObject self) { 9 | super(paramters,body,enviroment,size); 10 | this.self=self; 11 | } 12 | 13 | @Override 14 | public Environment makeEnv() { 15 | ArrayEnv e=new ArrayEnv(size,env); 16 | e.put(0,0,self); 17 | return e; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/OptStoneObject.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/2. 5 | */ 6 | public class OptStoneObject { 7 | 8 | public static class AccessException extends Exception{} 9 | 10 | protected OptClassInfo classInfo; 11 | protected Object[]fields; 12 | 13 | public OptStoneObject(OptClassInfo ci,int size){ 14 | classInfo=ci; 15 | fields=new Object[size]; 16 | } 17 | 18 | public OptClassInfo classInfo(){ 19 | return classInfo; 20 | } 21 | public Object read(String name) throws AccessException { 22 | Integer i=classInfo.fieldIndex(name); 23 | if (i!=null){ 24 | return fields[i]; 25 | }else { 26 | i=classInfo.methodIndex(name); 27 | if (i!=null){ 28 | return method(i); 29 | } 30 | } 31 | throw new AccessException(); 32 | } 33 | 34 | public void write(String name,Object val) throws AccessException { 35 | Integer i=classInfo.fieldIndex(name); 36 | if (i==null){ 37 | throw new AccessException(); 38 | }else { 39 | fields[i]=val; 40 | } 41 | } 42 | 43 | public Object read(int index){ 44 | return fields[index]; 45 | } 46 | public void write(int index,Object val){ 47 | fields[index]=val; 48 | } 49 | public Object method(int index){ 50 | return classInfo.method(this,index); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ParamterList.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.type.TypeTag; 7 | import com.misakimei.stone.vm.EnvEx; 8 | import com.misakimei.stone.vm.StoneVM; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * Created by 18754 on 2016/7/29. 14 | */ 15 | public class ParamterList extends ASTList { 16 | 17 | protected int[] offsets;//name的index 18 | 19 | public ParamterList(List lis) { 20 | super(lis); 21 | } 22 | 23 | public String name(int i) { 24 | return ((ASTLeaf) child(i).child(0)).token().getText(); 25 | } 26 | 27 | public int size() { 28 | return numChildren(); 29 | } 30 | 31 | public TypeTag typeTag(int i) { 32 | return (TypeTag) child(i).child(1); 33 | } 34 | 35 | @Override 36 | public void lookup(Symbols symbol) { 37 | int s = size(); 38 | offsets = new int[s]; 39 | for (int i = 0; i < s; i++) { 40 | offsets[i] = symbol.putNew(name(i)); 41 | } 42 | } 43 | 44 | public void eval(Environment env, int index, Object val) { 45 | env.put(0,offsets[index],val); 46 | } 47 | 48 | @Override 49 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 50 | return super.typecheck(tenv); 51 | } 52 | 53 | public TypeInfo[] types() { 54 | int s=size(); 55 | TypeInfo[]result=new TypeInfo[s]; 56 | for (int i=0;i lis) { 14 | super(lis); 15 | } 16 | 17 | public abstract Object eval(Environment env,Object value); 18 | public TypeInfo typecheck(TypeEnv tenv,TypeInfo tatget)throws TypeException{return null;}; 19 | public String translate(String expr){return null;} 20 | } 21 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/PrimaryExpr.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.tool.Log; 4 | import com.misakimei.stone.type.TypeEnv; 5 | import com.misakimei.stone.type.TypeException; 6 | import com.misakimei.stone.type.TypeInfo; 7 | import com.misakimei.stone.vm.Code; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * Created by 18754 on 2016/7/27. 13 | */ 14 | public class PrimaryExpr extends ASTList { 15 | public PrimaryExpr(List lis) { 16 | super(lis); 17 | } 18 | 19 | //擦这是何等的卧槽 这个函数的作用就是避免只有一个还被包裹的情况 20 | public static ASTree create(List lis) { 21 | //如何只有一个 那么就直接返回这个 而不是包裹一层 如果不是则包裹 22 | return lis.size() == 1 ? lis.get(0) : new PrimaryExpr(lis); 23 | } 24 | 25 | //返回名字的部分 sum(1) 返回sum 之前def已经把 sum声明成一个对象了 26 | public ASTree operand() { 27 | return child(0); 28 | } 29 | 30 | public Postfix postfix(int nest) { 31 | return (Postfix) child(numChildren() - nest - 1); 32 | } 33 | 34 | //后缀 例如 sum(1) 的 (1) 35 | public boolean hasPostfix(int nest) { 36 | return numChildren() - nest > 1; 37 | } 38 | 39 | @Override 40 | public Object eval(Environment env) { 41 | return evalSubExpr(env, 0); 42 | } 43 | 44 | public Object evalSubExpr(Environment env, int nest) { 45 | 46 | if (hasPostfix(nest)) { 47 | Object target = evalSubExpr(env, nest + 1);//递归返回的是一个函数对象 48 | return postfix(nest).eval(env, target);//Arguments的eval 最后是这里 49 | } else { 50 | //最里层的是一个Name 51 | return operand().eval(env); 52 | } 53 | } 54 | 55 | @Override 56 | public void lookup(Symbols symbol) { 57 | super.lookup(symbol); 58 | } 59 | 60 | @Override 61 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 62 | return typecheck(0, tenv); 63 | } 64 | 65 | private TypeInfo typecheck(int nest, TypeEnv tenv) { 66 | if (hasPostfix(nest)) { 67 | TypeInfo target = typecheck(nest + 1, tenv); 68 | return postfix(nest).typecheck(tenv, target); 69 | } else { 70 | return operand().typecheck(tenv); 71 | } 72 | } 73 | 74 | @Override 75 | public String translate(TypeInfo res) { 76 | return translate(0); 77 | } 78 | 79 | private String translate(int nest) { 80 | if (hasPostfix(nest)) { 81 | String expr = translate(nest + 1); 82 | return ((Postfix) postfix(nest)).translate(expr); 83 | } else { 84 | return operand().translate(null); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/ResizableArrayEnv.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 18754 on 2016/8/1. 7 | */ 8 | public class ResizableArrayEnv extends ArrayEnv { 9 | private Symbols names; 10 | public ResizableArrayEnv() { 11 | super(10, null); 12 | names=new Symbols(); 13 | } 14 | 15 | @Override 16 | public Symbols symbol() { 17 | return names; 18 | } 19 | 20 | @Override 21 | public Object get(String name) { 22 | Integer i=names.find(name); 23 | if (i==null){ 24 | if (outer==null){ 25 | return null; 26 | }else { 27 | return outer.get(name); 28 | } 29 | }else { 30 | return values[i]; 31 | } 32 | } 33 | 34 | @Override 35 | public void put(String name, Object value) { 36 | Environment e=where(name); 37 | if (e==null){ 38 | e=this;//如果在找不到外部 自身有此变量的定义 那么将他放入自身中 39 | } 40 | e.putNew(name,value); 41 | } 42 | 43 | @Override 44 | public void putNew(String name, Object value) { 45 | int index=names.putNew(name); 46 | assign(index,value); 47 | } 48 | 49 | 50 | @Override 51 | public Environment where(String name) { 52 | if (names.find(name)!=null){ 53 | return this; 54 | }else if (outer==null){ 55 | return null; 56 | }else { 57 | return outer.where(name); 58 | } 59 | } 60 | 61 | @Override 62 | public void put(int nest, int index, Object value) { 63 | if (nest==0){ 64 | assign(index,value); 65 | }else { 66 | super.put(nest,index,value); 67 | } 68 | } 69 | 70 | private void assign(int index, Object value) { 71 | if (index>=values.length){ 72 | int newlen=values.length*2; 73 | if (index>=newlen){ 74 | newlen=index+1; 75 | } 76 | values= Arrays.copyOf(values,newlen); 77 | } 78 | values[index]=value; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/StoneExcetion.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/28. 5 | */ 6 | public class StoneExcetion extends RuntimeException { 7 | public StoneExcetion(String s) { 8 | super(s); 9 | } 10 | public StoneExcetion(String msg,ASTree t){ 11 | super(msg+" "+t.location()); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/StoneObject.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/31. 5 | */ 6 | public class StoneObject { 7 | 8 | public static class AccessException extends Exception{} 9 | 10 | private Environment env; 11 | 12 | 13 | public StoneObject(NestEnv nestEnv) { 14 | env=nestEnv; 15 | 16 | } 17 | 18 | public Object read(String name) throws AccessException { 19 | return getEnv(name).get(name); 20 | } 21 | 22 | public void write(String name, Object value) throws AccessException { 23 | getEnv(name).putNew(name,value); 24 | } 25 | /* 26 | Object的env可能有外部的env 此时是不允许访问的 27 | 例如 语句是 28 | 29 | a=100 30 | class t{ 31 | 32 | } 33 | c=t.new 34 | c.a=101 35 | 36 | 此时使用c.a 如何不检查的话 在全局变量中的a 就会被改变 37 | 38 | */ 39 | 40 | 41 | 42 | private Environment getEnv(String member) throws AccessException { 43 | Environment e=env.where(member); 44 | if (e!=null&&e==env){ 45 | return e; 46 | } 47 | throw new AccessException(); 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/StrToken.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/27. 5 | */ 6 | public class StrToken extends Token { 7 | private String str; 8 | public StrToken(int lineNo, String str) { 9 | super(lineNo); 10 | this.str=str; 11 | } 12 | 13 | @Override 14 | public boolean isString() { 15 | return true; 16 | } 17 | 18 | @Override 19 | public String getText() { 20 | return str; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/StringLiteral.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.vm.Code; 7 | 8 | import static com.misakimei.stone.vm.Opcode.SCONST; 9 | import static com.misakimei.stone.vm.Opcode.encodeRegister; 10 | import static com.misakimei.stone.vm.Opcode.encodeShortOffset; 11 | 12 | /** 13 | * Created by 18754 on 2016/7/27. 14 | */ 15 | public class StringLiteral extends ASTLeaf { 16 | public StringLiteral(Token t) { 17 | super(t); 18 | } 19 | 20 | public String value() { 21 | return token().getText(); 22 | } 23 | 24 | @Override 25 | public Object eval(Environment env) { 26 | return value(); 27 | } 28 | 29 | @Override 30 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 31 | return TypeInfo.STRING; 32 | } 33 | 34 | @Override 35 | public String translate(TypeInfo res) { 36 | StringBuilder code = new StringBuilder(); 37 | String literal = value(); 38 | code.append('"'); 39 | for (int i = 0; i < literal.length(); i++) { 40 | char c = literal.charAt(i); 41 | switch (c) { 42 | case '"': 43 | code.append("\\\""); 44 | break; 45 | case '\\': 46 | code.append("\\\\"); 47 | break; 48 | case '\n': 49 | code.append("\\n"); 50 | break; 51 | default: 52 | code.append(c); 53 | } 54 | code.append('"'); 55 | 56 | } 57 | return code.toString(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/SymbolThis.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/4. 5 | */ 6 | public class SymbolThis extends Symbols { 7 | public static final String name="this"; 8 | public SymbolThis(Symbols out){ 9 | super(out); 10 | add(name); 11 | } 12 | 13 | @Override 14 | public int putNew(String key) { 15 | throw new StoneExcetion("SymbolThis putNew 失败"); 16 | } 17 | 18 | @Override 19 | public Location put(String key) { 20 | Location loc=outer.put(key); 21 | if (loc.nest>=0){ 22 | loc.nest++; 23 | } 24 | return loc; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Symbols.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * Created by 18754 on 2016/8/1. 7 | */ 8 | public class Symbols { 9 | public static class Location{ 10 | public int nest,index; 11 | 12 | public Location(int nest, int index) { 13 | this.nest = nest; 14 | this.index = index; 15 | } 16 | } 17 | protected Symbols outer; 18 | protected HashMaptable; 19 | public Symbols(){ 20 | this(null); 21 | } 22 | public Symbols(Symbols outer){ 23 | this.outer=outer; 24 | this.table=new HashMap<>(); 25 | } 26 | public int size(){ 27 | return table.size(); 28 | } 29 | public void append(Symbols s){ 30 | table.putAll(s.table); 31 | } 32 | 33 | public Integer find(String key){ 34 | return table.get(key); 35 | } 36 | 37 | public Location get(String key){ 38 | return get(key,0); 39 | } 40 | 41 | protected Location get(String key, int nest) { 42 | Integer index=table.get(key); 43 | 44 | 45 | 46 | if (index==null){ 47 | if (outer==null){ 48 | return null; 49 | }else { 50 | return outer.get(key,nest+1); 51 | } 52 | }else { 53 | return new Location(nest,index.intValue()); 54 | } 55 | } 56 | 57 | public int putNew(String key) { 58 | Integer i=find(key); 59 | if (i==null){ 60 | return add(key); 61 | }else { 62 | return i; 63 | } 64 | } 65 | 66 | public Location put(String key){ 67 | Location loc=get(key,0); 68 | if (loc==null){ 69 | return new Location(0,add(key)); 70 | } 71 | return loc; 72 | } 73 | 74 | protected int add(String key) { 75 | int i=table.size(); 76 | table.put(key,i); 77 | return i; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/Token.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | /** 4 | * Created by 18754 on 2016/7/26. 5 | */ 6 | public abstract class Token { 7 | public static final Token EOF=new Token(-1){};//EOF 表示文件终结 8 | public static final String EOL="\\n";//EOL 标识一行终结 9 | private int lineNumber; 10 | 11 | protected Token(int line){ 12 | this.lineNumber=line; 13 | } 14 | public int getLineNumber(){return lineNumber;} 15 | public boolean isIdentifier(){return false;}//是否是标识符 16 | public boolean isNumber(){return false;}//是否是数字字面量 17 | public boolean isString(){return false;}//是否是字符串字面量 18 | 19 | public String getText(){return "";}//具体的字符串 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/WhileStmnt.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone; 2 | 3 | import com.misakimei.stone.type.TypeEnv; 4 | import com.misakimei.stone.type.TypeException; 5 | import com.misakimei.stone.type.TypeInfo; 6 | import com.misakimei.stone.vm.Code; 7 | 8 | import java.rmi.activation.ActivationGroup_Stub; 9 | import java.util.List; 10 | 11 | import static com.misakimei.stone.type.ToJava.returnZero; 12 | import static com.misakimei.stone.vm.Opcode.*; 13 | 14 | /** 15 | * Created by 18754 on 2016/7/27. 16 | */ 17 | public class WhileStmnt extends ASTList { 18 | 19 | public WhileStmnt(List lis) { 20 | super(lis); 21 | } 22 | 23 | public ASTree condition() { 24 | return child(0); 25 | } 26 | 27 | public ASTree body() { 28 | return child(1); 29 | } 30 | 31 | public String toString() { 32 | return "(while " + condition() + " " + body() + ")"; 33 | } 34 | 35 | @Override 36 | public Object eval(Environment env) { 37 | Object result = 0; 38 | while (checkCondition(env)) { 39 | result = body().eval(env); 40 | } 41 | return result; 42 | } 43 | 44 | private boolean checkCondition(Environment env) { 45 | Object conditon=condition().eval(env); 46 | boolean con; 47 | if (conditon instanceof Boolean) { 48 | con = ((Boolean) conditon).booleanValue(); 49 | } else if (conditon instanceof Integer) { 50 | int intval = ((Integer) conditon).intValue(); 51 | con = intval != 0 ? true : false;//非0为真 52 | } else { 53 | throw new StoneExcetion("无法判断if 语句中的条件的正确性 ", this); 54 | } 55 | return con; 56 | } 57 | 58 | @Override 59 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 60 | TypeInfo condtype=condition().typecheck(tenv); 61 | condtype.assertSubtypeOf(TypeInfo.INT,tenv,this); 62 | TypeInfo bodytype=body().typecheck(tenv); 63 | return bodytype.union(TypeInfo.INT,tenv); 64 | } 65 | 66 | @Override 67 | public String translate(TypeInfo res) { 68 | String code="while("+condition().translate(null)+"!=0){\n" 69 | +body().translate(res) 70 | +"}\n"; 71 | if (res==null){ 72 | return code; 73 | }else{ 74 | return returnZero(res)+"\n"+code; 75 | } 76 | } 77 | } -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/ArrayInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.ClassParse; 4 | import com.misakimei.stone.Native; 5 | import com.misakimei.stone.NestEnv; 6 | 7 | /** 8 | * Created by 18754 on 2016/7/31. 9 | */ 10 | public class ArrayInterpreter extends BasicInterpreter{ 11 | public static void main(String[] args) { 12 | run(new ClassParse(), new Native().environment(new NestEnv()),"./data/array.stone"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/BasicInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.*; 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileReader; 8 | 9 | /** 10 | * Created by 18754 on 2016/7/28. 11 | * Stone的解释器 12 | * 基本解释器运行测试 13 | */ 14 | public class BasicInterpreter { 15 | private static String file = "./data/interpreter.stone"; 16 | 17 | public static void main(String[] args) { 18 | run(new BasicParser(), new BasicEnv(), file); 19 | } 20 | 21 | public static void run(BasicParser basicParser, Environment basicEnv, String path) { 22 | try { 23 | Lexer lex = new Lexer(new FileReader(new File(path))); 24 | while (lex.peek(0) != Token.EOF) { 25 | ASTree ast = basicParser.parse(lex); 26 | if (!(ast instanceof NULLStmnt)) { 27 | Object o = ast.eval(basicEnv); 28 | //Log.d("debug==> "+o.toString()); 29 | } 30 | } 31 | } catch (FileNotFoundException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/ClassInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.ClassParse; 4 | import com.misakimei.stone.Native; 5 | import com.misakimei.stone.NestEnv; 6 | 7 | /** 8 | * Created by 18754 on 2016/7/31. 9 | */ 10 | public class ClassInterpreter extends BasicInterpreter { 11 | public static void main(String[] args) { 12 | run(new ClassParse(), new Native().environment(new NestEnv()),"./data/classfib.stone"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/ClosureInterpretre.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.ClosureParse; 4 | import com.misakimei.stone.NestEnv; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/29. 8 | * 闭包测试器 9 | */ 10 | public class ClosureInterpretre extends BasicInterpreter{ 11 | public static void main(String[] args) { 12 | run(new ClosureParse(),new NestEnv(),"./data/closure.stone"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/EnvOptInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.*; 4 | 5 | import java.io.FileNotFoundException; 6 | import java.io.FileReader; 7 | 8 | /** 9 | * Created by 18754 on 2016/8/1. 10 | * 使用数组环境 目前只能支持到闭包 不支持 类 数组 11 | */ 12 | public class EnvOptInterpreter { 13 | public static void main(String[] args) throws FileNotFoundException { 14 | run(new ClosureParse(),new Native().environment(new ResizableArrayEnv()),"./data/testfun.stone"); 15 | } 16 | 17 | protected static void run(BasicParser parser, Environment env,String file){ 18 | Lexer lexer= null; 19 | try { 20 | lexer = new Lexer(new FileReader(file)); 21 | } catch (FileNotFoundException e) { 22 | throw new StoneExcetion("找不到要执行的文件 "+file); 23 | } 24 | while (lexer.peek(0)!= Token.EOF){ 25 | ASTree t=parser.parse(lexer); 26 | if (!(t instanceof NULLStmnt)){ 27 | t.lookup(env.symbol()); 28 | Object r=t.eval(env); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/FuncInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.FuncParse; 4 | import com.misakimei.stone.NestEnv; 5 | 6 | /** 7 | * Created by 18754 on 2016/7/29. 8 | * 增加函数功能测试 9 | */ 10 | public class FuncInterpreter extends BasicInterpreter{ 11 | 12 | public static void main(String[] args) { 13 | run(new FuncParse(),new NestEnv(),"./data/fun.stone"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/NativeInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.ClosureParse; 4 | import com.misakimei.stone.Native; 5 | import com.misakimei.stone.NestEnv; 6 | 7 | /** 8 | * Created by 18754 on 2016/7/30. 9 | * 增加原生函数 10 | */ 11 | public class NativeInterpreter extends BasicInterpreter { 12 | 13 | public static void main(String[] args) { 14 | run(new ClosureParse(),new Native().environment(new NestEnv()),"./data/nativefun.stone"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/ObjOpInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.*; 4 | import com.misakimei.stone.run.EnvOptInterpreter; 5 | 6 | import java.io.FileNotFoundException; 7 | import java.io.FileReader; 8 | 9 | /** 10 | * Created by 18754 on 2016/8/4. 11 | * 优化对象操作性能 12 | * 大写的尴尬 使用斐波那契测试时反而慢了 之前是3661 现在是 4671 增加内联缓存大概是 4300-4560 感觉并没有什么卵用 13 | */ 14 | public class ObjOpInterpreter{ 15 | public static void main(String[] args) throws FileNotFoundException { 16 | run(new ClassParse(),new Native().environment(new ResizableArrayEnv()),"./data/classfib.stone"); 17 | } 18 | protected static void run(BasicParser parser, Environment env, String file) throws FileNotFoundException { 19 | Lexer lexer=new Lexer(new FileReader(file)); 20 | while (lexer.peek(0)!= Token.EOF){ 21 | ASTree t=parser.parse(lexer); 22 | if (!(t instanceof NULLStmnt)) { 23 | t.lookup(env.symbol()); 24 | Object r = t.eval(env); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/ParserRunner.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.*; 4 | import com.misakimei.stone.tool.Log; 5 | 6 | import java.io.File; 7 | import java.io.FileNotFoundException; 8 | import java.io.FileReader; 9 | 10 | /** 11 | * Created by 18754 on 2016/7/27. 12 | * 语法树分割测试 13 | */ 14 | public class ParserRunner { 15 | public static void main(String[] args) throws FileNotFoundException { 16 | Lexer lexer=new Lexer(new FileReader(new File("./data/fun.stone"))); 17 | BasicParser bp=new BasicParser(); 18 | while (lexer.peek(0)!= Token.EOF){ 19 | ASTree ast=bp.parse(lexer); 20 | Log.d(ast.toString()); 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/TypedInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.*; 4 | import com.misakimei.stone.tool.Log; 5 | import com.misakimei.stone.type.TypeEnv; 6 | import com.misakimei.stone.type.TypeInfo; 7 | import com.misakimei.stone.type.TypedNatives; 8 | import com.misakimei.stone.type.TypedParse; 9 | 10 | import java.io.File; 11 | import java.io.FileNotFoundException; 12 | import java.io.FileReader; 13 | 14 | /** 15 | * Created by 18754 on 2016/9/14. 16 | */ 17 | public class TypedInterpreter { 18 | 19 | public static void main(String[] args) { 20 | TypeEnv te=new TypeEnv(); 21 | run(new TypedParse(),new TypedNatives(te).environment(new ResizableArrayEnv()),te,"./data/typecheck.stone"); 22 | } 23 | private static void run(BasicParser bp, Environment environment, TypeEnv te,String path) { 24 | try { 25 | Lexer lex = new Lexer(new FileReader(new File(path))); 26 | while (lex.peek(0)!= Token.EOF){ 27 | ASTree asTree=bp.parse(lex); 28 | asTree.lookup(environment.symbol()); 29 | TypeInfo type=asTree.typecheck(te); 30 | Object r=asTree.eval(environment); 31 | Log.d("==> "+r+" : "+type); 32 | } 33 | } catch (FileNotFoundException e) { 34 | e.printStackTrace(); 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/run/VMInterpreter.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.run; 2 | 3 | import com.misakimei.stone.FuncParse; 4 | import com.misakimei.stone.Native; 5 | import com.misakimei.stone.vm.StoneVMEnv; 6 | 7 | /** 8 | * Created by 18754 on 2016/9/6. 9 | * fib 33 使用虚拟机优化2988毫秒 和之前的ObjOpInterpreter 比要快 另外调试真是太痛苦啦 最后果然是粗心出错了 2333 10 | */ 11 | public class VMInterpreter extends EnvOptInterpreter{ 12 | public static void main(String[] args) { 13 | run(new FuncParse(),new Native().environment(new StoneVMEnv(100000,100000,100000)),"./data/fun.stone"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/tool/Log.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.tool; 2 | 3 | 4 | /** 5 | * Created by 18754 on 2016/7/27. 6 | */ 7 | public class Log { 8 | 9 | public static void d(String msg){ 10 | System.out.println(msg); 11 | } 12 | public static void e(String msg){ 13 | System.out.println(msg); 14 | } 15 | 16 | public static void d(Object o) { 17 | if (o==null){ 18 | d("null"); 19 | }else { 20 | d(o.toString()); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/JavaFunction.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.*; 4 | 5 | import java.lang.reflect.InvocationTargetException; 6 | 7 | /** 8 | * Created by 18754 on 2016/9/17. 9 | */ 10 | public class JavaFunction extends Function { 11 | protected String className; 12 | protected Classclazz; 13 | 14 | public JavaFunction(String name,String method,JavaLoader loader){ 15 | super(null,null,null); 16 | className=name; 17 | clazz=loader.load(className,method); 18 | } 19 | 20 | public static String className(String name){ 21 | return name; 22 | } 23 | 24 | public Object invoke(Object[]args){ 25 | try { 26 | return clazz.getDeclaredMethods()[0].invoke(null,args); 27 | } catch (Exception e) { 28 | throw new StoneExcetion(e.getMessage()); 29 | } 30 | } 31 | 32 | public JavaFunction(ParamterList paramters, BlockStment body, Environment env) { 33 | super(paramters, body, env); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/JavaLoader.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.StoneExcetion; 4 | import com.misakimei.stone.tool.Log; 5 | import javassist.CannotCompileException; 6 | import javassist.ClassPool; 7 | import javassist.CtClass; 8 | import javassist.CtMethod; 9 | 10 | /** 11 | * Created by 18754 on 2016/9/17. 12 | */ 13 | public class JavaLoader { 14 | protected ClassLoader loader; 15 | protected ClassPool cpool; 16 | 17 | public JavaLoader(){ 18 | cpool=new ClassPool(null); 19 | cpool.appendSystemPath(); 20 | loader=new ClassLoader(this.getClass().getClassLoader()){}; 21 | } 22 | public Classload(String className,String method){ 23 | CtClass cc=cpool.makeClass(className); 24 | try { 25 | Log.d(method); 26 | cc.addMethod(CtMethod.make(method,cc)); 27 | return cc.toClass(loader,null); 28 | } catch (CannotCompileException e) { 29 | e.printStackTrace(); 30 | throw new StoneExcetion(e.getMessage()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/Runtime.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/17. 7 | */ 8 | public class Runtime { 9 | public static int eq(Object a,Object b){ 10 | if (a==null){ 11 | return b==null?1:0; 12 | }else { 13 | return a.equals(b)?1:0; 14 | } 15 | } 16 | public static Object plus(Object a,Object b){ 17 | if (a instanceof Integer&&b instanceof Integer){ 18 | return ((Integer) a).intValue()+((Integer) b).intValue(); 19 | }else { 20 | return a.toString()+b.toString(); 21 | } 22 | } 23 | public static int writeInt(ArrayEnv env,int index,int val){ 24 | env.put(0,index,val); 25 | return val; 26 | } 27 | public static String writeString(ArrayEnv env,int index,String val){ 28 | env.put(0,index,val); 29 | return val; 30 | } 31 | public static Object writeAny(ArrayEnv env,int index,Object val){ 32 | env.put(0,index,val); 33 | return val; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/ToJava.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ASTree; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/17. 7 | */ 8 | public class ToJava { 9 | public static final String METHOD="m"; 10 | public static final String LOCAL="v"; 11 | public static final String ENV="env"; 12 | public static final String RESULT="res"; 13 | public static final String ENV_TYPE="com.misakimei.stone.ArrayEnv"; 14 | 15 | 16 | public static String transloateExpr(ASTree ast,TypeInfo from,TypeInfo to){ 17 | return transloateExpr(ast.translate(null),from,to); 18 | } 19 | public static String transloateExpr(String expr,TypeInfo from,TypeInfo to){ 20 | from=from.type(); 21 | to=to.type(); 22 | if (from==TypeInfo.INT){ 23 | if (to==TypeInfo.ANY){ 24 | return "new Integer("+expr+")"; 25 | }else if (to==TypeInfo.STRING){ 26 | return "Integer.toString("+expr+")"; 27 | } 28 | }else if (from==TypeInfo.ANY){ 29 | if (to==TypeInfo.STRING){ 30 | return expr+".toString()"; 31 | }else if (to==TypeInfo.INT){ 32 | return "((Integer)"+expr+").intValue()"; 33 | } 34 | } 35 | return expr; 36 | } 37 | public static String returnZero(TypeInfo to){ 38 | if (to.type()==TypeInfo.ANY){ 39 | return RESULT+"=new Integer(0);"; 40 | }else { 41 | return RESULT+"=0"; 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/TypeException.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ASTree; 4 | import com.misakimei.stone.StoneExcetion; 5 | 6 | /** 7 | * Created by 18754 on 2016/9/12. 8 | */ 9 | public class TypeException extends StoneExcetion { 10 | public TypeException(String s) { 11 | super(s); 12 | } 13 | 14 | public TypeException(String msg, ASTree t) { 15 | super(msg, t); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/TypeTag.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ASTLeaf; 4 | import com.misakimei.stone.ASTList; 5 | import com.misakimei.stone.ASTree; 6 | import com.misakimei.stone.Environment; 7 | 8 | import java.util.Iterator; 9 | import java.util.List; 10 | 11 | /** 12 | * Created by 18754 on 2016/9/12. 13 | */ 14 | public class TypeTag extends ASTList { 15 | 16 | public static final String UNDEF=""; 17 | 18 | public TypeTag(List lis) { 19 | super(lis); 20 | } 21 | 22 | public String type(){ 23 | if (numChildren()>0){ 24 | return ((ASTLeaf)child(0)).token().getText(); 25 | }else { 26 | return UNDEF; 27 | } 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return ":"+type(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/TypedNatives.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.Environment; 4 | import com.misakimei.stone.Native; 5 | import com.misakimei.stone.ResizableArrayEnv; 6 | 7 | /** 8 | * Created by 18754 on 2016/9/14. 9 | */ 10 | public class TypedNatives extends Native { 11 | protected TypeEnv typeEnv; 12 | public TypedNatives(TypeEnv te) { 13 | typeEnv=te; 14 | } 15 | 16 | protected void append(Environment env,String name,Classclassz,String methodname,TypeInfo type,Class ...params){ 17 | append(env,name,classz,methodname,params); 18 | int inndex=env.symbol().find(name); 19 | typeEnv.put(0,inndex,type); 20 | } 21 | protected void appendNatives(Environment env){ 22 | append(env,"print",print.class,"m",TypeInfo.function(TypeInfo.INT,TypeInfo.ANY),Object.class); 23 | append(env,"read",read.class,"m",TypeInfo.function(TypeInfo.STRING),Object.class); 24 | append(env,"print",length.class,"m",TypeInfo.function(TypeInfo.INT,TypeInfo.STRING),Object.class); 25 | append(env,"print",toInt.class,"m",TypeInfo.function(TypeInfo.INT,TypeInfo.ANY),Object.class); 26 | append(env,"print",currenttime.class,"m",TypeInfo.function(TypeInfo.INT),Object.class); 27 | 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/TypedParse.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.FuncParse; 4 | import com.misakimei.stone.parser.Parser; 5 | 6 | import static com.misakimei.stone.parser.Parser.rule; 7 | 8 | /** 9 | * Created by 18754 on 2016/9/12. 10 | */ 11 | public class TypedParse extends FuncParse{ 12 | Parser typetag=rule(TypeTag.class).sep(":").identifier(reserved); 13 | Parser variable=rule(VarStmnt.class).sep("var").identifier(reserved).maybe(typetag).sep("=").ast(expr); 14 | 15 | public TypedParse(){ 16 | reserved.add(":"); 17 | param.maybe(typetag); 18 | def.reset().sep("def").identifier(reserved).ast(paramList) 19 | .maybe(typetag).ast(block); 20 | 21 | statement.insertChoice(variable); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/VarStmnt.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.*; 4 | 5 | import java.util.Iterator; 6 | import java.util.List; 7 | 8 | import static com.misakimei.stone.type.ToJava.LOCAL; 9 | import static com.misakimei.stone.type.ToJava.transloateExpr; 10 | 11 | /** 12 | * Created by 18754 on 2016/9/12. 13 | */ 14 | public class VarStmnt extends ASTList { 15 | protected int index; 16 | protected TypeInfo vartype,valuetype; 17 | 18 | 19 | public VarStmnt(List lis) { 20 | super(lis); 21 | } 22 | public String name(){ 23 | return ((ASTLeaf)child(0)).token().getText(); 24 | } 25 | public TypeTag type(){ 26 | return (TypeTag) child(1); 27 | } 28 | public ASTree initializer(){ 29 | return child(2); 30 | } 31 | 32 | public void lookup(Symbols syms){ 33 | index=syms.putNew(name()); 34 | initializer().lookup(syms); 35 | } 36 | 37 | @Override 38 | public Object eval(Environment env) { 39 | Object val=initializer().eval(env); 40 | env.put(0,index,val); 41 | return val; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "(var "+name()+" "+type()+" "+initializer()+")"; 47 | } 48 | 49 | @Override 50 | public TypeInfo typecheck(TypeEnv tenv) throws TypeException { 51 | if (tenv.get(0,index)!=null) 52 | { 53 | throw new TypeException("定义重复 "+name(),this); 54 | } 55 | vartype=TypeInfo.get(type()); 56 | tenv.put(0,index,vartype); 57 | valuetype=initializer().typecheck(tenv); 58 | valuetype.assertSubtypeOf(vartype,tenv,this); 59 | return vartype; 60 | } 61 | 62 | @Override 63 | public String translate(TypeInfo res) { 64 | return LOCAL+index+"="+transloateExpr(initializer(),valuetype,vartype); 65 | } 66 | } -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/currenttime.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/14. 7 | */ 8 | public class currenttime { 9 | public static long starttime=System.currentTimeMillis(); 10 | public static int m(ArrayEnv env){ 11 | return m(); 12 | } 13 | 14 | private static int m() { 15 | return (int) (System.currentTimeMillis()-starttime); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/length.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/14. 7 | */ 8 | public class length { 9 | public static int m(ArrayEnv env,String s){return m(s);} 10 | 11 | private static int m(String s) { 12 | return s.length(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/print.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/14. 7 | */ 8 | public class print { 9 | public static int m(ArrayEnv env, Object obj){return m(obj);} 10 | 11 | private static int m(Object obj) { 12 | System.out.println(obj.toString()); 13 | return 0; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/read.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | 5 | import javax.swing.*; 6 | 7 | /** 8 | * Created by 18754 on 2016/9/14. 9 | */ 10 | public class read { 11 | public static String m(ArrayEnv env){return m();} 12 | 13 | private static String m() { 14 | return JOptionPane.showInputDialog(null); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/type/toInt.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.type; 2 | 3 | import com.misakimei.stone.ArrayEnv; 4 | import com.misakimei.stone.StringLiteral; 5 | 6 | /** 7 | * Created by 18754 on 2016/9/14. 8 | */ 9 | public class toInt { 10 | public static int m(ArrayEnv env, Object obj){return m(obj);} 11 | 12 | private static int m(Object obj) { 13 | if (obj instanceof String){ 14 | return Integer.parseInt((String) obj); 15 | }else if (obj instanceof Integer){ 16 | return ((Integer) obj).intValue(); 17 | }else { 18 | throw new NumberFormatException(obj.toString()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/Code.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | /** 4 | * Created by 18754 on 2016/9/6. 5 | */ 6 | public class Code { 7 | public StoneVM svm; 8 | public int codesize; 9 | public int numofStrings; 10 | public int nextReg; 11 | public int frameSize; 12 | 13 | public Code(StoneVM svm) { 14 | this.svm = svm; 15 | codesize = 0; 16 | numofStrings = 0; 17 | } 18 | 19 | public int position() { 20 | return codesize; 21 | } 22 | 23 | public void set(short value, int pos) { 24 | svm.code()[pos] = (byte) (value >>> 8); 25 | svm.code()[pos + 1] = (byte) value; 26 | } 27 | public void add(short s){ 28 | add((byte)(s>>8)); 29 | add((byte)s); 30 | } 31 | public void add(int i) { 32 | add((byte) (i >>> 24)); 33 | add((byte) (i >>> 16)); 34 | add((byte) (i >>> 8)); 35 | add((byte) i); 36 | } 37 | public void add(byte b){ 38 | svm.code()[codesize++]=b; 39 | } 40 | 41 | 42 | public int record(String s){ 43 | svm.strings()[numofStrings]=s; 44 | return numofStrings++; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/EnvEx.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | import com.misakimei.stone.Environment; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/6. 7 | */ 8 | public interface EnvEx extends Environment { 9 | StoneVM stoneVM(); 10 | Code code(); 11 | } 12 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/HeapMemory.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | /** 4 | * Created by 18754 on 2016/8/31. 5 | */ 6 | public interface HeapMemory { 7 | Object read(int index); 8 | void write(int index,Object v); 9 | } 10 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/Opcode.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | import com.misakimei.stone.StoneExcetion; 4 | 5 | /** 6 | * Created by 18754 on 2016/8/31. 7 | */ 8 | public class Opcode { 9 | public static final byte ICONST =1; 10 | public static final byte BCONST =2; 11 | public static final byte SCONST =3; 12 | public static final byte MOVE =4; 13 | public static final byte GMOVE =5; 14 | public static final byte IFZERO =6; 15 | public static final byte GOTO =7; 16 | public static final byte CALL =8; 17 | public static final byte RETURN =9; 18 | public static final byte SAVE =10; 19 | public static final byte RESTORE=11; 20 | public static final byte NEG =12; 21 | public static final byte ADD =13; 22 | public static final byte SUB =14; 23 | public static final byte MUL =15; 24 | public static final byte DIV =16; 25 | public static final byte REM =17; 26 | public static final byte EQUAL =18; 27 | public static final byte MORE =19; 28 | public static final byte LESS =20; 29 | 30 | 31 | public static byte encodeRegister(int reg){ 32 | if (reg> StoneVM.NUM_OF_REG){ 33 | throw new StoneExcetion("无法解析此指令"); 34 | }else { 35 | return (byte)-(reg+1); 36 | } 37 | } 38 | 39 | public static int decodeRegister(byte operand){ 40 | return -1-operand; 41 | } 42 | 43 | public static byte encodeOffset(int offset){ 44 | if (offset>Byte.MAX_VALUE){ 45 | throw new StoneExcetion("过大的偏移值 "+offset); 46 | } 47 | return (byte) offset; 48 | } 49 | 50 | public static short encodeShortOffset(int offset){ 51 | if (offsetShort.MAX_VALUE){ 52 | throw new StoneExcetion("过大的 short 偏移值 "+offset); 53 | } 54 | return (short) offset; 55 | } 56 | 57 | public static int decodeOffset(byte operand){return operand;} 58 | 59 | public static boolean isRegister(byte operand){return operand<0;} 60 | 61 | public static boolean isOffset(byte operand){return operand>=0;} 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/StoneVMEnv.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | import com.misakimei.stone.ResizableArrayEnv; 4 | 5 | /** 6 | * Created by 18754 on 2016/9/6. 7 | */ 8 | public class StoneVMEnv extends ResizableArrayEnv implements HeapMemory,EnvEx { 9 | 10 | protected StoneVM svm; 11 | protected Code code; 12 | 13 | public StoneVMEnv(int codesize,int stacksize,int stringsize) { 14 | svm=new StoneVM(codesize,stacksize,stringsize,this); 15 | code=new Code(svm); 16 | } 17 | 18 | 19 | 20 | @Override 21 | public Object read(int index) { 22 | return values[index]; 23 | } 24 | 25 | @Override 26 | public void write(int index, Object v) { 27 | values[index]=v; 28 | } 29 | 30 | @Override 31 | public StoneVM stoneVM() { 32 | return svm; 33 | } 34 | 35 | @Override 36 | public Code code() { 37 | return code; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/misakimei/stone/vm/VMFunction.java: -------------------------------------------------------------------------------- 1 | package com.misakimei.stone.vm; 2 | 3 | import com.misakimei.stone.BlockStment; 4 | import com.misakimei.stone.Environment; 5 | import com.misakimei.stone.Function; 6 | import com.misakimei.stone.ParamterList; 7 | 8 | /** 9 | * Created by 18754 on 2016/9/6. 10 | */ 11 | public class VMFunction extends Function{ 12 | 13 | protected int entry; 14 | public VMFunction(ParamterList paramters, BlockStment body, Environment env,int entry){ 15 | super(paramters,body,env); 16 | this.entry=entry; 17 | } 18 | 19 | public int entry() { 20 | return entry; 21 | } 22 | 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/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 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/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 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/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 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/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 javassist.bytecode; 18 | 19 | import 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/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 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/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 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/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 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/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 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/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 javassist.bytecode.analysis; 17 | 18 | import java.util.NoSuchElementException; 19 | 20 | class IntQueue { 21 | private static class Entry { 22 | private IntQueue.Entry next; 23 | private int value; 24 | private Entry(int value) { 25 | this.value = value; 26 | } 27 | } 28 | private IntQueue.Entry head; 29 | 30 | private IntQueue.Entry tail; 31 | 32 | void add(int value) { 33 | IntQueue.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/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 javassist.bytecode.analysis; 17 | 18 | import java.util.ArrayList; 19 | import java.util.Collection; 20 | import java.util.HashSet; 21 | import java.util.List; 22 | import java.util.Set; 23 | 24 | /** 25 | * Represents a nested method subroutine (marked by JSR and RET). 26 | * 27 | * @author Jason T. Greene 28 | */ 29 | public class Subroutine { 30 | //private Set callers = new HashSet(); 31 | private List callers = new ArrayList(); 32 | private Set access = new HashSet(); 33 | private int start; 34 | 35 | public Subroutine(int start, int caller) { 36 | this.start = start; 37 | callers.add(new Integer(caller)); 38 | } 39 | 40 | public void addCaller(int caller) { 41 | callers.add(new Integer(caller)); 42 | } 43 | 44 | public int start() { 45 | return start; 46 | } 47 | 48 | public void access(int index) { 49 | access.add(new Integer(index)); 50 | } 51 | 52 | public boolean isAccessed(int index) { 53 | return access.contains(new Integer(index)); 54 | } 55 | 56 | public Collection accessed() { 57 | return access; 58 | } 59 | 60 | public Collection callers() { 61 | return callers; 62 | } 63 | 64 | public String toString() { 65 | return "start = " + start + " callers = " + callers.toString(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/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 javassist.bytecode.analysis; 17 | 18 | import javassist.bytecode.CodeIterator; 19 | import 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/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/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 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/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 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/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/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/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 javassist.bytecode.stackmap; 18 | 19 | import javassist.bytecode.StackMapTable; 20 | 21 | public interface TypeTag { 22 | String TOP_TYPE = "*top*"; 23 | TypeData.BasicType TOP = new TypeData.BasicType(TOP_TYPE, StackMapTable.TOP, ' '); 24 | TypeData.BasicType INTEGER = new TypeData.BasicType("int", StackMapTable.INTEGER, 'I'); 25 | TypeData.BasicType FLOAT = new TypeData.BasicType("float", StackMapTable.FLOAT, 'F'); 26 | TypeData.BasicType DOUBLE = new TypeData.BasicType("double", StackMapTable.DOUBLE, 'D'); 27 | TypeData.BasicType LONG = new TypeData.BasicType("long", StackMapTable.LONG, 'J'); 28 | 29 | // and NULL, THIS, OBJECT, UNINIT 30 | } 31 | -------------------------------------------------------------------------------- /src/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 javassist.compiler; 18 | 19 | import javassist.CannotCompileException; 20 | import 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/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 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/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 javassist.compiler; 18 | 19 | import 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/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 javassist.compiler; 18 | 19 | import javassist.bytecode.Bytecode; 20 | import javassist.compiler.ast.ASTList; 21 | 22 | /** 23 | * An interface to an object for implementing $proceed(). 24 | * 25 | * @see javassist.compiler.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/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 javassist.compiler; 18 | 19 | import java.util.HashMap; 20 | import javassist.compiler.ast.Declarator; 21 | 22 | public final class SymbolTable extends HashMap { 23 | private SymbolTable parent; 24 | 25 | public SymbolTable() { this(null); } 26 | 27 | public SymbolTable(SymbolTable p) { 28 | super(); 29 | parent = p; 30 | } 31 | 32 | public SymbolTable getParent() { return parent; } 33 | 34 | public Declarator lookup(String name) { 35 | Declarator found = (Declarator)get(name); 36 | if (found == null && parent != null) 37 | return parent.lookup(name); 38 | else 39 | return found; 40 | } 41 | 42 | public void append(String name, Declarator value) { 43 | put(name, value); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/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 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/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 javassist.compiler.ast; 18 | 19 | import java.io.Serializable; 20 | import javassist.compiler.CompileError; 21 | 22 | /** 23 | * Abstract Syntax Tree. An ASTree object represents a node of 24 | * a binary tree. If the node is a leaf node, both getLeft() 25 | * and getRight() returns null. 26 | */ 27 | public abstract class ASTree implements Serializable { 28 | public ASTree getLeft() { return null; } 29 | 30 | public ASTree getRight() { return null; } 31 | 32 | public void setLeft(ASTree _left) {} 33 | 34 | public void setRight(ASTree _right) {} 35 | 36 | /** 37 | * Is a method for the visitor pattern. It calls 38 | * atXXX() on the given visitor, where 39 | * XXX is the class name of the node object. 40 | */ 41 | public abstract void accept(Visitor v) throws CompileError; 42 | 43 | public String toString() { 44 | StringBuffer sbuf = new StringBuffer(); 45 | sbuf.append('<'); 46 | sbuf.append(getTag()); 47 | sbuf.append('>'); 48 | return sbuf.toString(); 49 | } 50 | 51 | /** 52 | * Returns the type of this node. This method is used by 53 | * toString(). 54 | */ 55 | protected String getTag() { 56 | String name = getClass().getName(); 57 | return name.substring(name.lastIndexOf('.') + 1); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import javassist.compiler.CompileError; 20 | import javassist.compiler.TokenId; 21 | import javassist.compiler.MemberResolver; 22 | 23 | /** 24 | * Method call expression. 25 | */ 26 | public class CallExpr extends Expr { 27 | private MemberResolver.Method method; // cached result of lookupMethod() 28 | 29 | private CallExpr(ASTree _head, ASTList _tail) { 30 | super(TokenId.CALL, _head, _tail); 31 | method = null; 32 | } 33 | 34 | public void setMethod(MemberResolver.Method m) { 35 | method = m; 36 | } 37 | 38 | public MemberResolver.Method getMethod() { 39 | return method; 40 | } 41 | 42 | public static CallExpr makeCall(ASTree target, ASTree args) { 43 | return new CallExpr(target, new ASTList(args)); 44 | } 45 | 46 | public void accept(Visitor v) throws CompileError { v.atCallExpr(this); } 47 | } 48 | -------------------------------------------------------------------------------- /src/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 javassist.compiler.ast; 18 | 19 | import javassist.compiler.TokenId; 20 | import javassist.compiler.CompileError; 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import javassist.compiler.CompileError; 20 | import javassist.CtField; 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import javassist.compiler.TokenId; 20 | import javassist.compiler.CompileError; 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.compiler.ast; 18 | 19 | import 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/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 javassist.convert; 18 | 19 | import javassist.CtMethod; 20 | import javassist.NotFoundException; 21 | import 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/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 javassist.convert; 18 | 19 | import javassist.CannotCompileException; 20 | import javassist.CtClass; 21 | import javassist.bytecode.BadBytecode; 22 | import javassist.bytecode.CodeAttribute; 23 | import javassist.bytecode.CodeIterator; 24 | import javassist.bytecode.ConstPool; 25 | import javassist.bytecode.MethodInfo; 26 | import javassist.bytecode.Opcode; 27 | 28 | /** 29 | * Transformer and its subclasses are used for executing 30 | * code transformation specified by CodeConverter. 31 | * 32 | * @see javassist.CodeConverter 33 | */ 34 | public abstract class Transformer implements Opcode { 35 | private Transformer next; 36 | 37 | public Transformer(Transformer t) { 38 | next = t; 39 | } 40 | 41 | public Transformer getNext() { return next; } 42 | 43 | public void initialize(ConstPool cp, CodeAttribute attr) {} 44 | 45 | public void initialize(ConstPool cp, CtClass clazz, MethodInfo minfo) throws CannotCompileException { 46 | initialize(cp, minfo.getCodeAttribute()); 47 | } 48 | 49 | public void clean() {} 50 | 51 | public abstract int transform(CtClass clazz, int pos, CodeIterator it, 52 | ConstPool cp) throws CannotCompileException, BadBytecode; 53 | 54 | public int extraLocals() { return 0; } 55 | 56 | public int extraStack() { return 0; } 57 | } 58 | -------------------------------------------------------------------------------- /src/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 javassist.expr; 18 | 19 | import javassist.CtClass; 20 | import javassist.CtConstructor; 21 | import javassist.CtMethod; 22 | import javassist.NotFoundException; 23 | import javassist.bytecode.CodeIterator; 24 | import 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/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/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 javassist.jar
    17 | 
18 | 19 |

It prints the version number on the console. 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/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 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 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/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 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/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 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/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/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 javassist.scopedpool; 18 | 19 | import 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/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 javassist.scopedpool; 18 | 19 | import 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/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/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 javassist.tools; 18 | 19 | import java.io.*; 20 | import javassist.bytecode.ClassFile; 21 | import javassist.bytecode.ClassFilePrinter; 22 | 23 | /** 24 | * Dump is a tool for viewing the class definition in the given 25 | * class file. Unlike the JDK javap tool, Dump works even if 26 | * the class file is broken. 27 | * 28 | *

For example, 29 | *

% java javassist.tools.Dump foo.class
30 | * 31 | *

prints the contents of the constant pool and the list of methods 32 | * and fields. 33 | */ 34 | public class Dump { 35 | private Dump() {} 36 | 37 | /** 38 | * Main method. 39 | * 40 | * @param args args[0] is the class file name. 41 | */ 42 | public static void main(String[] args) throws Exception { 43 | if (args.length != 1) { 44 | System.err.println("Usage: java Dump "); 45 | return; 46 | } 47 | 48 | DataInputStream in = new DataInputStream( 49 | new FileInputStream(args[0])); 50 | ClassFile w = new ClassFile(in); 51 | PrintWriter out = new PrintWriter(System.out, true); 52 | out.println("*** constant pool ***"); 53 | w.getConstPool().print(out); 54 | out.println(); 55 | out.println("*** members ***"); 56 | ClassFilePrinter.print(w, out); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/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 javassist.tools; 17 | 18 | import javassist.ClassPool; 19 | import javassist.CtClass; 20 | import 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 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 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/javassist/tools/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Covenient tools. 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/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 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/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 javassist.tools.reflect; 18 | 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.IllegalAccessException; 21 | 22 | /** 23 | * Thrown when method invocation using the reflection API has thrown 24 | * an exception. 25 | * 26 | * @see javassist.tools.reflect.Metaobject#trapMethodcall(int, Object[]) 27 | * @see javassist.tools.reflect.ClassMetaobject#trapMethodcall(int, Object[]) 28 | * @see javassist.tools.reflect.ClassMetaobject#invoke(Object, int, Object[]) 29 | */ 30 | public class CannotInvokeException extends RuntimeException { 31 | 32 | private Throwable err = null; 33 | 34 | /** 35 | * Returns the cause of this exception. It may return null. 36 | */ 37 | public Throwable getReason() { return err; } 38 | 39 | /** 40 | * Constructs a CannotInvokeException with an error message. 41 | */ 42 | public CannotInvokeException(String reason) { 43 | super(reason); 44 | } 45 | 46 | /** 47 | * Constructs a CannotInvokeException with an InvocationTargetException. 48 | */ 49 | public CannotInvokeException(InvocationTargetException e) { 50 | super("by " + e.getTargetException().toString()); 51 | err = e.getTargetException(); 52 | } 53 | 54 | /** 55 | * Constructs a CannotInvokeException with an IllegalAccessException. 56 | */ 57 | public CannotInvokeException(IllegalAccessException e) { 58 | super("by " + e.toString()); 59 | err = e; 60 | } 61 | 62 | /** 63 | * Constructs a CannotInvokeException with an ClassNotFoundException. 64 | */ 65 | public CannotInvokeException(ClassNotFoundException e) { 66 | super("by " + e.toString()); 67 | err = e; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/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 javassist.tools.reflect; 18 | 19 | import 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 javassist.tools.reflect.Reflection#makeReflective(CtClass,CtClass,CtClass) 29 | * @see javassist.CannotCompileException 30 | */ 31 | public class CannotReflectException extends CannotCompileException { 32 | public CannotReflectException(String msg) { 33 | super(msg); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/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 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/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 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/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/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 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/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 javassist.tools.rmi; 18 | 19 | /** 20 | * An interface implemented by proxy classes. 21 | * 22 | * @see javassist.tools.rmi.StubGenerator 23 | */ 24 | public interface Proxy { 25 | int _getObjectId(); 26 | } 27 | -------------------------------------------------------------------------------- /src/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 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/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 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/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 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/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/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 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/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/javassist/util/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Utility classes. 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/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 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/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 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/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 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/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 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/javassist/util/proxy/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dynamic proxy (similar to Enhancer of cglib). 4 | See ProxyFactory for more details. 5 | 6 | 7 | --------------------------------------------------------------------------------