private void addGetId ( TypeSpec . Builder classBuilder ) { MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "__getStubID" ) . addModifiers ( Modifier . PRIVATE ) . returns ( int . class ) . addStatement ( "android.os.Parcel data = android.os.Parcel.obtain()" ) . addStatement ( "android.os.Parcel reply = android.os.Parcel.obtain()" ) . addStatement ( "int result" ) ; methodBuilder . beginControlFlow ( "try" ) ; //write the descriptor methodBuilder . addStatement ( "data.writeInterfaceToken(DESCRIPTOR)" ) ; methodBuilder . addStatement ( "mRemote.transact(TRANSACTION__getStubID, data, reply, 0)" ) ; //read exception if any methodBuilder . addStatement ( "Throwable exception = checkException(reply)" ) ; methodBuilder . beginControlFlow ( "if(exception != null)" ) ; methodBuilder . addStatement ( "throw ($T)exception" , RuntimeException . class ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addStatement ( "result = reply.readInt()" ) ; //end of try methodBuilder . endControlFlow ( ) ; //catch rethrow methodBuilder . beginControlFlow ( "catch ($T re)" , ClassName . get ( "android.os" , "RemoteException" ) ) ; methodBuilder . addStatement ( "throw new $T(re)" , RuntimeException . class ) ; methodBuilder . endControlFlow ( ) ; //finally block methodBuilder . beginControlFlow ( "finally" ) ; methodBuilder . addStatement ( "reply.recycle()" ) ; methodBuilder . addStatement ( "data.recycle()" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addStatement ( "return result" ) ; classBuilder . addMethod ( methodBuilder . build ( ) ) ; }
Add proxy method to get unique id