├── JVM-final ├── JavaCode ├── 20-good-map │ ├── GoodMap.iml │ ├── README.md │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ ├── Game.java │ │ │ │ ├── Map.java │ │ │ │ ├── Player.java │ │ │ │ └── Result.java │ │ └── test │ │ │ └── java │ │ │ └── GameTest.java │ └── target │ │ ├── classes │ │ ├── Game.class │ │ ├── Map.class │ │ ├── Player.class │ │ └── Result.class │ │ └── test-classes │ │ └── GameTest.class ├── 252-commandlineutil │ ├── README.md │ ├── README.pdf │ ├── commandLineUtil.iml │ ├── figure │ │ └── Flow_Chart.jpg │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ └── edu │ │ │ │ └── nju │ │ │ │ └── CommandLineUtil.java │ │ └── test │ │ │ └── java │ │ │ └── edu │ │ │ └── nju │ │ │ └── CommandLineUtilTest.java │ └── target │ │ ├── classes │ │ └── edu │ │ │ └── nju │ │ │ └── CommandLineUtil.class │ │ └── test-classes │ │ └── edu │ │ └── nju │ │ └── CommandLineUtilTest.class ├── 259-jvvm-02-classfilereader-new3 │ ├── README.md │ ├── class-file-reader.iml │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ └── edu │ │ │ │ └── nju │ │ │ │ ├── ArchivedEntry.java │ │ │ │ ├── ClassFileReader.java │ │ │ │ ├── CompositeEntry.java │ │ │ │ ├── DirEntry.java │ │ │ │ ├── Entry.java │ │ │ │ ├── IOUtil.java │ │ │ │ └── WildEntry.java │ │ └── test │ │ │ ├── java │ │ │ └── edu │ │ │ │ └── nju │ │ │ │ └── ClassFileReaderTest.java │ │ │ ├── libs │ │ │ └── parser.jar │ │ │ └── testfilepath │ │ │ └── dir │ │ │ ├── Object.class │ │ │ ├── java │ │ │ └── lang │ │ │ │ └── Object.class │ │ │ └── subdir │ │ │ ├── empty.jar │ │ │ └── rt.JAR │ └── target │ │ ├── classes │ │ └── edu │ │ │ └── nju │ │ │ ├── ArchivedEntry.class │ │ │ ├── ClassFileReader.class │ │ │ ├── CompositeEntry.class │ │ │ ├── DirEntry.class │ │ │ ├── Entry.class │ │ │ ├── IOUtil.class │ │ │ └── WildEntry.class │ │ └── test-classes │ │ └── edu │ │ └── nju │ │ └── ClassFileReaderTest.class ├── 40-good-map-game │ ├── GoodMapGame.iml │ ├── README.md │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ ├── Game.java │ │ │ │ ├── Gun.java │ │ │ │ ├── Map.java │ │ │ │ ├── Player.java │ │ │ │ └── Result.java │ │ └── test │ │ │ └── java │ │ │ └── GameTest.java │ └── target │ │ ├── classes │ │ ├── Game.class │ │ ├── Gun.class │ │ ├── Map.class │ │ ├── Player.class │ │ └── Result.class │ │ ├── maven-status │ │ └── maven-compiler-plugin │ │ │ ├── compile │ │ │ └── default-compile │ │ │ │ ├── createdFiles.lst │ │ │ │ └── inputFiles.lst │ │ │ └── testCompile │ │ │ └── default-testCompile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ │ ├── surefire-reports │ │ ├── GameTest.txt │ │ └── TEST-GameTest.xml │ │ └── test-classes │ │ └── GameTest.class ├── AddDigits.java ├── Arithmetic.java ├── ArrayToInteger.java ├── BadMatrix.java ├── Bin2Dec.java ├── BinarySearch.java ├── CSVFile.java ├── CheckOddEven.java ├── CircleComputation.java ├── ClimbStairs.java ├── Comparenumbers.java ├── ComputePI.java ├── ContainsDuplicate.java ├── CurrencyCalculation.java ├── DetectCaptial.java ├── Digits.java ├── Fibonacci.java ├── GradesHistogram.java ├── HammingDistance.java ├── HappyNumber.java ├── HelloWorld.java ├── KthLargestElement.java ├── LeftRotateString.java ├── MathStudy.java ├── MatrixCalculation.java ├── MyMatrix.java ├── NumberComplement.java ├── NumberConversion.java ├── NumberOfOneBits.java ├── OrderString.java ├── PerfectAndDeficientNumbers.java ├── PhoneKeyPad.java ├── Polynomial.java ├── PowerOfTwo.java ├── PrimeFactors.java ├── PrintNumberInWord.java ├── PrintTriangles.java ├── ReOrderArray.java ├── RepeatedSubstringPattern.java ├── ResponseTimeCalculation.java ├── ReverseInteger.java ├── ReverseString.java ├── SingleCharacterI.java ├── SingleCharacterII.java ├── SingleCharacterIII.java ├── StringAddition.java ├── ThreeSum.java ├── TicTacToe │ ├── Board.java │ ├── Game.java │ ├── GameChessStrategy.java │ ├── GameChessStrategy_Uzi.java │ ├── GameWinStrategy_HV.java │ ├── GameWinStrategy_HVD.java │ ├── Player.java │ └── Result.java ├── TimeTable.java ├── TrigonometricSeries.java ├── UglyNumber.java ├── heartrate.java └── simple-tictactoe │ ├── Game.java │ └── Result.java ├── PythonCode ├── Alice_Bob_and_Chocolate.py ├── Taxi.py ├── Xenia_and_Ringroad.py ├── arrival_of_general.py ├── beautiful_year.py ├── bmi_judge.py ├── characters.py ├── delete_staff.py ├── filter_even_numbers.py ├── football.py ├── football_dangerous.py ├── generate_multiplication_table.py ├── grammarlesson.py ├── heart_rate_cal.py ├── insomnia_cure.py ├── interest_cal.py ├── isAnagram_cal.py ├── magnets.py ├── max_number.py ├── new_year_candles.py ├── number_to_month.py ├── paint_cal.py ├── read_csv.py ├── room_area_cal.py ├── stream_process.py ├── sum_of_nums.py ├── to_do_list.py ├── validate_input.py ├── verification.py └── word_count.py ├── README.md ├── jvm-final ├── Starter.java ├── classloader │ ├── ClassLoader.java │ ├── Classpath.java │ ├── classfileparser │ │ ├── BuildUtil.java │ │ ├── ClassFile.java │ │ ├── FieldInfo.java │ │ ├── MethodInfo.java │ │ ├── attribute │ │ │ ├── AttributeBuilder.java │ │ │ ├── AttributeInfo.java │ │ │ ├── BootstrapMethodInfo.java │ │ │ ├── BootstrapMethodsAttribute.java │ │ │ ├── CodeAttribute.java │ │ │ ├── ConstantValueAttr.java │ │ │ ├── ExceptionTable.java │ │ │ ├── ExceptionsAttribute.java │ │ │ └── smta │ │ │ │ ├── AppendFrame.java │ │ │ │ ├── ChopFrame.java │ │ │ │ ├── FullFrame.java │ │ │ │ ├── ObjectVariableInfo.java │ │ │ │ ├── SameFramExtended.java │ │ │ │ ├── SameFrame.java │ │ │ │ ├── SameLocals1StackItemFramExtended.java │ │ │ │ ├── SameLocals1StackItemFrame.java │ │ │ │ ├── StackMapFrame.java │ │ │ │ ├── StackMapTableAttribute.java │ │ │ │ ├── UninitializedVariableInfo.java │ │ │ │ └── VerificationTypeInfo.java │ │ └── constantpool │ │ │ ├── ConstantPool.java │ │ │ └── info │ │ │ ├── ClassInfo.java │ │ │ ├── ConstantPoolInfo.java │ │ │ ├── DoubleInfo.java │ │ │ ├── FieldrefInfo.java │ │ │ ├── FloatInfo.java │ │ │ ├── IntegerInfo.java │ │ │ ├── InterfaceMethodrefInfo.java │ │ │ ├── InvokeDynamicInfo.java │ │ │ ├── LongInfo.java │ │ │ ├── MemberRefInfo.java │ │ │ ├── MethodHandleInfo.java │ │ │ ├── MethodTypeInfo.java │ │ │ ├── MethodrefInfo.java │ │ │ ├── NameAndTypeInfo.java │ │ │ ├── StringInfo.java │ │ │ └── UTF8Info.java │ └── classreader │ │ ├── ArchivedEntry.java │ │ ├── CompositeEntry.java │ │ ├── DirEntry.java │ │ ├── Entry.java │ │ └── WildEntry.java ├── execution │ ├── Interpreter.java │ └── Opcode.java ├── instructions │ ├── README │ ├── base │ │ ├── BranchInstruction.java │ │ ├── ByteReader.java │ │ ├── Index16Instruction.java │ │ ├── Index8Instruction.java │ │ ├── Instruction.java │ │ └── NoOperandsInstruction.java │ ├── comparisons │ │ ├── DCMPG.java │ │ ├── DCMPL.java │ │ ├── FCMPG.java │ │ ├── FCMPL.java │ │ ├── IFACMP │ │ │ ├── IF_ACMPEQ.java │ │ │ └── IF_ACMPNE.java │ │ ├── IFCOND │ │ │ ├── IFEQ.java │ │ │ ├── IFGE.java │ │ │ ├── IFGT.java │ │ │ ├── IFLE.java │ │ │ ├── IFLT.java │ │ │ └── IFNE.java │ │ ├── IFICMP │ │ │ ├── IF_ICMPEQ.java │ │ │ ├── IF_ICMPGE.java │ │ │ ├── IF_ICMPGT.java │ │ │ ├── IF_ICMPLE.java │ │ │ ├── IF_ICMPLT.java │ │ │ └── IF_ICMPNE.java │ │ └── LCMP.java │ ├── constants │ │ ├── ACONST_NULL.java │ │ ├── BIPUSH.java │ │ ├── DCONST_N │ │ │ ├── DCONST_0.java │ │ │ └── DCONST_1.java │ │ ├── FCONST_N │ │ │ ├── FCONST_0.java │ │ │ ├── FCONST_1.java │ │ │ └── FCONST_2.java │ │ ├── ICONST_N │ │ │ ├── ICONST_M1.java │ │ │ └── ICONST_N.java │ │ ├── LCONST_N │ │ │ ├── LCONST_0.java │ │ │ └── LCONST_1.java │ │ ├── LDC.java │ │ ├── LDC2_W.java │ │ ├── LDC_W.java │ │ ├── NOP.java │ │ └── SIPUSH.java │ ├── control │ │ ├── ARETURN.java │ │ ├── DRETURN.java │ │ ├── FRETURN.java │ │ ├── GOTO.java │ │ ├── IRETURN.java │ │ ├── LOOKUP_SWITCH.java │ │ ├── LRETURN.java │ │ ├── RETURN.java │ │ └── TABLE_SWITCH.java │ ├── conversions │ │ ├── D2F.java │ │ ├── D2I.java │ │ ├── D2L.java │ │ ├── F2D.java │ │ ├── F2I.java │ │ ├── F2L.java │ │ ├── I2B.java │ │ ├── I2C.java │ │ ├── I2D.java │ │ ├── I2F.java │ │ ├── I2L.java │ │ ├── I2S.java │ │ ├── L2D.java │ │ ├── L2F.java │ │ └── L2I.java │ ├── extended │ │ ├── IFNONNULL.java │ │ ├── IFNULL.java │ │ └── WIDE.java │ ├── loads │ │ ├── ALOAD.java │ │ ├── ALOAD_N.java │ │ ├── ArrayLoad │ │ │ ├── AALOAD.java │ │ │ ├── BALOAD.java │ │ │ ├── CALOAD.java │ │ │ ├── DALOAD.java │ │ │ ├── FALOAD.java │ │ │ ├── IALOAD.java │ │ │ ├── LALOAD.java │ │ │ └── SALOAD.java │ │ ├── DLOAD.java │ │ ├── DLOAD_N.java │ │ ├── FLOAD.java │ │ ├── FLOAD_N.java │ │ ├── ILOAD.java │ │ └── ILOAD_N.java │ ├── math │ │ ├── ADD │ │ │ ├── DADD.java │ │ │ ├── FADD.java │ │ │ ├── IADD.java │ │ │ └── LADD.java │ │ ├── AND │ │ │ ├── IAND.java │ │ │ └── LAND.java │ │ └── DIV │ │ │ ├── DDIV.java │ │ │ ├── FDIV.java │ │ │ └── IDIV.java │ ├── references │ │ ├── ANEWARRAY.java │ │ ├── ARRAY_LENGTH.java │ │ ├── ATYPE.java │ │ ├── CHECKCAST.java │ │ └── GET_FIELD.java │ ├── stack │ │ ├── DUP.java │ │ └── DUP2.java │ └── stores │ │ ├── ASTORE.java │ │ ├── ASTORE_N.java │ │ ├── ArrayStores │ │ ├── AASTORE.java │ │ ├── BASTORE.java │ │ ├── CASTORE.java │ │ ├── DASTORE.java │ │ ├── FASTORE.java │ │ ├── IASTORE.java │ │ ├── LASTORE.java │ │ └── SASTORE.java │ │ ├── DSTORE.java │ │ ├── DSTORE_N.java │ │ ├── FSTORE.java │ │ ├── FSTORE_N.java │ │ ├── ISTORE.java │ │ ├── ISTORE_N.java │ │ ├── LSTORE.java │ │ ├── LSTORE_N.java │ │ └── STORE_N.java ├── memory │ ├── heap │ │ ├── ArrayObject.java │ │ ├── MyObject.java │ │ ├── NonArrayObject.java │ │ ├── NullObject.java │ │ └── Vars.java │ └── jclass │ │ ├── AccessFlags.java │ │ ├── ClassMember.java │ │ ├── Field.java │ │ ├── JClass.java │ │ ├── Method.java │ │ └── runtimeConstantPool │ │ ├── RuntimeConstantPool.java │ │ └── constant │ │ ├── Constant.java │ │ ├── other │ │ ├── InvokeDynamic.java │ │ ├── MethodHandle.java │ │ ├── MethodType.java │ │ ├── NameAndType.java │ │ └── UTF8.java │ │ ├── ref │ │ ├── ClassRef.java │ │ ├── FieldRef.java │ │ ├── InterfaceMethodRef.java │ │ ├── MemberRef.java │ │ ├── MethodRef.java │ │ └── SymRef.java │ │ └── wrapper │ │ ├── DoubleWrapper.java │ │ ├── FloatWrapper.java │ │ ├── IntWrapper.java │ │ ├── LongWrapper.java │ │ └── StringWrapper.java ├── readme ├── runtime │ ├── LocalVars.java │ ├── MyFrame.java │ ├── MyThread.java │ ├── OperandStack.java │ ├── Slot.java │ └── ThreadStack.java └── util │ ├── ColorUtil.java │ └── IOUtil.java └── lambda.py /JVM-final: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /JavaCode/20-good-map/GoodMap.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /JavaCode/20-good-map/README.md: -------------------------------------------------------------------------------- 1 | 现有n*n大小的地图,甲乙双方降落在地图上的某个点,且甲乙双方能运动。 2 | 3 | 现给定初始位置,**运动状态**和运动方向,求最终甲乙之间的距离,**判断二者是否相遇**,并打印最终的地图。 4 | 运动状态:每位player都有一个状态为isHide, 表示是否隐身 5 | 运动方向如下: 6 | 7 | L:左移一位 8 | R:右移一位 9 | D:下移一位 10 | U:上移一位 11 | 12 | 说明:现有Game类,Map类,Player类和Result类。 13 | Game由一个Map对象和两个Player对象组成,我们在Game类中对Map和Player进行了初始化,确定了Map的大小和Player的初始位置及状态。 14 | 15 | Game类的playGame方法,根据输入的运动方向字符串,循环地让甲、乙移动;每次移动后都计算甲乙之间距离并返回。 16 | 当移动结束后,让Map对象打印地图信息。 17 | 甲、乙在移动后的最终位置可能重合(即二者相遇),若重合,打印地图信息时只需打印甲的状态 18 | 19 | 要求:要实现的是Map类中的resultEvaluation()、 print()方法和Player类中calDistance(Player player)、move(char move)、getSymbol()方法。 20 | 21 | Game类输入格式:n为地图大小,x、y为初始位置横纵坐标,isHide表示是否隐身(隐身用小写字母表示),以空格隔开,后面一行为移动方向序列。 22 | 先输入甲的信息,再输入乙的信息。 23 | 24 | n;x1,y1,isHideX;x2,y2,isHideY 25 | mx1,my1,mx2,my2,mx3,my3 26 | 27 | 其中,x、y方向如下所示: 28 | —— ——> y方向 29 | | 30 | | 31 | V 32 | x方向 33 | 34 | 示例: 5;0,0,false;4,4,true 35 | R,U,R,L,D,U 36 | 37 | 打印地图格式: 38 | 39 | 地图中的每一行成一行 40 | 甲用X表示,乙用Y表示 41 | 其他地区用定义好的MAP_DEFAULT表示(-),每个字符中间用空格隔开 42 | example: 43 | - - - - - 44 | - - X - - 45 | - - - y - 46 | - - - - - 47 | - - - - - 48 | -------------------------------------------------------------------------------- /JavaCode/20-good-map/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | edu.nju 8 | GoodMap 9 | 1.0.0 10 | GoodMap 11 | 12 | 13 | UTF-8 14 | 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | 22 | 23 | 24 | 25 | 26 | 27 | org.apache.maven.plugins 28 | maven-surefire-report-plugin 29 | 2.21.0 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 40 | 1.8 41 | 1.8 42 | ${file_encoding} 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /JavaCode/20-good-map/src/main/java/Result.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by lujxu on 2017/11/6. 3 | */ 4 | public enum Result { 5 | /** 6 | * ENCOUNTER:代表二者相遇 7 | * DRAW:代表二者始终未相遇,平局 8 | */ 9 | ENCOUNTER,DRAW 10 | } 11 | -------------------------------------------------------------------------------- /JavaCode/20-good-map/target/classes/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/20-good-map/target/classes/Game.class -------------------------------------------------------------------------------- /JavaCode/20-good-map/target/classes/Map.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/20-good-map/target/classes/Map.class -------------------------------------------------------------------------------- /JavaCode/20-good-map/target/classes/Player.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/20-good-map/target/classes/Player.class -------------------------------------------------------------------------------- /JavaCode/20-good-map/target/classes/Result.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/20-good-map/target/classes/Result.class -------------------------------------------------------------------------------- /JavaCode/20-good-map/target/test-classes/GameTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/20-good-map/target/test-classes/GameTest.class -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/252-commandlineutil/README.pdf -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/commandLineUtil.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/figure/Flow_Chart.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/252-commandlineutil/figure/Flow_Chart.jpg -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | edu.nju 8 | commandLineUtil 9 | 1.0 10 | 11 | 12 | commons-cli 13 | commons-cli 14 | 1.4 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | test 22 | 23 | 24 | 25 | com.github.stefanbirkner 26 | system-rules 27 | 1.16.0 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/target/classes/edu/nju/CommandLineUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/252-commandlineutil/target/classes/edu/nju/CommandLineUtil.class -------------------------------------------------------------------------------- /JavaCode/252-commandlineutil/target/test-classes/edu/nju/CommandLineUtilTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/252-commandlineutil/target/test-classes/edu/nju/CommandLineUtilTest.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/main/java/edu/nju/CompositeEntry.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | import java.io.IOException; 4 | 5 | /** 6 | * format : dir/subdir;dir/subdir/*;dir/target.jar* 7 | */ 8 | public class CompositeEntry extends Entry{ 9 | public CompositeEntry(String classpath) { 10 | super(classpath); 11 | } 12 | 13 | @Override 14 | public byte[] readClassFile(String className) throws IOException { 15 | String[] classPaths = classpath.split(PATH_SEPARATOR); 16 | byte[] res = null; 17 | for (String classPath : classPaths) { 18 | if (classPath.contains("JAR") || classPath.contains("jar")) { 19 | ArchivedEntry arch = new ArchivedEntry(classPath); 20 | res = arch.readClassFile(className); 21 | } else if (classPath.contains("*")) { 22 | WildEntry wild = new WildEntry(classPath); 23 | res = wild.readClassFile(className); 24 | } else { 25 | DirEntry dir = new DirEntry(classPath); 26 | res = dir.readClassFile(className); 27 | } 28 | if (res != null) return res; 29 | } 30 | return res; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/main/java/edu/nju/DirEntry.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | 8 | /** 9 | * format : dir/subdir/.../ 10 | */ 11 | public class DirEntry extends Entry{ 12 | public DirEntry(String classpath) { 13 | super(classpath); 14 | } 15 | 16 | @Override 17 | public byte[] readClassFile(String className) throws IOException { 18 | File test = new File(classpath + FILE_SEPARATOR + className); 19 | if ( test.exists() ){ 20 | InputStream inputstream = new FileInputStream(classpath + FILE_SEPARATOR + className); 21 | return IOUtil.readFileByBytes(inputstream); 22 | } else { 23 | return null; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/main/java/edu/nju/Entry.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public abstract class Entry { 7 | public final String PATH_SEPARATOR = File.pathSeparator; 8 | public final String FILE_SEPARATOR = File.separator; 9 | public String classpath; 10 | 11 | public Entry(String classpath){ 12 | this.classpath = classpath; 13 | } 14 | 15 | public abstract byte[] readClassFile(String className) throws IOException; 16 | } 17 | -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/main/java/edu/nju/IOUtil.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | import java.io.*; 4 | 5 | public class IOUtil { 6 | public static byte[] readFileByBytes(InputStream is) throws IOException { 7 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { 8 | BufferedInputStream in = null; 9 | in = new BufferedInputStream(is); 10 | int buf_size = 1024; 11 | byte[] buffer = new byte[buf_size]; 12 | int len = 0; 13 | while (-1 != (len = in.read(buffer, 0, buf_size))) { 14 | bos.write(buffer, 0, len); 15 | } 16 | return bos.toByteArray(); 17 | } catch (IOException e) { 18 | e.printStackTrace(); 19 | throw e; 20 | } 21 | } 22 | 23 | public static String transform(String pathName) { 24 | if (pathName.contains("/")) { 25 | return pathName.replace("/", File.separator); 26 | } 27 | return pathName; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/main/java/edu/nju/WildEntry.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.util.Arrays; 6 | 7 | /** 8 | * format : dir/.../* 9 | */ 10 | public class WildEntry extends Entry{ 11 | public WildEntry(String classpath) { 12 | super(classpath); 13 | } 14 | 15 | @Override 16 | public byte[] readClassFile(String className) throws IOException { 17 | File file = new File(classpath.replace(FILE_SEPARATOR+"*", "")); 18 | String[] files = file.list(); 19 | //System.out.println(classpath.replace(FILE_SEPARATOR+"*", "")); 20 | if( files != null ){ 21 | //System.out.println(stringConstructor(files, classpath, PATH_SEPARATOR)); 22 | CompositeEntry comp = new CompositeEntry(stringConstructor(files, classpath, PATH_SEPARATOR)); 23 | return comp.readClassFile(className); 24 | } 25 | return null; 26 | } 27 | 28 | public static String stringConstructor(String[] files, String classpath, String PATH_SEPERATOR){ 29 | StringBuilder str = new StringBuilder(); 30 | for( String file : files){ 31 | str.append(classpath.replace("*", file)); 32 | str.append(PATH_SEPERATOR); 33 | } 34 | return str.toString(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/test/libs/parser.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/src/test/libs/parser.jar -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/Object.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/Object.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/java/lang/Object.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/java/lang/Object.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/subdir/empty.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/subdir/empty.jar -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/subdir/rt.JAR: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/src/test/testfilepath/dir/subdir/rt.JAR -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/ArchivedEntry.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/ArchivedEntry.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/ClassFileReader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/ClassFileReader.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/CompositeEntry.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/CompositeEntry.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/DirEntry.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/DirEntry.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/Entry.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/Entry.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/IOUtil.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/IOUtil.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/WildEntry.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/classes/edu/nju/WildEntry.class -------------------------------------------------------------------------------- /JavaCode/259-jvvm-02-classfilereader-new3/target/test-classes/edu/nju/ClassFileReaderTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/259-jvvm-02-classfilereader-new3/target/test-classes/edu/nju/ClassFileReaderTest.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/GoodMapGame.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/README.md: -------------------------------------------------------------------------------- 1 | 在 GoodMap 的基础上,我们期望能根据它实现一个游戏,规则如下: 2 | 3 | 1. 游戏中有两个玩家,分别用 X,Y 表示 4 | 2. 游戏地图为正方形,初始时,会向系统输入地图的大小,玩家的初始位置和血量 5 | 3. 每个玩家都有一个武器,也就是 Gun,它的伤害值是 1,射程是 3,这个武器的伤害值和射程在整个游戏过程中保持不变 6 | 4. 一个玩家可以攻击另一个玩家,假如 X 攻击 Y,如果 Y 到 X 的距离小于等于枪的射程(3),那么 Y 就受到 1 点伤害,生命值减 1; 7 | 如果 Y 到 X 的距离大于3,那么 Y 不受到伤害 8 | 5. 游戏为回合制,X 和 Y 轮流行动,X 先行动。 9 | 每个回合,玩家需要向上下左右任意一个方向移动,选择是否隐身,和是否攻击对方,每个玩家的动作用一个长度为3的字符串表示, 10 | 每个字符依次表示 '方向','是否攻击','是否隐身',如: 11 | U01 12 | 表示当前玩家向上走一步,不攻击对方,隐身; 13 | 两个玩家的动作用','分隔,比如,一个完整的行动序列为 14 | U01,L10 15 | 表示 X 向上走 1 步,不攻击,隐身; Y 向左走 1 步,攻击,不隐身 16 | 6. 在游戏初始时,需要打印游戏的初始状态,即地图和玩家在地图上的位置,以及两个玩家的初始血量; 17 | 在任意一个玩家的回合之后,同样要打印地图和玩家在地图上的位置,以及两个玩家的当前血量 18 | 例如,在输入为 19 | Game game=new Game("3;0,0,1;0,1,1"); //地图大小,玩家的初始位置和初始血量 20 | Result result=game.playGame("D11,L11,R11,D11"); //玩家行动的序列 21 | 打印的结果应该为 22 | 23 | 0 1 2 24 | 0 X Y - 25 | 1 - - - 26 | 2 - - - 27 | X : 1 28 | Y : 1 29 | 0 1 2 30 | 0 - Y - 31 | 1 x - - 32 | 2 - - - 33 | X : 1 34 | Y : 0 35 | 36 | 注意玩家在当前回合若隐身,需要用小写字母表示其在地图上的位置 37 | 打印玩家血量则一律用大写字母表示 38 | 7. 在游戏过程中,需要判断游戏结果,游戏结果有3种 39 | 1) 如果走完所有的回合,双方血量都大于 0 ,那么没有玩家死亡,返回 DRAW 40 | 2) 如果 X 在某回合死亡,则 Y 胜利,返回 Y_WIN 41 | 3) 如果 Y 在某回合死亡,则 X 胜利,返回 X_WIN 42 | 注意游戏可能在中途结束,比如,输入共有 10 个回合,但玩家 Y 在第 5 个回合就死了,那么在第 5 个回合返回 X_WIN,之后的回合就不用继续了 -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.nju.software 6 | GoodMapGame 7 | 1.0.0 8 | GoodMapGame 9 | 10 | 11 | UTF-8 12 | 13 | 14 | 15 | 16 | junit 17 | junit 18 | 4.12 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-surefire-report-plugin 27 | 2.21.0 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.apache.maven.plugins 36 | maven-compiler-plugin 37 | 3.6.0 38 | 39 | 1.8 40 | 1.8 41 | UTF-8 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/src/main/java/Gun.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Shifang on 2017/10/21. 3 | * 武器类,请不要改动 4 | */ 5 | public class Gun { 6 | 7 | 8 | private final int damage = 1; //伤害值,请不要修改 9 | private final int range = 3; //射程,请不要修改 10 | 11 | public int getDamage() { //修改 12 | return damage; 13 | } 14 | 15 | public int getRange() { //修改 16 | return range; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/src/main/java/Result.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by Shifang on 2017/10/21. 3 | * 游戏结果,请不要改动 4 | */ 5 | public enum Result { 6 | /** 7 | * X_WIN:代表 X 胜利 8 | * Y_WIN: 代表 Y 胜利 9 | * DRAW: 代表平局 10 | */ 11 | X_WIN, Y_WIN,DRAW 12 | } 13 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/classes/Game.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/classes/Game.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/classes/Gun.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/classes/Gun.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/classes/Map.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/classes/Map.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/classes/Player.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/classes/Player.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/classes/Result.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/classes/Result.class -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | Game.class 2 | Map.class 3 | Result.class 4 | Gun.class 5 | Player.class 6 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\main\java\Game.java 2 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\main\java\Gun.java 3 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\main\java\Result.java 4 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\main\java\Map.java 5 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\main\java\Player.java 6 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | GameTest.class 2 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | C:\Users\669\Desktop\1fb4942e58f046e3a6b0f2d845f09c6a\40-good-map-game\src\test\java\GameTest.java 2 | -------------------------------------------------------------------------------- /JavaCode/40-good-map-game/target/test-classes/GameTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wyh2023/Software-Eng-Computing-I/234fd658425b79f3df3b88cb0bc0032befd89109/JavaCode/40-good-map-game/target/test-classes/GameTest.class -------------------------------------------------------------------------------- /JavaCode/AddDigits.java: -------------------------------------------------------------------------------- 1 | 2 | public class AddDigits { 3 | public static void main(String[] args){ 4 | System.out.println(addDigits(38)); 5 | } 6 | 7 | public static int addDigits(int num) { 8 | int ans = num; 9 | while (num > 10){ 10 | ans = addResult(num); 11 | num = ans; 12 | } 13 | return ans; 14 | } 15 | 16 | public static int addResult(int num){ 17 | int res = 0; 18 | while (num > 0){ 19 | res += num % 10; 20 | num /= 10; 21 | } 22 | return res; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JavaCode/Arithmetic.java: -------------------------------------------------------------------------------- 1 | 2 | public class Arithmetic { 3 | 4 | public static String arithmetic(String s) { 5 | if(s == null){ 6 | return "wrong"; 7 | } 8 | StringBuilder str = new StringBuilder(); 9 | String[] arith = s.split(" "); 10 | str.append(arith[0]); 11 | str.append(arith[2]); 12 | str.append(arith[1]); 13 | str.append("="); 14 | if ("+".equals(arith[2])) { 15 | str.append(Long.parseLong(arith[0]) + Long.parseLong(arith[1])); 16 | }else if("-".equals(arith[2])) { 17 | str.append(Integer.parseInt(arith[0]) - Integer.parseInt(arith[1])); 18 | }else if("/".equals(arith[2]) && !arith[1].equals("0")) { 19 | str.append(Integer.parseInt(arith[0]) / Integer.parseInt(arith[1])); 20 | }else if("*".equals(arith[2])) { 21 | str.append(Integer.parseInt(arith[0]) * Integer.parseInt(arith[1])); 22 | }else { 23 | return "wrong"; 24 | } 25 | return str.toString() ; 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /JavaCode/ArrayToInteger.java: -------------------------------------------------------------------------------- 1 | 2 | public class ArrayToInteger { 3 | 4 | public static int getInteger(int[] numbers) { 5 | int ans = 0; 6 | for(int i=0; i=0; i--){ 11 | if(bin[i] == '1'){ 12 | ans += (int)Math.pow(2, bin.length-i-1); 13 | } 14 | } 15 | return Integer.toString(ans); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /JavaCode/BinarySearch.java: -------------------------------------------------------------------------------- 1 | 2 | public class BinarySearch { 3 | 4 | public static int search(int[] data, int target) { 5 | int len = data.length; 6 | int start = 0; 7 | int end = len-1; 8 | int mid; 9 | while (start <= end){ 10 | mid = (start + end) / 2; 11 | if(target < data[mid]){ 12 | end = mid-1; 13 | }else if(target > data[mid]){ 14 | start = mid+1; 15 | }else{ 16 | return mid; 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /JavaCode/CSVFile.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.File; 3 | import java.io.FileReader; 4 | import java.io.IOException; 5 | import java.nio.file.Paths; 6 | import java.util.Scanner; 7 | 8 | public class CSVFile { 9 | 10 | public static void main(String[] args) { 11 | String filePath = CSVFile.class.getClassLoader().getResource("data.txt").getPath(); 12 | printCSVFile(filePath); 13 | } 14 | 15 | public static void printCSVFile(String filePath) { 16 | File file = new File(filePath); 17 | BufferedReader reader = null; 18 | try { 19 | System.out.println("Last Fisrt Salary"); 20 | reader = new BufferedReader(new FileReader(file)); 21 | String tempString = null; 22 | while ((tempString = reader.readLine()) != null) { 23 | System.out.println(tempString.replace(",", " ")); 24 | } 25 | reader.close(); 26 | } catch (IOException e) { 27 | e.printStackTrace(); 28 | } finally { 29 | if (reader != null) { 30 | try { 31 | reader.close(); 32 | } catch (IOException ignored) { 33 | } 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /JavaCode/CheckOddEven.java: -------------------------------------------------------------------------------- 1 | 2 | public class CheckOddEven { 3 | 4 | public static boolean isOdd(int num){ 5 | if(num % 2 != 0){ 6 | return true; 7 | } 8 | return false; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaCode/CircleComputation.java: -------------------------------------------------------------------------------- 1 | /*Exercise CircleComputation (User Input): Write a program called CircleComputation, 2 | * which prompts user for a radius (of double) and compute the area and perimeter 3 | * of a circle. 4 | Hints: π is kept in a constant called Math.PI. 5 | */ 6 | 7 | public class CircleComputation{ 8 | 9 | /** 10 | * 计算圆面积 11 | * @param radius 12 | * @return 面积大小 13 | */ 14 | public double area(double radius){ 15 | return radius*radius*Math.PI; 16 | } 17 | 18 | /** 19 | * 计算圆周长 20 | * @param radius 21 | * @return 圆周长 22 | */ 23 | public double perimeter(double radius){ 24 | return 2*radius*Math.PI; 25 | } 26 | } -------------------------------------------------------------------------------- /JavaCode/ClimbStairs.java: -------------------------------------------------------------------------------- 1 | 2 | public class ClimbStairs { 3 | 4 | public static int climbStairs(int n) { 5 | if(n == 1) return 1; 6 | else if(n == 0) return 0; 7 | else if(n == 2) return 2; 8 | else return climbStairs(n - 1) + climbStairs(n - 2); 9 | } 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /JavaCode/ComputePI.java: -------------------------------------------------------------------------------- 1 | //see more details on http://www.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html 2 | //Write a program called ComputePI to compute the value of π, using the following series expansion. 3 | //You have to decide on the termination criterion used in the computation (such as the number of terms used or the magnitude of an additional term). 4 | //Is this series suitable for computing π? 5 | public class ComputePI { 6 | 7 | public static double compute(int maxDenominator){ 8 | double each = 1.0; 9 | double pi = 0; 10 | int flg = -1; 11 | for(int i = 1; i <= maxDenominator; i = i + 2){ 12 | flg *= -1; 13 | pi += (1.0 / i)*flg; 14 | } 15 | return 4*pi; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /JavaCode/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class ContainsDuplicate { 4 | 5 | public static boolean containsDuplicate(int[] nums) { 6 | for(int i = 0; i < nums.length-1; i++){ 7 | for(int j = i+1; j=0; i--){ 12 | ans += charlst[i]; 13 | } 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaCode/Fibonacci.java: -------------------------------------------------------------------------------- 1 | 2 | public class Fibonacci { 3 | public int FibonacciN(int input){ 4 | int first = 0; 5 | int second = 1; 6 | int n = input; 7 | while(n > 0){ 8 | int tmp = first; 9 | first = second; 10 | second = second + tmp; 11 | n--; 12 | } 13 | return first; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /JavaCode/GradesHistogram.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.nio.file.Paths; 3 | import java.util.Scanner; 4 | 5 | public class GradesHistogram { 6 | /** 7 | * 编写该方法对文件进行处理,并打印直方图,可添加新的方法 8 | * @param fileName 处理的文件名 9 | */ 10 | public static void histogram(String fileName) throws IOException { 11 | Scanner in = new Scanner(Paths.get(fileName), "UTF-8"); 12 | int num = in.nextInt(); 13 | int[] times = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 14 | for(int i=0; i>= 1; 14 | count++; 15 | } 16 | return sum; 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /JavaCode/HappyNumber.java: -------------------------------------------------------------------------------- 1 | 2 | public class HappyNumber { 3 | 4 | public static void main(String[] args){ 5 | System.out.println(isHappy(19)); 6 | } 7 | 8 | public static boolean isHappy(int n) { 9 | int count = 0; 10 | while (count < 1000){ 11 | count++; 12 | if(n == 1) return true; 13 | int tmp = n; 14 | int sum = 0; 15 | while (tmp > 0){ 16 | sum += (tmp % 10) * (tmp % 10); 17 | tmp /= 10; 18 | } 19 | n = sum; 20 | } 21 | return false; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /JavaCode/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package edu.nju; 2 | 3 | /** 4 | * Created by Moekr on 2017/10/30. 5 | */ 6 | public class HelloWorld { 7 | public String helloWorld(){ 8 | return "Hello World!"; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaCode/KthLargestElement.java: -------------------------------------------------------------------------------- 1 | 2 | public class KthLargestElement { 3 | 4 | public static int findKthLargest(int[] nums, int k) { 5 | for(int j = 0; j < nums.length-1; j++){ 6 | for(int i = 0; i < nums.length-1; i++){ 7 | if(nums[i]0? d:-d; 11 | } 12 | 13 | 14 | 15 | 16 | 17 | } -------------------------------------------------------------------------------- /JavaCode/NumberComplement.java: -------------------------------------------------------------------------------- 1 | 2 | public class NumberComplement { 3 | public static void main(String[] args){ 4 | System.out.println(findComplement(2)); 5 | } 6 | 7 | public static int findComplement(int num) { 8 | if(num < 0) return ~num; 9 | else if (num == 0) return -1; 10 | else{ 11 | int sum = 0; 12 | int flg = 1; 13 | while (num > 0){ 14 | if((num & 1) == 0){ 15 | sum += flg; 16 | } 17 | flg <<= 1; 18 | num >>= 1; 19 | }; 20 | return sum; 21 | } 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /JavaCode/NumberConversion.java: -------------------------------------------------------------------------------- 1 | public class NumberConversion { 2 | public static String toRadix(String in, int inRadix, int outRadix) { 3 | assert inRadix == 2 || inRadix == 8 || inRadix == 10 || inRadix == 16; 4 | assert outRadix == 2 || outRadix == 8 || outRadix == 10 || outRadix == 16; 5 | //TODO 6 | 7 | int param = Integer.parseInt(in, inRadix); 8 | switch (outRadix){ 9 | case 2: return Integer.toBinaryString(param); 10 | case 8: return Integer.toOctalString(param); 11 | case 10: return Integer.toString(param); 12 | case 16: return Integer.toHexString(param).toUpperCase(); 13 | } 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaCode/NumberOfOneBits.java: -------------------------------------------------------------------------------- 1 | 2 | public class NumberOfOneBits { 3 | public static int hammingWeight(int n) { 4 | String Bin = Integer.toBinaryString(n); 5 | char[] Binchar = Bin.toCharArray(); 6 | int num = 0; 7 | for( char x : Binchar){ 8 | if(x == '1') num++; 9 | } 10 | return num; 11 | } 12 | } -------------------------------------------------------------------------------- /JavaCode/OrderString.java: -------------------------------------------------------------------------------- 1 | package nju.edu.software.Exercise048_OrderString; 2 | 3 | public class OrderString { 4 | public String order(String str){ 5 | if(str.length()==0) return ""; 6 | char[] chars = str.toCharArray(); 7 | String copy = str; 8 | copy = copy.toLowerCase(); 9 | char[] chars1 = copy.toCharArray(); 10 | for(int i=0; ichars1[j+1]){ 13 | char tmp = chars[j]; 14 | char tmp1 = chars1[j]; 15 | chars[j] = chars[j+1]; 16 | chars[j+1] = tmp; 17 | chars1[j] = chars1[j+1]; 18 | chars1[j+1] = tmp1; 19 | } 20 | } 21 | } 22 | return String.valueOf(chars).replace(" ", ""); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JavaCode/PerfectAndDeficientNumbers.java: -------------------------------------------------------------------------------- 1 | public class PerfectAndDeficientNumbers { 2 | public static boolean isPerfect(int posInt) { 3 | assert posInt > 0; 4 | int sum = 0; 5 | for(int i = 1; i 0; 15 | int sum = 0; 16 | for(int i = 1; i 1){ 9 | n /= 2; 10 | if(n == 1) return true; 11 | } 12 | return false; 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /JavaCode/PrimeFactors.java: -------------------------------------------------------------------------------- 1 | public class PrimeFactors { 2 | public static boolean isProductOfPrimeFactors(int posInt) { 3 | assert posInt > 0; 4 | int check = 1; 5 | int ceiling = posInt; 6 | for(int i = 2; i < ceiling; i++){ 7 | if(posInt % i == 0){ 8 | check = (check%i==0)? check : check*i; 9 | posInt /= i; 10 | i--; 11 | } 12 | } 13 | /* 14 | System.out.print("check: "); 15 | System.out.println(check); 16 | System.out.print("ceiling"); 17 | System.out.println(ceiling); 18 | System.out.print("PosInt: "); 19 | System.out.println(posInt); 20 | */ 21 | return check == ceiling; 22 | } 23 | 24 | public static void main(String[] args){ 25 | System.out.println(isProductOfPrimeFactors(0)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JavaCode/PrintNumberInWord.java: -------------------------------------------------------------------------------- 1 | 2 | public class PrintNumberInWord { 3 | public String Number2String(int input){ 4 | return Integer.toString(input); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /JavaCode/ReOrderArray.java: -------------------------------------------------------------------------------- 1 | 2 | public class ReOrderArray { 3 | 4 | /** 5 | * 将所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分。 6 | * 需要保证奇数和奇数,偶数和偶数之间的相对位置不变。 7 | * @param array 8 | * @return 9 | */ 10 | public int[] reOrder(int[] array){ 11 | for(int j = 0; j < array.length-1; j++){ 12 | for(int i = 0; i < array.length-1; i++){ 13 | if(array[i+1]%2==1 && array[i]%2==0){ 14 | int tmp = array[i+1]; 15 | array[i+1] = array[i]; 16 | array[i] = tmp; 17 | } 18 | } 19 | } 20 | return array; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /JavaCode/RepeatedSubstringPattern.java: -------------------------------------------------------------------------------- 1 | 2 | public class RepeatedSubstringPattern { 3 | public static void main(String[] args){ 4 | System.out.println(repeatedSubstringPattern("abcd")); 5 | } 6 | 7 | public static boolean repeatedSubstringPattern(String str) { 8 | int n = 0; 9 | for(int i=1; i 0){ 6 | flg = 1; 7 | }else if(n < 0){ 8 | flg = -1; 9 | }else{ 10 | flg = 0; 11 | } 12 | n = Math.abs(n); 13 | int ans = 0; 14 | while(n > 0){ 15 | ans *= 10; 16 | ans += n % 10; 17 | n /= 10; 18 | } 19 | return ans*flg; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /JavaCode/ReverseString.java: -------------------------------------------------------------------------------- 1 | /* 2 | Exercise ReverseString: Write a program called ReverseString, which prompts user for 3 | a String, and prints the reverse of the String. 4 | 5 | For a String called inStr, you can use inStr.length() to get the length of the 6 | String; and inStr.charAt(index) to retrieve the char at the index position, 7 | where index begins with 0. 8 | 9 | Enter a String: abcdef 10 | Ouput:"fedcba". 11 | 12 | */ 13 | 14 | public class ReverseString { 15 | /** 16 | * 将s倒置 17 | * @param s 18 | * @return 倒置结果 19 | */ 20 | public static String reverseString(String s){ 21 | char[] string = s.toCharArray(); 22 | StringBuilder ans = new StringBuilder(); 23 | for(int i = string.length-1; i >= 0; i--){ 24 | ans.append(string[i]); 25 | } 26 | return ans.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /JavaCode/SingleCharacterI.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class SingleCharacterI { 4 | public static void main(String[] args){ 5 | System.out.println(singleNumber(new char[]{'a','b','c','a','b'})); 6 | } 7 | 8 | 9 | public static char singleNumber(char[] characters) { 10 | if(characters == null || characters.length == 0){ 11 | return '\0'; 12 | } 13 | char ans = '\0'; 14 | int flg = 0; 15 | for(int i=0; i 1 13 | taxi += four_groups 14 | # 3 + 1 -> 1 15 | three2one = min(three_groups, one_groups) 16 | taxi += three2one 17 | # 2 + 2 -> 1 18 | two2two = two_groups // 2 19 | taxi += two2two 20 | # rest 21 | rest_one = one_groups - three2one 22 | rest_two = two_groups % 2 23 | rest_three = three2one - three2one 24 | taxi += rest_three 25 | if rest_two != 0: 26 | taxi += 1 27 | if rest_one <= 2: 28 | rest_one = 0 29 | else: 30 | rest_one -= 2 31 | taxi += rest_one // 4 + (rest_one % 4 + 3) // 4 32 | print(taxi) 33 | return 34 | 35 | 36 | if __name__ == '__main__': 37 | solve() 38 | -------------------------------------------------------------------------------- /PythonCode/Xenia_and_Ringroad.py: -------------------------------------------------------------------------------- 1 | """write your code in method solve""" 2 | def solve(): 3 | m_n_lst = list(map(int, input().strip().split())) 4 | n, m = m_n_lst[0], m_n_lst[1] 5 | a_lst = list(map(int, input().strip().split())) 6 | point = 1 7 | count = 0 8 | for i in range(m): 9 | while point % n != a_lst[i]: 10 | point += 1 11 | count += 1 12 | 13 | print(count) 14 | return 15 | 16 | 17 | if __name__ == '__main__': 18 | solve() -------------------------------------------------------------------------------- /PythonCode/arrival_of_general.py: -------------------------------------------------------------------------------- 1 | """write your code in method""" 2 | 3 | 4 | def calculate(): 5 | num = int(input()) 6 | soldiers = [] 7 | for i in range(num): 8 | soldiers.append(int(input())) 9 | ans = 0 10 | tallest = max(soldiers) 11 | ans += soldiers.index(tallest) 12 | soldiers.remove(tallest) 13 | soldiers.insert(0, tallest) 14 | soldiers.reverse() 15 | lowest = min(soldiers) 16 | ans += soldiers.index(lowest) 17 | print(ans) 18 | 19 | 20 | if __name__ == '__main__': 21 | calculate() -------------------------------------------------------------------------------- /PythonCode/beautiful_year.py: -------------------------------------------------------------------------------- 1 | """write your code in method""" 2 | 3 | 4 | def get_year(): 5 | year = int(input()) 6 | while year <= 9999: 7 | year += 1 8 | unit = year % 10 9 | tens = (year % 100) // 10 10 | hundreds = (year % 1000) // 100 11 | thousands = year // 1000 12 | if unit != tens and unit != hundreds and unit != thousands and tens != hundreds and tens != thousands and hundreds != thousands: 13 | print(year) 14 | break 15 | return 16 | 17 | 18 | if __name__ == '__main__': 19 | get_year() 20 | 21 | -------------------------------------------------------------------------------- /PythonCode/bmi_judge.py: -------------------------------------------------------------------------------- 1 | import decimal 2 | from decimal import Decimal 3 | 4 | 5 | def bmi(): 6 | height = int(input("What is your height? ")) 7 | weight = int(input("What is your weight? ")) 8 | bmi = (weight / (height * height)) * 703 9 | bmi = decimal_retain(bmi, 1) 10 | print("Your BMI is {:.1f}.".format(bmi)) 11 | if bmi < 18.5: 12 | print("You are underweight.You should see your doctor.") 13 | elif bmi > 25.0: 14 | print("You are overweight.You should see your doctor.") 15 | else: 16 | print("You are within the ideal weight range.") 17 | 18 | 19 | def decimal_retain(number, ndigits=0): 20 | """ 21 | Round a number to a given precision in decimal digits (default 0 digits). 22 | The strategy is : Low values rounded up. 23 | :param number: The number needed to be rounded. 24 | :param ndigits: The number of digits. 25 | :return: The number after being rounded. 26 | """ 27 | context = decimal.getcontext() 28 | context.rounding = decimal.ROUND_UP 29 | return round(Decimal(str(number)), ndigits) 30 | 31 | 32 | if __name__=='__main__': 33 | bmi() -------------------------------------------------------------------------------- /PythonCode/characters.py: -------------------------------------------------------------------------------- 1 | """write your code in method count_characters""" 2 | 3 | 4 | def count_characters(input_str): 5 | print('{} has {} characters.'.format(input_str, len(input_str))) 6 | return 7 | 8 | -------------------------------------------------------------------------------- /PythonCode/delete_staff.py: -------------------------------------------------------------------------------- 1 | """write your code in method delete_staff""" 2 | 3 | 4 | def delete_staff(staff_list, del_staff): 5 | print("There are {} staff:".format(len(staff_list))) 6 | for i in staff_list: 7 | print(i) 8 | print() 9 | print("remove the staff: " + del_staff) 10 | print() 11 | if del_staff in staff_list: 12 | staff_list.remove(del_staff) 13 | print("There are {} staff:".format(len(staff_list))) 14 | for i in staff_list: 15 | print(i) 16 | return 17 | 18 | 19 | if __name__ == '__main__': 20 | delete_staff([], 'Chris Jones') 21 | 22 | -------------------------------------------------------------------------------- /PythonCode/filter_even_numbers.py: -------------------------------------------------------------------------------- 1 | def filter_even_numbers(numbers): 2 | index = [] 3 | ans = [] 4 | for i in range(len(numbers)): 5 | if numbers[i] % 2 == 0: 6 | index.append(i) 7 | for i in range(len(numbers)): 8 | if i in index: 9 | ans.append(numbers[i]) 10 | return ans 11 | 12 | 13 | if __name__ == '__main__': 14 | print(filter_even_numbers([3, 7])) -------------------------------------------------------------------------------- /PythonCode/football.py: -------------------------------------------------------------------------------- 1 | """write your code in method""" 2 | 3 | 4 | def is_dangerous(): 5 | line = input() 6 | one_line = line.split('0') 7 | zero_line = line.split('1') 8 | flg = 0 9 | for i in one_line: 10 | if len(i) >= 7: 11 | flg = 1 12 | for i in zero_line: 13 | if len(i) >= 7: 14 | flg = 1 15 | if flg == 1: 16 | print('YES') 17 | else: 18 | print('NO') 19 | return 20 | 21 | 22 | if __name__ == '__main__': 23 | is_dangerous() 24 | -------------------------------------------------------------------------------- /PythonCode/football_dangerous.py: -------------------------------------------------------------------------------- 1 | """write your code in method""" 2 | 3 | 4 | def is_dangerous(): 5 | line = input() 6 | one_line = line.split('0') 7 | zero_line = line.split('1') 8 | flg = 0 9 | for i in one_line: 10 | if len(i) >= 7: 11 | flg = 1 12 | for i in zero_line: 13 | if len(i) >= 7: 14 | flg = 1 15 | if flg == 1: 16 | print('YES') 17 | else: 18 | print('NO') 19 | return 20 | 21 | 22 | if __name__ == '__main__': 23 | is_dangerous() -------------------------------------------------------------------------------- /PythonCode/generate_multiplication_table.py: -------------------------------------------------------------------------------- 1 | 2 | def generate(): 3 | n = int(input('n=')) 4 | for i in range(n + 1): 5 | for j in range(n + 1): 6 | print("{} X {} = {}".format(i, j, i*j)) 7 | 8 | 9 | if __name__=='__main__': 10 | generate() 11 | -------------------------------------------------------------------------------- /PythonCode/heart_rate_cal.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def heart_rate_calculation(): 4 | RestingHR = int(input("RestingHR:")) 5 | age = int(input("Age:")) 6 | print("Intensity| Rate") 7 | print("---------|------") 8 | intensity = 0.55 9 | while intensity < 1: 10 | TargetHeartRate = ((220 - age) - RestingHR) * intensity + RestingHR 11 | print(str(int(intensity * 100)) + "% |" + str(int(TargetHeartRate)) + "bpm") 12 | intensity += 0.05 13 | -------------------------------------------------------------------------------- /PythonCode/insomnia_cure.py: -------------------------------------------------------------------------------- 1 | """write your code in method sove""" 2 | def solve(): 3 | k = int(input()) 4 | l = int(input()) 5 | m = int(input()) 6 | n = int(input()) 7 | d = int(input()) 8 | total = 0 9 | for j in range(d): 10 | i = j + 1 11 | if i % k == 0 or i % l == 0 or i % m == 0 or i % n == 0: 12 | total += 1 13 | print(total) 14 | return 15 | 16 | 17 | if __name__ == '__main__': 18 | solve() -------------------------------------------------------------------------------- /PythonCode/interest_cal.py: -------------------------------------------------------------------------------- 1 | def calculation(): 2 | principle = int(input('Enter the principal: ')) 3 | interest = float(input('Enter the rate of interest: '))/100 4 | year = int(input('Enter the number of year: ')) 5 | print('investment:{:.2f}'.format(principle*(1 + interest*year))) 6 | 7 | 8 | if __name__ == '__main__': 9 | calculation() 10 | -------------------------------------------------------------------------------- /PythonCode/isAnagram_cal.py: -------------------------------------------------------------------------------- 1 | def is_anagram(str1, str2): 2 | s1 = str1[:] 3 | s2 = str2[:] 4 | lst1 = list(s1) 5 | lst2 = list(s2) 6 | lst1.sort() 7 | lst2.sort() 8 | return lst1 == lst2 9 | 10 | 11 | if __name__ == '__main__': 12 | is_anagram('apple', 'pplea') 13 | 14 | -------------------------------------------------------------------------------- /PythonCode/magnets.py: -------------------------------------------------------------------------------- 1 | """write your code in method solve""" 2 | def solve(): 3 | num = int(input()) 4 | magnets = list(input().split('\n')) 5 | mag_num = 1 6 | attri = magnets[0] 7 | for magnet in magnets: 8 | if magnet != attri: 9 | attri = magnet 10 | mag_num += 1 11 | print(mag_num) 12 | return 13 | 14 | 15 | if __name__ == '__main__': 16 | solve() -------------------------------------------------------------------------------- /PythonCode/max_number.py: -------------------------------------------------------------------------------- 1 | """write your code in method get_max_number""" 2 | def get_max_number(): 3 | num1 = int(input("Enter the first number:")) 4 | num2 = int(input("Enter the second number:")) 5 | num3 = int(input("Enter the third number:")) 6 | numlst = [num1, num2, num3] 7 | numlst.sort() 8 | if numlst.count(num1) > 1 or numlst.count(num3) > 1: 9 | print("Have the same number:" + str(numlst[1])) 10 | else: 11 | print("The largest number is " + str(numlst[2])) 12 | return 13 | -------------------------------------------------------------------------------- /PythonCode/new_year_candles.py: -------------------------------------------------------------------------------- 1 | """write your code in method solve""" 2 | def solve(): 3 | candles = list(map(int, input().split(' '))) 4 | num = candles[0] 5 | change = candles[1] 6 | rest = num 7 | total = 0 8 | while rest > 0: 9 | rest -= 1 10 | total += 1 11 | if total % change == 0: 12 | rest += 1 13 | print(total) 14 | return 15 | 16 | if __name__ == "__main__": 17 | solve() -------------------------------------------------------------------------------- /PythonCode/number_to_month.py: -------------------------------------------------------------------------------- 1 | """write your code in method number_2_month""" 2 | 3 | 4 | def number_2_month(): 5 | Month = {1: 'january', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 6 | 9: 'September', 7 | 10: 'October', 11: 'November', 12: 'December'} 8 | num = int(input('Please enter the number of the month:')) 9 | if not num in Month: 10 | print('Error.') 11 | else: 12 | print('The name of the month is {}.'.format(Month[num])) 13 | return 14 | 15 | 16 | if __name__ == '__main__': 17 | number_2_month() -------------------------------------------------------------------------------- /PythonCode/paint_cal.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def paint_calculation(): 4 | length = int(input('What is the length in feet? ')) 5 | width = int(input('What is the width in feet? ')) 6 | area = length * width 7 | if area % 350 == 0: 8 | total = area // 350 9 | else: 10 | total = (area // 350) + 1 11 | print('You will need to purchase {} gallons of paint'.format(total)) 12 | 13 | 14 | if __name__=='__main__': 15 | paint_calculation() -------------------------------------------------------------------------------- /PythonCode/read_csv.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | def read(file): 6 | f = open(file, 'r') 7 | lines = f.read().split('\n') 8 | f.close() 9 | print('Last Fisrt Salary') 10 | for line in lines: 11 | columns = line.split(',') 12 | print(columns[0] + ' ' + columns[1] + ' ' + ("%.2f" % float(columns[2]))) 13 | -------------------------------------------------------------------------------- /PythonCode/room_area_cal.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def room_area_calculation(): 4 | length = int(input('What is the length of the room in feet?')) 5 | width = int(input('What is the length of the room in feet?')) 6 | area = length*width 7 | print("{} square feet".format(area)) 8 | print("{:.3f} square meters".format(area * 0.09290304)) 9 | 10 | 11 | if __name__ == '__main__': 12 | room_area_calculation() -------------------------------------------------------------------------------- /PythonCode/stream_process.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | def process(): 6 | 7 | 8 | if __name__ == '__main__': 9 | print(process()) -------------------------------------------------------------------------------- /PythonCode/sum_of_nums.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def sum_of_nums_calculation(): 4 | sum = 0 5 | for i in range(5): 6 | sum += int(input('Enter a number:')) 7 | print("The total is {}".format(sum)) 8 | 9 | 10 | if __name__ == '__main__': 11 | sum_of_nums_calculation() -------------------------------------------------------------------------------- /PythonCode/validate_input.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import re 4 | 5 | def validate(): 6 | first_name = input("Enter the first name:") 7 | last_name = input("JEnter the last name:") 8 | zip_code = input("Enter the ZIP code:") 9 | id = input("Enter an employee ID:") 10 | if first_name == '': 11 | print("The first name must be filled in.") 12 | elif len(first_name) < 2: 13 | print("\"" + first_name + "\" is not a valid first name. It is too short.") 14 | 15 | if last_name == '': 16 | print("The last name must be filled in.") 17 | elif len(last_name) < 2: 18 | print("\"" + last_name + "\" is not a valid last name. It is too short.") 19 | 20 | if not zip_code.isdigit(): 21 | print("The ZIP code must be numeric.") 22 | 23 | pattern = re.compile(r'[A-Z][A-Z]-\d{4}') 24 | if not re.match(pattern, id): 25 | print(id + " is not a valid ID.") 26 | 27 | if __name__=='__main__': 28 | validate() -------------------------------------------------------------------------------- /PythonCode/verification.py: -------------------------------------------------------------------------------- 1 | 2 | def password_verification(user, pwd): 3 | """ 4 | :param user: The username stored in the computer. 5 | :param pwd: The password for the user stored in the computer. 6 | """ 7 | input_user = input('What is the user name?') 8 | input_pwd = input('What is the password?') 9 | if input_pwd == pwd and input_user == user: 10 | print('Welcome!') 11 | else: 12 | print("I don't know you.") 13 | -------------------------------------------------------------------------------- /PythonCode/word_count.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import re 4 | 5 | 6 | def wordcount(filepath): 7 | file = open(filepath, 'r') 8 | lines = file.readlines() 9 | dic = {} 10 | for line in lines: 11 | wordlst = line.strip().split(' ') 12 | for word in wordlst: 13 | if not word in dic: 14 | dic[word] = 1 15 | else: 16 | tmp = dic[word] 17 | tmp += 1 18 | dic[word] = tmp 19 | 20 | dic1 = sorted(dic.items(), key=lambda kv: (-kv[1], kv[0])) 21 | dic2 = [] 22 | lenlst = [] 23 | for tup in dic1: 24 | dic2.append([tup[0], tup[1]]) 25 | lenlst.append(len(tup[0])) 26 | blank_len = max(lenlst) + 2 27 | for i in dic2: 28 | i[0] += ':' 29 | num = i[1] 30 | print(i[0] + ' '*(blank_len - len(i[0])) + '*'*num) 31 | 32 | 33 | if __name__ == '__main__': 34 | wordcount(r'C:\Users\669\Desktop\软工一\106-wordcount\files\test1') 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Software-Eng-Computing-I 2 | ## NJU SoftwareEngineering Institute 2019 3 | 4 | 这个仓库中存放的是南京大学软件学院2019级“软件工程与计算I”的作业代码。这门课的课程定位是软件工程专业的入门课,主要面向软件专业零基础的大一学生。 5 | 主要教授入门级计算理论、简单的函数式编程和Lambda演算、结构化编程、面向对象编程思想以及Java虚拟机等内容。主要使用语言有Java和Python。会有部分的Scheme,但不要求掌握编写。 6 | 目前没有上传对应代码的相关题目介绍,未来有时间会补充上。 7 | 8 | 9 | 代码主要是我本人自己码下来的,难免会有一些地方尚且不足,希望各位多多包涵。部分代码也参考了网络博客。上传的目的主要是交流学习,也许可以为学弟学妹提供参考。 10 | 11 | 12 | ### 部分作业心得——截至2020年4月23日 13 | 希望大家多多自己测试,掌握自己debug的能力。这一点非常重要。顺便给告诉大家ICS课上的两条debug准则: 14 | 15 | **机器一定是对的;** 16 | 17 | **没有测试过的代码一定是错的。** 18 | 19 | 20 | 有关Python代码,大家可以多多参考UCB的CS61A上的相关文档,在此指路:[CS61A_Debug]:https://cs61a.org/articles/debugging.html 和 [CS61A_composition]:https://cs61a.org/articles/composition.html 21 | 22 | 23 | 在Python作业中,Todolist和wordcount会有一点点难度。我再找找以前看过的文档,可以的话以后更新。 24 | 25 | 26 | Java作业的话,通读Head First Java 或者 Core Java (Java核心技术) 可以解决大部分的问题。 27 | 28 | 29 | 一定要学会自己测试。 30 | 如果有遗漏未上传的作业的话,大家可以提醒我更改或者上传。 31 | 32 | 33 | 欢迎大家积极讨论!联系邮箱:191250142@smail.nju.edu.cn 34 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/BuildUtil.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | @Getter 10 | @Setter 11 | public class BuildUtil { 12 | private ConstantPool constantPool; 13 | private ByteBuffer byteBuffer; 14 | 15 | public BuildUtil(ByteBuffer byteBuffer) { 16 | this.byteBuffer = byteBuffer; 17 | } 18 | 19 | public BuildUtil(ConstantPool constantPool, ByteBuffer byteBuffer) { 20 | this.constantPool = constantPool; 21 | this.byteBuffer = byteBuffer; 22 | } 23 | 24 | public int getU1() { 25 | int signed = byteBuffer.get(); 26 | return signed & 0xFF; 27 | } 28 | 29 | public int getU2() { 30 | int signed = byteBuffer.getShort(); 31 | return signed & 0xFFFF; 32 | } 33 | 34 | public long getU4() { 35 | long signed = byteBuffer.getInt(); 36 | return signed & 0xFFFFFFFFL; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/AttributeInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | @Getter 10 | @Setter 11 | public class AttributeInfo { 12 | 13 | private int attributeNameAndIndex; 14 | private int attributeLength; 15 | private byte[] info; 16 | 17 | protected AttributeInfo(int attributeNameAndIndex, int attributeLength) { 18 | this.attributeNameAndIndex = attributeNameAndIndex; 19 | this.attributeLength = attributeLength; 20 | } 21 | 22 | public AttributeInfo(BuildUtil buildUtil, int index, int length) { 23 | ByteBuffer byteBuffer = buildUtil.getByteBuffer(); 24 | this.attributeNameAndIndex = index; 25 | this.attributeLength = length; 26 | this.info = new byte[this.attributeLength]; 27 | for (int i = 0; i < this.attributeLength; i++) { 28 | info[i] = byteBuffer.get(); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/BootstrapMethodInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class BootstrapMethodInfo { 10 | private int bootstrapMethodRef; 11 | private int numBootstrapArguments; 12 | private int[] bootstrapArguments; 13 | 14 | public BootstrapMethodInfo(BuildUtil buildUtil) { 15 | bootstrapMethodRef = buildUtil.getU2(); 16 | numBootstrapArguments = buildUtil.getU2(); 17 | bootstrapArguments = new int[numBootstrapArguments]; 18 | for (int i = 0; i < bootstrapArguments.length; i++) { 19 | bootstrapArguments[i] = buildUtil.getU2(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/BootstrapMethodsAttribute.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class BootstrapMethodsAttribute extends AttributeInfo { 6 | private int numBootstrapMethods; 7 | private BootstrapMethodInfo[] bootstrapMethods; 8 | 9 | public BootstrapMethodsAttribute(BuildUtil buildUtil, int index, int length) { 10 | super(index, length); 11 | numBootstrapMethods = buildUtil.getU2(); 12 | bootstrapMethods = new BootstrapMethodInfo[numBootstrapMethods]; 13 | for (int i = 0; i < bootstrapMethods.length; i++) { 14 | bootstrapMethods[i] = new BootstrapMethodInfo(buildUtil); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/ConstantValueAttr.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class ConstantValueAttr extends AttributeInfo { 10 | private int constantValueIndex; 11 | 12 | public ConstantValueAttr(BuildUtil buildUtil, int index, int length) { 13 | super(index, length); 14 | constantValueIndex = buildUtil.getU2(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/ExceptionTable.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | @Getter 7 | @Setter 8 | public class ExceptionTable { 9 | private int startPC; 10 | private int endPC; 11 | private int handlerPC; 12 | private int catchType; 13 | 14 | public ExceptionTable(int startPC, int endPC, int handlerPC, int catchType) { 15 | this.startPC = startPC; 16 | this.endPC = endPC; 17 | this.handlerPC = handlerPC; 18 | this.catchType = catchType; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/ExceptionsAttribute.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class ExceptionsAttribute extends AttributeInfo { 6 | private int numberOfExceptions; 7 | private int[] exceptionIndexTable; 8 | 9 | public ExceptionsAttribute(BuildUtil buildUtil, int index, int length) { 10 | super(index, length); 11 | numberOfExceptions = buildUtil.getU2(); 12 | this.exceptionIndexTable = new int[numberOfExceptions]; 13 | for (int i = 0; i < this.exceptionIndexTable.length; i++) { 14 | exceptionIndexTable[i] = buildUtil.getU2(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/AppendFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class AppendFrame extends StackMapFrame { 6 | private int offsetDelta; 7 | private VerificationTypeInfo[] locals; 8 | 9 | public AppendFrame(int frameType, BuildUtil buildUtil) { 10 | super(frameType); 11 | offsetDelta = buildUtil.getU2(); 12 | locals = new VerificationTypeInfo[frameType - 251]; 13 | for (int i = 0; i < locals.length; i++) { 14 | locals[i] = VerificationTypeInfo.read(buildUtil); 15 | } 16 | } 17 | 18 | @Override 19 | public int getOffsetDelta() { 20 | return offsetDelta; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/ChopFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class ChopFrame extends StackMapFrame { 6 | private int offsetDelta; 7 | 8 | public ChopFrame(int frameType, BuildUtil buildUtil) { 9 | super(frameType); 10 | offsetDelta = buildUtil.getU2(); 11 | } 12 | 13 | @Override 14 | public int getOffsetDelta() { 15 | return offsetDelta; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/FullFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class FullFrame extends StackMapFrame { 6 | private int offsetDelta; 7 | private int numberOfLocals; 8 | private VerificationTypeInfo[] locals; 9 | private int numberOfStackItems; 10 | private VerificationTypeInfo[] stack; 11 | 12 | public FullFrame(int frameType, BuildUtil buildUtil) { 13 | super(frameType); 14 | offsetDelta = buildUtil.getU2(); 15 | numberOfLocals = buildUtil.getU2(); 16 | locals = new VerificationTypeInfo[numberOfLocals]; 17 | for (int i = 0; i < locals.length; i++) { 18 | locals[i] = VerificationTypeInfo.read(buildUtil); 19 | } 20 | numberOfStackItems = buildUtil.getU2(); 21 | stack = new VerificationTypeInfo[numberOfStackItems]; 22 | for (int i = 0; i < stack.length; i++) { 23 | stack[i] = VerificationTypeInfo.read(buildUtil); 24 | } 25 | } 26 | 27 | @Override 28 | public int getOffsetDelta() { 29 | return offsetDelta; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/ObjectVariableInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class ObjectVariableInfo extends VerificationTypeInfo { 6 | private int cpIndex; 7 | 8 | public ObjectVariableInfo(BuildUtil buildUtil) { 9 | super(VerificationTypeInfo.ITEM_Object); 10 | cpIndex = buildUtil.getU2(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/SameFramExtended.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class SameFramExtended extends StackMapFrame { 6 | private int offsetDelta; 7 | 8 | public SameFramExtended(int frameType, BuildUtil buildUtil) { 9 | super(frameType); 10 | offsetDelta = buildUtil.getU2(); 11 | } 12 | 13 | @Override 14 | public int getOffsetDelta() { 15 | return offsetDelta; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/SameFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | public class SameFrame extends StackMapFrame { 4 | 5 | public SameFrame(int frameType) { 6 | super(frameType); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/SameLocals1StackItemFramExtended.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class SameLocals1StackItemFramExtended extends StackMapFrame { 6 | private int offsetDelta; 7 | private VerificationTypeInfo[] stack = new VerificationTypeInfo[1]; 8 | 9 | public SameLocals1StackItemFramExtended(int frameType, BuildUtil buildUtil) { 10 | super(frameType); 11 | offsetDelta = buildUtil.getU2(); 12 | stack[0] = VerificationTypeInfo.read(buildUtil); 13 | } 14 | 15 | @Override 16 | public int getOffsetDelta() { 17 | return offsetDelta; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/SameLocals1StackItemFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class SameLocals1StackItemFrame extends StackMapFrame { 6 | private VerificationTypeInfo[] stack = new VerificationTypeInfo[1]; 7 | 8 | public SameLocals1StackItemFrame(int frameType, BuildUtil buildUtil) { 9 | super(frameType); 10 | stack[0] = VerificationTypeInfo.read(buildUtil); 11 | } 12 | 13 | @Override 14 | public int getOffsetDelta() { 15 | return super.frameType - 64; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/StackMapTableAttribute.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | import com.njuse.jvmfinal.classloader.classfileparser.attribute.AttributeInfo; 5 | 6 | public class StackMapTableAttribute extends AttributeInfo { 7 | private int numberOfEntries; 8 | private StackMapFrame[] entries; 9 | 10 | public StackMapTableAttribute(BuildUtil buildUtil, int index, int length) { 11 | super(index, length); 12 | numberOfEntries = buildUtil.getU2(); 13 | entries = new StackMapFrame[numberOfEntries]; 14 | for (int i = 0; i < entries.length; i++) { 15 | entries[i] = StackMapFrame.read(buildUtil); 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/attribute/smta/UninitializedVariableInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.attribute.smta; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.BuildUtil; 4 | 5 | public class UninitializedVariableInfo extends VerificationTypeInfo { 6 | private int offset; 7 | 8 | public UninitializedVariableInfo(BuildUtil buildUtil) { 9 | super(VerificationTypeInfo.ITEM_Uninitialized); 10 | this.offset = buildUtil.getU2(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/ClassInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | /** 6 | * todo 7 | */ 8 | public class ClassInfo extends ConstantPoolInfo { 9 | private int nameIndex; 10 | 11 | public ClassInfo(ConstantPool constantPool, int nameIndex) { 12 | super(constantPool); 13 | this.nameIndex = nameIndex; 14 | super.tag = ConstantPoolInfo.CLASS; 15 | } 16 | 17 | 18 | public String getClassName() { 19 | return ((UTF8Info) myCP.get(nameIndex)).getString(); 20 | 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return getClassName(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/DoubleInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | import java.nio.ByteBuffer; 6 | 7 | public class DoubleInfo extends ConstantPoolInfo { 8 | private byte[] highBytes; 9 | private byte[] lowBytes; 10 | private double value; 11 | 12 | public DoubleInfo(ConstantPool constantPool, byte[] highBytes, byte[] lowBytes) { 13 | super(constantPool); 14 | this.highBytes = highBytes; 15 | this.lowBytes = lowBytes; 16 | if (lowBytes.length != 4 || highBytes.length != 4) { 17 | throw new UnsupportedOperationException( 18 | "Double constantpool info expects 8 bytes, actual is " + lowBytes.length + " " + highBytes.length); 19 | } 20 | 21 | byte[] valueBytes = new byte[8]; 22 | System.arraycopy(highBytes, 0, valueBytes, 0, 4); 23 | System.arraycopy(lowBytes, 0, valueBytes, 4, 4); 24 | this.value = ByteBuffer.wrap(valueBytes).getDouble(); 25 | super.tag = ConstantPoolInfo.DOUBLE; 26 | 27 | } 28 | 29 | @Override 30 | public int getEntryLength() { 31 | return 2; 32 | } 33 | 34 | 35 | public Double getValue() { 36 | return value; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/FieldrefInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import org.apache.commons.lang3.tuple.Pair; 5 | 6 | public class FieldrefInfo extends MemberRefInfo { 7 | private int classIndex; 8 | private int nameAndTypeIndex; 9 | 10 | public FieldrefInfo(ConstantPool constantPool, int classIndex, int nameAndTypeIndex) { 11 | super(constantPool); 12 | this.classIndex = classIndex; 13 | this.nameAndTypeIndex = nameAndTypeIndex; 14 | super.tag = ConstantPoolInfo.FIELD_REF; 15 | } 16 | 17 | 18 | public String getClassName() { 19 | return getClassName(classIndex); 20 | } 21 | 22 | 23 | public Pair getNameAndDescriptor() { 24 | return ((NameAndTypeInfo) myCP.get(nameAndTypeIndex)).getNameAndDescriptor(); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/FloatInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | import java.nio.ByteBuffer; 6 | 7 | public class FloatInfo extends ConstantPoolInfo { 8 | private byte[] bytes; 9 | private float value; 10 | 11 | public FloatInfo(ConstantPool constantPool, byte[] bytes) { 12 | super(constantPool); 13 | this.bytes = bytes; 14 | if (bytes.length != 4) { 15 | throw new UnsupportedOperationException( 16 | "Float constantpool info expects 4 bytes, actual is " + bytes.length); 17 | } 18 | this.value = ByteBuffer.wrap(bytes).getFloat(); 19 | super.tag = ConstantPoolInfo.FLOAT; 20 | 21 | } 22 | 23 | public Float getValue() { 24 | return value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/IntegerInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | import java.nio.ByteBuffer; 6 | 7 | public class IntegerInfo extends ConstantPoolInfo { 8 | private byte[] bytes; 9 | private int value; 10 | 11 | public IntegerInfo(ConstantPool constantPool, byte[] bytes) { 12 | super(constantPool); 13 | this.bytes = bytes; 14 | if (bytes.length != 4) { 15 | throw new UnsupportedOperationException( 16 | "Integer constantpool info expects 4 bytes, actual is " + bytes.length); 17 | } 18 | this.value = ByteBuffer.wrap(bytes).getInt(); 19 | super.tag = ConstantPoolInfo.INTEGER; 20 | } 21 | 22 | 23 | public Integer getValue() { 24 | return value; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/InterfaceMethodrefInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import org.apache.commons.lang3.tuple.Pair; 5 | 6 | public class InterfaceMethodrefInfo extends MemberRefInfo { 7 | private int classIndex; 8 | private int nameAndTypeIndex; 9 | 10 | public InterfaceMethodrefInfo(ConstantPool constantPool, int classIndex, int nameAndTypeIndex) { 11 | super(constantPool); 12 | this.classIndex = classIndex; 13 | this.nameAndTypeIndex = nameAndTypeIndex; 14 | super.tag = ConstantPoolInfo.INTERFACE_METHOD_REF; 15 | } 16 | 17 | public String getClassName() { 18 | return getClassName(classIndex); 19 | } 20 | 21 | 22 | public Pair getNameAndDescriptor() { 23 | return ((NameAndTypeInfo) myCP.get(nameAndTypeIndex)).getNameAndDescriptor(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/InvokeDynamicInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | public class InvokeDynamicInfo extends ConstantPoolInfo { 6 | private int bootstrapMethodAttrIndex; 7 | private int nameAndTypeIndex; 8 | 9 | public InvokeDynamicInfo(ConstantPool constantPool, int bootstrapMethodAttrIndex, int nameAndTypeIndex) { 10 | super(constantPool); 11 | this.bootstrapMethodAttrIndex = bootstrapMethodAttrIndex; 12 | this.nameAndTypeIndex = nameAndTypeIndex; 13 | super.tag = ConstantPoolInfo.INVOKE_DYNAMIC; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/LongInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import org.apache.commons.lang3.ArrayUtils; 5 | 6 | import java.nio.ByteBuffer; 7 | 8 | public class LongInfo extends ConstantPoolInfo { 9 | private byte[] highBytes; 10 | private byte[] lowBytes; 11 | private long value; 12 | 13 | public LongInfo(ConstantPool constantPool, byte[] highBytes, byte[] lowBytes) { 14 | super(constantPool); 15 | this.highBytes = highBytes; 16 | this.lowBytes = lowBytes; 17 | if (lowBytes.length != 4 || highBytes.length != 4) { 18 | throw new UnsupportedOperationException( 19 | "Long constantpool info expects 8 bytes, actual is " + lowBytes.length + " " + highBytes.length); 20 | } 21 | this.value = ByteBuffer 22 | .wrap(ArrayUtils.addAll(highBytes, lowBytes)) 23 | .getLong(); 24 | super.tag = ConstantPoolInfo.LONG; 25 | 26 | } 27 | 28 | @Override 29 | public int getEntryLength() { 30 | return 2; 31 | } 32 | 33 | 34 | public Long getValue() { 35 | return value; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/MemberRefInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import org.apache.commons.lang3.tuple.Pair; 5 | 6 | public abstract class MemberRefInfo extends ConstantPoolInfo { 7 | 8 | public MemberRefInfo(ConstantPool myCP) { 9 | super(myCP); 10 | } 11 | 12 | 13 | public abstract String getClassName(); 14 | 15 | protected String getClassName(int idx) { 16 | return ((ClassInfo) myCP.get(idx)).getClassName(); 17 | } 18 | 19 | 20 | public abstract Pair getNameAndDescriptor(); 21 | } 22 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/MethodHandleInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | public class MethodHandleInfo extends ConstantPoolInfo { 6 | private int referenceKind; 7 | private int referenceIndex; 8 | 9 | public MethodHandleInfo(ConstantPool constantPool, int referenceKind, int referenceIndex) { 10 | super(constantPool); 11 | this.referenceKind = referenceKind; 12 | this.referenceIndex = referenceIndex; 13 | super.tag = ConstantPoolInfo.METHOD_HANDLE; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/MethodTypeInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | public class MethodTypeInfo extends ConstantPoolInfo { 6 | private int descriptorIndex; 7 | 8 | public MethodTypeInfo(ConstantPool constantPool, int descriptorIndex) { 9 | super(constantPool); 10 | this.descriptorIndex = descriptorIndex; 11 | super.tag = ConstantPoolInfo.METHOD_TYPE; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/MethodrefInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import org.apache.commons.lang3.tuple.Pair; 5 | 6 | public class MethodrefInfo extends MemberRefInfo { 7 | private int classIndex; 8 | private int nameAndTypeIndex; 9 | 10 | public MethodrefInfo(ConstantPool constantPool, int classIndex, int nameAndTypeIndex) { 11 | super(constantPool); 12 | this.classIndex = classIndex; 13 | this.nameAndTypeIndex = nameAndTypeIndex; 14 | super.tag = ConstantPoolInfo.METHOD_REF; 15 | } 16 | 17 | 18 | public String getClassName() { 19 | return getClassName(classIndex); 20 | } 21 | 22 | 23 | public Pair getNameAndDescriptor() { 24 | return ((NameAndTypeInfo) myCP.get(nameAndTypeIndex)).getNameAndDescriptor(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/NameAndTypeInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | import lombok.Getter; 5 | import org.apache.commons.lang3.tuple.Pair; 6 | 7 | @Getter 8 | public class NameAndTypeInfo extends ConstantPoolInfo { 9 | private int nameIndex; 10 | private int descriptorIndex; 11 | 12 | public NameAndTypeInfo(ConstantPool constantPool, int nameIndex, int descriptorIndex) { 13 | super(constantPool); 14 | this.nameIndex = nameIndex; 15 | this.descriptorIndex = descriptorIndex; 16 | super.tag = ConstantPoolInfo.NAME_AND_TYPE; 17 | } 18 | 19 | public Pair getNameAndDescriptor() { 20 | return Pair.of( 21 | ((UTF8Info) myCP.get(nameIndex)).getString(), 22 | ((UTF8Info) myCP.get(descriptorIndex)).getString()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /jvm-final/classloader/classfileparser/constantpool/info/StringInfo.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classfileparser.constantpool.info; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.ConstantPool; 4 | 5 | public class StringInfo extends ConstantPoolInfo { 6 | private int stringIndex; 7 | 8 | public StringInfo(ConstantPool constantPool, int stringIndex) { 9 | super(constantPool); 10 | this.stringIndex = stringIndex; 11 | super.tag = ConstantPoolInfo.STRING; 12 | } 13 | 14 | public String getStringValue() { 15 | UTF8Info utf8Info = (UTF8Info) myCP.get(stringIndex); 16 | return utf8Info.getString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/classloader/classreader/DirEntry.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classreader; 2 | 3 | import com.njuse.jvmfinal.util.IOUtil; 4 | 5 | import java.io.*; 6 | 7 | public class DirEntry extends Entry { 8 | public String absDir; 9 | 10 | public DirEntry(String classpath) { 11 | super(classpath); 12 | this.absDir = new File(classpath).getAbsolutePath(); 13 | } 14 | 15 | //className doesn't contain ".class" 16 | @Override 17 | public byte[] readClass(String className) throws IOException { 18 | File file = new File(constructPath(className)); 19 | if ( file.exists() ) { 20 | InputStream inputStream = new FileInputStream(constructPath(className)); 21 | return IOUtil.readFileByBytes(inputStream); 22 | } else { 23 | return null; 24 | } 25 | } 26 | 27 | public String constructPath(String className) { 28 | return IOUtil.transform(absDir + FILE_SEPARATOR 29 | + className.replace(".", FILE_SEPARATOR) + ".class"); 30 | } 31 | 32 | public String toString() { 33 | return absDir; 34 | } 35 | 36 | public static String transform(String pathName) { 37 | return pathName.contains("/") ? pathName.replace("/", File.separator) : pathName; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /jvm-final/classloader/classreader/Entry.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classreader; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public abstract class Entry { 7 | public final String PATH_SEPARATOR = "" + File.pathSeparator; 8 | public final String FILE_SEPARATOR = "" + File.separator; 9 | public String classpath; 10 | 11 | public Entry(String classpath) { 12 | this.classpath = classpath; 13 | } 14 | 15 | public String toString(){ 16 | return classpath; 17 | } 18 | 19 | public abstract byte[] readClass(String className) throws IOException; 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/classloader/classreader/WildEntry.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.classloader.classreader; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | public class WildEntry extends Entry { 7 | 8 | public WildEntry(String classpath) { 9 | super(classpath); 10 | } 11 | 12 | @Override 13 | public byte[] readClass(String className) throws IOException { 14 | File file = new File(classpath.replace(FILE_SEPARATOR+"*", "")); 15 | String[] files = file.list(); 16 | //System.out.println(classpath.replace(FILE_SEPARATOR+"*", "")); 17 | if( files != null ){ 18 | //System.out.println(stringConstructor(files, classpath, PATH_SEPARATOR)); 19 | CompositeEntry comp = new CompositeEntry(stringConstructor(files, classpath, PATH_SEPARATOR)); 20 | return comp.readClass(className); 21 | } 22 | return null; 23 | } 24 | 25 | public static String stringConstructor(String[] files, String classpath, String PATH_SEPERATOR){ 26 | StringBuilder str = new StringBuilder(); 27 | for( String file : files){ 28 | str.append(classpath.replace("*", file)); 29 | str.append(PATH_SEPERATOR); 30 | } 31 | return str.toString(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /jvm-final/instructions/README: -------------------------------------------------------------------------------- 1 | INSTRUCTIONS 2 | -------------------------------------------------------------------------------- /jvm-final/instructions/base/BranchInstruction.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.base; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | @Setter 10 | @Getter 11 | public abstract class BranchInstruction extends Instruction{ 12 | public int offset; 13 | 14 | public BranchInstruction() {} 15 | 16 | @Override 17 | public void fetchOperands(ByteReader reader) { 18 | this.offset = reader.read16(); 19 | } 20 | 21 | public String toString(){ 22 | return this.getClass().getSimpleName() + "offset: " + this.offset; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /jvm-final/instructions/base/Index16Instruction.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.base; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import java.nio.ByteBuffer; 7 | 8 | @Setter 9 | @Getter 10 | public abstract class Index16Instruction extends Instruction{ 11 | private int index; 12 | 13 | @Override 14 | public void fetchOperands(ByteReader reader) { 15 | this.index = reader.readU16(); 16 | } 17 | 18 | public String toString(){ 19 | return this.getClass().getSimpleName() + "index: " + this.index; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /jvm-final/instructions/base/Index8Instruction.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.base; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | public abstract class Index8Instruction extends Instruction{ 10 | public int index; 11 | 12 | public Index8Instruction() {} 13 | 14 | @Override 15 | public void fetchOperands(ByteReader reader) { 16 | this.index = reader.readU8(); 17 | } 18 | 19 | public String toString(){ 20 | return this.getClass().getSimpleName() + "index: " + this.index; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/base/Instruction.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.base; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | import java.nio.ByteBuffer; 6 | 7 | public abstract class Instruction { 8 | public Instruction(){} 9 | 10 | public abstract void fetchOperands(ByteReader reader); 11 | 12 | public abstract void execute(MyFrame frame); 13 | 14 | public void jump(MyFrame frame, int offset){ 15 | int PC = frame.getThread().getPC(); 16 | int nextPC = PC + offset; 17 | frame.setNextPC(nextPC); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/base/NoOperandsInstruction.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.base; 2 | 3 | import java.nio.ByteBuffer; 4 | 5 | public abstract class NoOperandsInstruction extends Instruction { 6 | public NoOperandsInstruction(){} 7 | 8 | @Override 9 | public void fetchOperands(ByteReader reader) { } 10 | 11 | public String toString(){ 12 | return this.getClass().getSimpleName(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/DCMPG.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class DCMPG extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val2 = stack.popDouble(); 12 | double val1 = stack.popDouble(); 13 | if(val1 > val2){ 14 | stack.pushInt(1); 15 | } else if(val1 == val2){ 16 | stack.pushInt(0); 17 | } else if(val1 < val2){ 18 | stack.pushInt(-1); 19 | } else { 20 | stack.pushInt(1); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/DCMPL.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class DCMPL extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val2 = stack.popDouble(); 12 | double val1 = stack.popDouble(); 13 | if(val1 > val2){ 14 | stack.pushInt(1); 15 | } else if(val1 == val2){ 16 | stack.pushInt(0); 17 | } else if(val1 < val2){ 18 | stack.pushInt(-1); 19 | } else { 20 | stack.pushInt(-1); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/FCMPG.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class FCMPG extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val2 = stack.popFloat(); 12 | float val1 = stack.popFloat(); 13 | if(val1 > val2){ 14 | stack.pushInt(1); 15 | } else if(val1 == val2){ 16 | stack.pushInt(0); 17 | } else if(val1 < val2){ 18 | stack.pushInt(-1); 19 | } else { 20 | stack.pushInt(1); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/FCMPL.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class FCMPL extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val2 = stack.popFloat(); 12 | float val1 = stack.popFloat(); 13 | if(val1 > val2){ 14 | stack.pushInt(1); 15 | } else if(val1 == val2){ 16 | stack.pushInt(0); 17 | } else if(val1 < val2){ 18 | stack.pushInt(-1); 19 | } else { 20 | stack.pushInt(-1); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFACMP/IF_ACMPEQ.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFACMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | import com.njuse.jvmfinal.memory.heap.MyObject; 7 | 8 | public class IF_ACMPEQ extends BranchInstruction { 9 | @Override 10 | public void execute(MyFrame frame) { 11 | OperandStack stack = frame.getOperandStack(); 12 | MyObject ref2 = stack.popRef(); 13 | MyObject ref1 = stack.popRef(); 14 | if( ref1 == ref2 ){ 15 | jump(frame, this.offset); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFACMP/IF_ACMPNE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFACMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | import com.njuse.jvmfinal.memory.heap.MyObject; 7 | 8 | public class IF_ACMPNE extends BranchInstruction { 9 | @Override 10 | public void execute(MyFrame frame) { 11 | OperandStack stack = frame.getOperandStack(); 12 | MyObject ref2 = stack.popRef(); 13 | MyObject ref1 = stack.popRef(); 14 | if( ref1 != ref2 ){ 15 | jump(frame, this.offset); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFEQ.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFEQ extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val == 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFGE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFGE extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val >= 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFGT.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFGT extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val > 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFLE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFLE extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val <= 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFLT.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFLT extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val < 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFCOND/IFNE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFCOND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class IFNE extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | if( val != 0 ){ 11 | jump(frame, this.offset); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPEQ.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPEQ extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if( val1 == val2 ){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPGE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPGE extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if( val1 >= val2 ){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPGT.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPGT extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if( val1 > val2 ){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPLE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPLE extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if( val1 <= val2 ){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPLT.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPLT extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if( val1 < val2 ){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/IFICMP/IF_ICMPNE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons.IFICMP; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IF_ICMPNE extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if(val1 != val2){ 14 | jump(frame, this.offset); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/comparisons/LCMP.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.comparisons; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class LCMP extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val2 = stack.popLong(); 12 | long val1 = stack.popLong(); 13 | if(val1 > val2){ 14 | stack.pushInt(1); 15 | } else if (val1 == val2) { 16 | stack.pushInt(0); 17 | } else { 18 | stack.pushInt(-1); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/ACONST_NULL.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.NullObject; 5 | import com.njuse.jvmfinal.runtime.MyFrame; 6 | 7 | public class ACONST_NULL extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | NullObject nullObject = new NullObject(); 11 | frame.getOperandStack().pushRef(nullObject); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/BIPUSH.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.ByteReader; 4 | import com.njuse.jvmfinal.instructions.base.Instruction; 5 | import com.njuse.jvmfinal.runtime.MyFrame; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | public class BIPUSH extends Instruction { 10 | private int val; 11 | 12 | public BIPUSH(){} 13 | 14 | @Override 15 | public void execute(MyFrame frame) { 16 | frame.getOperandStack().pushInt(this.val); 17 | } 18 | 19 | @Override 20 | public void fetchOperands(ByteReader reader) { 21 | this.val = reader.read8(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/DCONST_N/DCONST_0.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.DCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class DCONST_0 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushDouble(0.0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/DCONST_N/DCONST_1.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.DCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class DCONST_1 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushDouble(1.0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/FCONST_N/FCONST_0.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.FCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class FCONST_0 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushFloat(0.0f); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/FCONST_N/FCONST_1.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.FCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class FCONST_1 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushFloat(1.0f); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/FCONST_N/FCONST_2.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.FCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class FCONST_2 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushFloat(2.0f); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/ICONST_N/ICONST_M1.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.ICONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class ICONST_M1 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushInt(-1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/ICONST_N/ICONST_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.ICONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class ICONST_N extends NoOperandsInstruction { 7 | public int val; 8 | private static int[] valid = new int[]{0, 1, 2, 3, 4, 5}; 9 | 10 | public static void checkIndex(int i){ 11 | assert i >= valid[0] && i <= valid[valid.length - 1]; 12 | } 13 | 14 | public ICONST_N(int val){ 15 | checkIndex(val); 16 | this.val = val; 17 | } 18 | 19 | @Override 20 | public void execute(MyFrame frame) { 21 | frame.getOperandStack().pushInt(this.val); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/LCONST_N/LCONST_0.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.LCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class LCONST_0 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushLong(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/LCONST_N/LCONST_1.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants.LCONST_N; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class LCONST_1 extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | frame.getOperandStack().pushLong(1L); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/LDC.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 5 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.FloatWrapper; 6 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.IntWrapper; 7 | import com.njuse.jvmfinal.runtime.MyFrame; 8 | import com.njuse.jvmfinal.runtime.OperandStack; 9 | 10 | public class LDC extends Index8Instruction { 11 | @Override 12 | public void execute(MyFrame frame) { 13 | OperandStack stack = frame.getOperandStack(); 14 | Constant constant = frame.getMethod().getClazz().getRuntimeConstantPool().getConstant(this.index); 15 | if (constant instanceof IntWrapper) { 16 | stack.pushInt(((IntWrapper) constant).getValue()); 17 | } 18 | else if (constant instanceof FloatWrapper) { 19 | stack.pushFloat(((FloatWrapper) constant).getValue()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/LDC2_W.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 5 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.DoubleWrapper; 6 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.FloatWrapper; 7 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.IntWrapper; 8 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.LongWrapper; 9 | import com.njuse.jvmfinal.runtime.MyFrame; 10 | import com.njuse.jvmfinal.runtime.OperandStack; 11 | 12 | public class LDC2_W extends Index16Instruction { 13 | @Override 14 | public void execute(MyFrame frame) { 15 | OperandStack stack = frame.getOperandStack(); 16 | Constant constant = frame.getMethod().getClazz().getRuntimeConstantPool().getConstant(this.getIndex()); 17 | if (constant instanceof LongWrapper) { 18 | stack.pushLong(((LongWrapper) constant).getValue()); 19 | } 20 | else if (constant instanceof DoubleWrapper) { 21 | stack.pushDouble(((DoubleWrapper) constant).getValue()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/LDC_W.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 5 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.FloatWrapper; 6 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.IntWrapper; 7 | import com.njuse.jvmfinal.runtime.MyFrame; 8 | import com.njuse.jvmfinal.runtime.OperandStack; 9 | 10 | public class LDC_W extends Index16Instruction { 11 | @Override 12 | public void execute(MyFrame frame) { 13 | OperandStack stack = frame.getOperandStack(); 14 | Constant constant = frame.getMethod().getClazz().getRuntimeConstantPool().getConstant(this.getIndex()); 15 | if (constant instanceof IntWrapper) { 16 | stack.pushInt(((IntWrapper) constant).getValue()); 17 | } 18 | else if (constant instanceof FloatWrapper) { 19 | stack.pushFloat(((FloatWrapper) constant).getValue()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/NOP.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class NOP extends NoOperandsInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { } 9 | } 10 | -------------------------------------------------------------------------------- /jvm-final/instructions/constants/SIPUSH.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.constants; 2 | 3 | import com.njuse.jvmfinal.instructions.base.ByteReader; 4 | import com.njuse.jvmfinal.instructions.base.Instruction; 5 | import com.njuse.jvmfinal.runtime.MyFrame; 6 | 7 | import java.nio.ByteBuffer; 8 | 9 | public class SIPUSH extends Instruction { 10 | private int val; 11 | 12 | public SIPUSH() {} 13 | 14 | @Override 15 | public void fetchOperands(ByteReader reader) { 16 | this.val = reader.read16(); 17 | } 18 | 19 | @Override 20 | public void execute(MyFrame frame) { 21 | frame.getOperandStack().pushInt(val); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/ARETURN.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.MyObject; 5 | import com.njuse.jvmfinal.runtime.MyFrame; 6 | import com.njuse.jvmfinal.runtime.MyThread; 7 | 8 | public class ARETURN extends NoOperandsInstruction { 9 | @Override 10 | public void execute(MyFrame frame) { 11 | MyThread thread = frame.getThread(); 12 | MyFrame currFrame = thread.popFrame(); 13 | MyFrame invoker = thread.getTopFrame(); 14 | MyObject retRef = currFrame.getOperandStack().popRef(); 15 | invoker.getOperandStack().pushRef(retRef); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/DRETURN.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.MyThread; 6 | 7 | public class DRETURN extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyThread thread = frame.getThread(); 11 | MyFrame currFrame = thread.popFrame(); 12 | MyFrame invoker = thread.getTopFrame(); 13 | double retVal = currFrame.getOperandStack().popDouble(); 14 | invoker.getOperandStack().pushDouble(retVal); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/FRETURN.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.MyThread; 6 | 7 | public class FRETURN extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyThread thread = frame.getThread(); 11 | MyFrame currFrame = thread.popFrame(); 12 | MyFrame invoker = thread.getTopFrame(); 13 | float retVal = currFrame.getOperandStack().popFloat(); 14 | invoker.getOperandStack().pushFloat(retVal); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/GOTO.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class GOTO extends BranchInstruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | jump(frame, this.offset); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/IRETURN.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.MyThread; 6 | 7 | public class IRETURN extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyThread thread = frame.getThread(); 11 | MyFrame currFrame = thread.popFrame(); 12 | MyFrame invoker = thread.getTopFrame(); 13 | int retVal = currFrame.getOperandStack().popInt(); 14 | invoker.getOperandStack().pushInt(retVal); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/control/LOOKUP_SWITCH.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.control; 2 | 3 | import com.njuse.jvmfinal.instructions.base.ByteReader; 4 | import com.njuse.jvmfinal.instructions.base.Instruction; 5 | import com.njuse.jvmfinal.runtime.MyFrame; 6 | 7 | public class LOOKUP_SWITCH extends Instruction { 8 | public int defaultOffset; 9 | public int npairs; 10 | public int[] matchOffsets; 11 | 12 | @Override 13 | public void fetchOperands(ByteReader reader) { 14 | reader.skipPadding(); 15 | this.defaultOffset = reader.read32(); 16 | // Each of the npairs pairs consists of an int match and a signed 32-bit offset 17 | this.npairs = reader.read32(); 18 | this.matchOffsets = reader.readIntArray(npairs * 2); 19 | } 20 | 21 | @Override 22 | public void execute(MyFrame frame) { 23 | int key = frame.getOperandStack().popInt(); 24 | int offset; 25 | int flg = 0; 26 | for(int i=0; i= this.low && index <= this.high){ 28 | offset = this.jmpOffsets[index]; 29 | } else { 30 | offset = this.defaultOffset; 31 | } 32 | jump(frame, offset); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/D2F.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class D2F extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val = stack.popDouble(); 12 | stack.pushFloat((float) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/D2I.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class D2I extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val = stack.popDouble(); 12 | stack.pushInt((int) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/D2L.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class D2L extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val = stack.popDouble(); 12 | stack.pushLong((long) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/F2D.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class F2D extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val = stack.popFloat(); 12 | stack.pushDouble(val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/F2I.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class F2I extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val = stack.popFloat(); 12 | stack.pushInt((int) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/F2L.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class F2L extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val = stack.popFloat(); 12 | stack.pushLong((long) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2B.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2B extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | int tmp = val << 24; 13 | int ret = tmp >> 24; 14 | stack.pushInt(ret); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2C.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2C extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | stack.pushInt(val & 0xFFFF); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2D.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2D extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | stack.pushDouble(val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2F.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2F extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | stack.pushFloat((float) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2L.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2L extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | stack.pushLong(val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/I2S.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class I2S extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val = stack.popInt(); 12 | int tmp = val << 16; 13 | int ret = tmp >> 16; 14 | stack.pushInt(ret); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/L2D.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class L2D extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val = stack.popLong(); 12 | stack.pushDouble((double) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/L2F.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class L2F extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val = stack.popLong(); 12 | stack.pushFloat((float) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/conversions/L2I.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.conversions; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class L2I extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val = stack.popLong(); 12 | stack.pushInt((int) val); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /jvm-final/instructions/extended/IFNONNULL.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.extended; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | 7 | public class IFNONNULL extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyObject ref = frame.getOperandStack().popRef(); 11 | if ( !ref.isNull() ){ 12 | jump(frame, this.offset); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jvm-final/instructions/extended/IFNULL.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.extended; 2 | 3 | import com.njuse.jvmfinal.instructions.base.BranchInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | 7 | public class IFNULL extends BranchInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyObject ref = frame.getOperandStack().popRef(); 11 | if( ref.isNull() ){ 12 | jump(frame, this.offset); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | 7 | public class ALOAD extends Index8Instruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyObject ref = frame.getLocalVars().getRef(this.index); 11 | frame.getOperandStack().pushRef(ref); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ALOAD_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | import com.njuse.jvmfinal.memory.heap.MyObject; 5 | 6 | public class ALOAD_N extends LOAD_N { 7 | public ALOAD_N(int index){ 8 | checkIndex(index); 9 | this.index = index; 10 | } 11 | 12 | @Override 13 | public void execute(MyFrame frame) { 14 | MyObject ref = frame.getLocalVars().getRef(this.index); 15 | frame.getOperandStack().pushRef(ref); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/AALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class AALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | MyObject[] arrs = ((ArrayObject) arrRef).getRefs(); 16 | stack.pushRef(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/BALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class BALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | int[] arrs = ((ArrayObject) arrRef).getBytes(); 16 | stack.pushInt(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/CALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class CALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | int[] arrs = ((ArrayObject) arrRef).getChars(); 16 | stack.pushInt(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/DALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class DALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | double[] arrs = ((ArrayObject) arrRef).getDoubles(); 16 | stack.pushDouble(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/FALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class FALOAD extends Index16Instruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | float[] arrs = ((ArrayObject) arrRef).getFloats(); 16 | stack.pushFloat(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/IALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class IALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | int[] arrs = ((ArrayObject) arrRef).getInts(); 16 | stack.pushInt(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/LALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class LALOAD extends Index16Instruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | long[] arrs = ((ArrayObject) arrRef).getLongs(); 16 | stack.pushLong(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ArrayLoad/SALOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads.ArrayLoad; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class SALOAD extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int index = stack.popInt(); 14 | MyObject arrRef = stack.popRef(); 15 | int[] arrs = ((ArrayObject) arrRef).getShorts(); 16 | stack.pushInt(arrs[index]); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/DLOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class DLOAD extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | double val = frame.getLocalVars().getDouble(this.index); 10 | frame.getOperandStack().pushDouble(val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/DLOAD_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class DLOAD_N extends LOAD_N { 6 | public DLOAD_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | double val = frame.getLocalVars().getDouble(this.index); 14 | frame.getOperandStack().pushDouble(val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/FLOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class FLOAD extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | float val = frame.getLocalVars().getFloat(this.index); 10 | frame.getOperandStack().pushFloat(val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/FLOAD_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class FLOAD_N extends LOAD_N { 6 | public FLOAD_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | float val = frame.getLocalVars().getFloat(this.index); 14 | frame.getOperandStack().pushFloat(val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ILOAD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class ILOAD extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getLocalVars().getInt(this.index); 10 | frame.getOperandStack().pushInt(val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/loads/ILOAD_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.loads; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class ILOAD_N extends LOAD_N{ 6 | public ILOAD_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | int val = frame.getLocalVars().getInt(this.index); 14 | frame.getOperandStack().pushInt(val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/ADD/DADD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.ADD; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class DADD extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val2 = stack.popDouble(); 12 | double val1 = stack.popDouble(); 13 | double res = val1 + val2; 14 | stack.pushDouble(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/ADD/FADD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.ADD; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class FADD extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val2 = stack.popFloat(); 12 | float val1 = stack.popFloat(); 13 | float res = val1 + val2; 14 | stack.pushFloat(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/ADD/IADD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.ADD; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IADD extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | int res = val1 + val2; 14 | stack.pushInt(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/ADD/LADD.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.ADD; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class LADD extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val2 = stack.popLong(); 12 | long val1 = stack.popLong(); 13 | long res = val1 + val2; 14 | stack.pushLong(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/AND/IAND.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.AND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IAND extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | int res = val1 & val2; 14 | stack.pushInt(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/AND/LAND.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.AND; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class LAND extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | long val2 = stack.popLong(); 12 | long val1 = stack.popLong(); 13 | long res = val1 & val2; 14 | stack.pushLong(res); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/DIV/DDIV.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.DIV; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class DDIV extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | double val2 = stack.popDouble(); 12 | double val1 = stack.popDouble(); 13 | if(val2 == 0){ 14 | throw new ArithmeticException(); 15 | } else { 16 | double res = val1 / val2; 17 | stack.pushDouble(res); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/DIV/FDIV.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.DIV; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class FDIV extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | float val2 = stack.popFloat(); 12 | float val1 = stack.popFloat(); 13 | if(val2 == 0){ 14 | throw new ArithmeticException(); 15 | } else { 16 | float res = val1 / val2; 17 | stack.pushFloat(res); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/instructions/math/DIV/IDIV.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.math.DIV; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | 7 | public class IDIV extends NoOperandsInstruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | OperandStack stack = frame.getOperandStack(); 11 | int val2 = stack.popInt(); 12 | int val1 = stack.popInt(); 13 | if(val2 == 0){ 14 | throw new ArithmeticException(); 15 | } else { 16 | int res = val1 / val2; 17 | stack.pushInt(res); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/instructions/references/ANEWARRAY.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.references; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.heap.MyObject; 5 | import com.njuse.jvmfinal.memory.jclass.JClass; 6 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.RuntimeConstantPool; 7 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.ref.ClassRef; 8 | import com.njuse.jvmfinal.runtime.MyFrame; 9 | import com.njuse.jvmfinal.runtime.OperandStack; 10 | 11 | public class ANEWARRAY extends Index16Instruction { 12 | @Override 13 | public void execute(MyFrame frame) { 14 | RuntimeConstantPool rtcp = frame.getMethod().getClazz().getRuntimeConstantPool(); 15 | ClassRef classRef = (ClassRef) rtcp.getConstant(this.getIndex()); 16 | 17 | OperandStack stack = frame.getOperandStack(); 18 | int len = stack.popInt(); 19 | 20 | try { 21 | JClass jClass = classRef.resolveClass(); 22 | JClass arrayClass = jClass.ArrayRefClass(); 23 | MyObject arr = arrayClass.newArrayObject(len); 24 | stack.pushRef(arr); 25 | } catch (ClassNotFoundException e){ 26 | e.printStackTrace(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /jvm-final/instructions/references/ARRAY_LENGTH.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.references; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class ARRAY_LENGTH extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | MyObject arrRef = stack.popRef(); 14 | int len = ((ArrayObject) arrRef).length(); 15 | stack.pushInt(len); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/references/ATYPE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.references; 2 | 3 | public class ATYPE { 4 | public static final int ATYPE_BOOLEAN = 4; 5 | public static final int ATYPE_CHAR = 5; 6 | public static final int ATYPE_FLOAT = 6; 7 | public static final int ATYPE_DOUBLE = 7; 8 | public static final int ATYPE_BYTE = 8; 9 | public static final int ATYPE_SHORT = 9; 10 | public static final int ATYPE_INT = 10; 11 | public static final int ATYPE_LONG = 11; 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/references/CHECKCAST.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.references; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index16Instruction; 4 | import com.njuse.jvmfinal.memory.heap.MyObject; 5 | import com.njuse.jvmfinal.memory.jclass.JClass; 6 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.RuntimeConstantPool; 7 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.ref.ClassRef; 8 | import com.njuse.jvmfinal.runtime.MyFrame; 9 | import com.njuse.jvmfinal.runtime.OperandStack; 10 | 11 | public class CHECKCAST extends Index16Instruction { 12 | @Override 13 | public void execute(MyFrame frame) { 14 | OperandStack stack = frame.getOperandStack(); 15 | MyObject ref = stack.popRef(); 16 | stack.pushRef(ref); 17 | if(!ref.isNull()){ 18 | RuntimeConstantPool rtcp = frame.getMethod().getClazz().getRuntimeConstantPool(); 19 | ClassRef classRef = (ClassRef) rtcp.getConstant(this.getIndex()); 20 | try { 21 | JClass target = classRef.resolveClass(); 22 | if(!ref.isInstanceOf(target)) throw new ClassCastException(); 23 | } catch (ClassNotFoundException e){ 24 | e.printStackTrace(); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /jvm-final/instructions/stack/DUP.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stack; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | import com.njuse.jvmfinal.runtime.Slot; 7 | 8 | public class DUP extends NoOperandsInstruction { 9 | @Override 10 | public void execute(MyFrame frame) { 11 | OperandStack stack = frame.getOperandStack(); 12 | Slot slot = stack.popSlot(); 13 | stack.pushSlot(slot.clone()); 14 | stack.pushSlot(slot.clone()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/stack/DUP2.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stack; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.runtime.OperandStack; 6 | import com.njuse.jvmfinal.runtime.Slot; 7 | 8 | public class DUP2 extends NoOperandsInstruction { 9 | @Override 10 | public void execute(MyFrame frame) { 11 | OperandStack stack = frame.getOperandStack(); 12 | Slot slot1 = stack.popSlot(); 13 | Slot slot2 = stack.popSlot(); 14 | stack.pushSlot(slot2.clone()); 15 | stack.pushSlot(slot1.clone()); 16 | stack.pushSlot(slot2.clone()); 17 | stack.pushSlot(slot1.clone()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | 7 | public class ASTORE extends Index8Instruction { 8 | @Override 9 | public void execute(MyFrame frame) { 10 | MyObject ref = frame.getOperandStack().popRef(); 11 | frame.getLocalVars().setRef(this.index, ref); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ASTORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | import com.njuse.jvmfinal.memory.heap.MyObject; 5 | 6 | public class ASTORE_N extends STORE_N{ 7 | public ASTORE_N(int index){ 8 | checkIndex(index); 9 | this.index = index; 10 | } 11 | 12 | @Override 13 | public void execute(MyFrame frame) { 14 | MyObject ref = frame.getOperandStack().popRef(); 15 | frame.getLocalVars().setRef(this.index, ref); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/AASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class AASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | MyObject val = stack.popRef(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | MyObject[] arrs = ((ArrayObject) arrRef).getRefs(); 17 | arrs[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/BASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class BASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int val = stack.popInt(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | int[] bytes = ((ArrayObject) arrRef).getBytes(); 17 | bytes[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/CASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class CASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int val = stack.popInt(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | int[] chars = ((ArrayObject) arrRef).getChars(); 17 | chars[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/DASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class DASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | double val = stack.popDouble(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | double[] doubles = ((ArrayObject) arrRef).getDoubles(); 17 | doubles[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/FASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class FASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | float val = stack.popFloat(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | float[] floats = ((ArrayObject) arrRef).getFloats(); 17 | floats[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/IASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class IASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int val = stack.popInt(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | int[] ints = ((ArrayObject) arrRef).getInts(); 17 | ints[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/LASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class LASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | long val = stack.popLong(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | long[] longs = ((ArrayObject) arrRef).getLongs(); 17 | longs[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ArrayStores/SASTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores.ArrayStores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | import com.njuse.jvmfinal.memory.heap.ArrayObject; 5 | import com.njuse.jvmfinal.memory.heap.MyObject; 6 | import com.njuse.jvmfinal.runtime.MyFrame; 7 | import com.njuse.jvmfinal.runtime.OperandStack; 8 | 9 | public class SASTORE extends NoOperandsInstruction { 10 | @Override 11 | public void execute(MyFrame frame) { 12 | OperandStack stack = frame.getOperandStack(); 13 | int val = stack.popInt(); 14 | int index = stack.popInt(); 15 | MyObject arrRef = stack.popRef(); 16 | int[] shorts = ((ArrayObject) arrRef).getShorts(); 17 | shorts[index] = val; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/DSTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class DSTORE extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | double val = frame.getOperandStack().popDouble(); 10 | frame.getLocalVars().setDouble(this.index, val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/DSTORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class DSTORE_N extends STORE_N { 6 | public DSTORE_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | double val = frame.getOperandStack().popDouble(); 14 | frame.getLocalVars().setDouble(this.index, val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/FSTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class FSTORE extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | float val = frame.getOperandStack().popFloat(); 10 | frame.getLocalVars().setFloat(this.index, val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/FSTORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class FSTORE_N extends STORE_N { 6 | public FSTORE_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | float val = frame.getOperandStack().popFloat(); 14 | frame.getLocalVars().setFloat(this.index, val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ISTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class ISTORE extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | int val = frame.getOperandStack().popInt(); 10 | frame.getLocalVars().setInt(this.index, val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/ISTORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class ISTORE_N extends STORE_N{ 6 | public ISTORE_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | int val = frame.getOperandStack().popInt(); 14 | frame.getLocalVars().setInt(this.index, val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/LSTORE.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.Index8Instruction; 4 | import com.njuse.jvmfinal.runtime.MyFrame; 5 | 6 | public class LSTORE extends Index8Instruction { 7 | @Override 8 | public void execute(MyFrame frame) { 9 | long val = frame.getOperandStack().popLong(); 10 | frame.getLocalVars().setLong(this.index, val); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/LSTORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.runtime.MyFrame; 4 | 5 | public class LSTORE_N extends STORE_N { 6 | public LSTORE_N(int index){ 7 | checkIndex(index); 8 | this.index = index; 9 | } 10 | 11 | @Override 12 | public void execute(MyFrame frame) { 13 | long val = frame.getOperandStack().popLong(); 14 | frame.getLocalVars().setLong(this.index, val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /jvm-final/instructions/stores/STORE_N.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.instructions.stores; 2 | 3 | import com.njuse.jvmfinal.instructions.base.NoOperandsInstruction; 4 | 5 | public abstract class STORE_N extends NoOperandsInstruction { 6 | public int index; 7 | private static int[] valid = new int[]{0, 1, 2, 3}; 8 | 9 | public static void checkIndex(int i) { 10 | assert i >= valid[0] && i <= valid[valid.length - 1]; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/memory/heap/MyObject.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.heap; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.JClass; 4 | import com.njuse.jvmfinal.runtime.Slot; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.ArrayList; 9 | 10 | @Getter 11 | @Setter 12 | public abstract class MyObject { 13 | protected JClass jClass; 14 | protected Object data; 15 | protected boolean isNull; 16 | protected boolean isArray = false; 17 | 18 | public MyObject(){ 19 | } 20 | 21 | public boolean isInstanceOf(JClass target) throws ClassNotFoundException { 22 | return target.isAssignableFrom(this.jClass); 23 | } 24 | 25 | public Vars getFields(){ 26 | return (Vars) data; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /jvm-final/memory/heap/NonArrayObject.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.heap; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.JClass; 4 | 5 | public class NonArrayObject extends MyObject { 6 | public NonArrayObject(JClass jClass){ 7 | this.jClass = jClass; 8 | this.data = (new Vars(jClass.getInstanceSlotCount())); 9 | this.isNull = false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jvm-final/memory/heap/NullObject.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.heap; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.JClass; 4 | 5 | public class NullObject extends MyObject { 6 | public NullObject(){ 7 | this.isNull = true; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/Field.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.FieldInfo; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class Field extends ClassMember { 10 | private int slotID; 11 | private int constValueIndex; 12 | 13 | public Field(FieldInfo info, JClass clazz) { 14 | this.clazz = clazz; 15 | accessFlags = info.getAccessFlags(); 16 | name = info.getName(); 17 | descriptor = info.getDescriptor(); 18 | if (info.getConstantValueAttr() != null) { 19 | constValueIndex = info.getConstantValueAttr().getConstantValueIndex(); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/Constant.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant; 2 | 3 | public interface Constant { 4 | } 5 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/other/InvokeDynamic.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.other; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | 5 | public class InvokeDynamic implements Constant { 6 | } 7 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/other/MethodHandle.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.other; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | 5 | public class MethodHandle implements Constant { 6 | } 7 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/other/MethodType.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.other; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | 5 | public class MethodType implements Constant { 6 | } 7 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/other/NameAndType.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.other; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.info.NameAndTypeInfo; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 5 | 6 | public class NameAndType implements Constant { 7 | private int nameIndex; 8 | private int descriptorIndex; 9 | 10 | public NameAndType(NameAndTypeInfo info) { 11 | this.nameIndex = info.getNameIndex(); 12 | this.descriptorIndex = info.getDescriptorIndex(); 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "NameAndType : nameIndex = " + nameIndex + " descriptorIndex = " + descriptorIndex; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/other/UTF8.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.other; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.info.UTF8Info; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 5 | 6 | public class UTF8 implements Constant { 7 | private int length; 8 | private byte[] bytes; 9 | private String myString; 10 | 11 | public UTF8(UTF8Info info) { 12 | this.length = info.getLength(); 13 | this.bytes = info.getBytes(); 14 | this.myString = info.getMyString(); 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "UTF8 " + myString; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/ref/ClassRef.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.ref; 2 | 3 | import com.njuse.jvmfinal.classloader.classfileparser.constantpool.info.ClassInfo; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.RuntimeConstantPool; 5 | 6 | public class ClassRef extends SymRef { 7 | public ClassRef(RuntimeConstantPool runtimeConstantPool, ClassInfo classInfo) { 8 | this.runtimeConstantPool = runtimeConstantPool; 9 | this.className = classInfo.getClassName(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/ref/SymRef.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.ref; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.JClass; 4 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.RuntimeConstantPool; 5 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import org.omg.CORBA.PUBLIC_MEMBER; 9 | 10 | @Getter 11 | @Setter 12 | public abstract class SymRef implements Constant { 13 | public RuntimeConstantPool runtimeConstantPool; 14 | public String className; //format : java/lang/Object 15 | public JClass clazz; 16 | 17 | public JClass resolveClass() throws ClassNotFoundException { 18 | if(this.clazz == null){ 19 | this.resolveClassRef(); 20 | } 21 | return this.clazz; 22 | } 23 | 24 | public void resolveClassRef() throws ClassNotFoundException { 25 | JClass D = this.runtimeConstantPool.getClazz(); 26 | JClass C = D.getClassLoader().loadClass(this.className); 27 | if (!C.isAccessibleTo(D)) throw new IllegalAccessError(); 28 | this.clazz = C; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/wrapper/DoubleWrapper.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class DoubleWrapper implements Constant { 10 | private double value; 11 | 12 | public DoubleWrapper(double value) { 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "double " + value; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/wrapper/FloatWrapper.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class FloatWrapper implements Constant { 10 | private float value; 11 | 12 | public FloatWrapper(float value) { 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "float " + value; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/wrapper/IntWrapper.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class IntWrapper implements Constant { 10 | private int value; 11 | 12 | public IntWrapper(int value) { 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "int " + value; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/wrapper/LongWrapper.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | public class LongWrapper implements Constant { 10 | private long value; 11 | 12 | public LongWrapper(long value) { 13 | this.value = value; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "long " + value; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /jvm-final/memory/jclass/runtimeConstantPool/constant/wrapper/StringWrapper.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | //not support String class in current version 8 | @Getter 9 | @Setter 10 | public class StringWrapper implements Constant { 11 | private String value; 12 | 13 | public StringWrapper(String value) { 14 | this.value = value; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "String " + value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /jvm-final/readme: -------------------------------------------------------------------------------- 1 | JVM-FINAL 2 | -------------------------------------------------------------------------------- /jvm-final/runtime/MyFrame.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.runtime; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.Method; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Setter 8 | @Getter 9 | public class MyFrame { 10 | private MyFrame lower; 11 | private LocalVars localVars; 12 | private OperandStack operandStack; 13 | private MyThread thread; 14 | private Method method; 15 | private int nextPC; 16 | 17 | public MyFrame(MyThread thread, Method method){ 18 | this.thread = thread; 19 | this.method = method; 20 | this.localVars = new LocalVars(method.getMaxLocal()); 21 | this.operandStack = new OperandStack(method.getMaxStack()); 22 | } 23 | 24 | public MyFrame(MyThread thread, Method method, int maxStack, int maxLocal){ 25 | this.thread = thread; 26 | this.method = method; 27 | this.localVars = new LocalVars(maxLocal); 28 | this.operandStack = new OperandStack(maxStack); 29 | } 30 | 31 | public void revertPC(){ 32 | this.nextPC = this.thread.getPC(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /jvm-final/runtime/MyThread.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.runtime; 2 | 3 | import com.njuse.jvmfinal.memory.jclass.Method; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Setter 8 | @Getter 9 | public class MyThread { 10 | private int PC; 11 | private ThreadStack threadStack; 12 | 13 | public MyThread(){ 14 | this.threadStack = new ThreadStack(); 15 | } 16 | 17 | public int getPC(){ 18 | return this.PC; 19 | } 20 | 21 | public void setPC(int pc){ 22 | this.PC = pc; 23 | } 24 | 25 | public void pushFrame(MyFrame frame){ 26 | this.threadStack.push(frame); 27 | } 28 | 29 | public MyFrame popFrame(){ 30 | return this.threadStack.pop(); 31 | } 32 | 33 | public MyFrame getTopFrame(){ 34 | return this.threadStack.getTop(); 35 | } 36 | 37 | public MyFrame getCurrentFrame() { 38 | return this.threadStack.getTop(); 39 | } 40 | 41 | public MyFrame newFrame(Method method){ 42 | return new MyFrame(this, method); 43 | } 44 | 45 | public boolean isStackEmpty(){ 46 | return this.threadStack.isEmpty(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /jvm-final/runtime/Slot.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.runtime; 2 | 3 | import com.njuse.jvmfinal.memory.heap.MyObject; 4 | 5 | public class Slot { 6 | private int val; 7 | private MyObject ref; 8 | 9 | public int getVal() { 10 | return val; 11 | } 12 | 13 | public void setVal(int val) { 14 | this.val = val; 15 | } 16 | 17 | public MyObject getRef(){ 18 | return ref; 19 | } 20 | 21 | public void setRef(MyObject ref){ 22 | this.ref = ref; 23 | } 24 | 25 | public Slot clone(){ 26 | Slot newSlot = new Slot(); 27 | newSlot.val = this.val; 28 | newSlot.ref = this.ref; 29 | return newSlot; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /jvm-final/runtime/ThreadStack.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.runtime; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import java.util.EmptyStackException; 7 | 8 | @Getter 9 | @Setter 10 | public class ThreadStack { 11 | private static int maxSize = 1024; 12 | private int size; 13 | private MyFrame top; 14 | 15 | public ThreadStack(){ 16 | } 17 | 18 | public void push(MyFrame frame){ 19 | if(this.size >= maxSize) throw new StackOverflowError(); 20 | if( this.top != null ){ 21 | frame.setLower(this.top); 22 | } 23 | this.top = frame; 24 | this.size++; 25 | } 26 | 27 | public MyFrame pop(){ 28 | if( this.top == null ) throw new EmptyStackException(); 29 | MyFrame top = this.top; 30 | this.top = top.getLower(); 31 | top.setLower(null); 32 | this.size--; 33 | return top; 34 | } 35 | 36 | public MyFrame getTop(){ 37 | if( this.top == null ) throw new EmptyStackException(); 38 | return top; 39 | } 40 | 41 | public boolean isEmpty(){ 42 | return this.top == null; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /jvm-final/util/IOUtil.java: -------------------------------------------------------------------------------- 1 | package com.njuse.jvmfinal.util; 2 | 3 | import java.io.*; 4 | 5 | public class IOUtil { 6 | public static byte[] readFileByBytes(InputStream is) throws IOException { 7 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { 8 | BufferedInputStream in; 9 | in = new BufferedInputStream(is); 10 | int buf_size = 1024; 11 | byte[] buffer = new byte[buf_size]; 12 | int len; 13 | while (-1 != (len = in.read(buffer, 0, buf_size))) { 14 | bos.write(buffer, 0, len); 15 | } 16 | return bos.toByteArray(); 17 | } catch (IOException e) { 18 | e.printStackTrace(); 19 | throw e; 20 | } 21 | } 22 | 23 | 24 | public static String transform(String pathName) { 25 | if (pathName.contains("/")) { 26 | return pathName.replace("/", File.separator); 27 | } 28 | return pathName; 29 | } 30 | } 31 | --------------------------------------------------------------------------------