public void addStubMethods ( TypeSpec . Builder classBuilder ) { MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "onTransact" ) . addModifiers ( Modifier . PUBLIC ) . addAnnotation ( Override . class ) . returns ( boolean . class ) . addException ( ClassName . get ( "android.os" , "RemoteException" ) ) . addParameter ( int . class , "code" ) . addParameter ( ClassName . get ( "android.os" , "Parcel" ) , "data" ) . addParameter ( ClassName . get ( "android.os" , "Parcel" ) , "reply" ) . addParameter ( int . class , "flags" ) ; methodBuilder . beginControlFlow ( "try" ) ; methodBuilder . beginControlFlow ( "switch (code)" ) ; methodBuilder . beginControlFlow ( "case INTERFACE_TRANSACTION:" ) ; methodBuilder . addStatement ( "reply.writeString(DESCRIPTOR)" ) ; methodBuilder . addStatement ( "return true" ) ; methodBuilder . endControlFlow ( ) ; processRemoterElements ( classBuilder , new ElementVisitor ( ) { @ Override public void visitElement ( TypeSpec . Builder classBuilder , Element member , int methodIndex , MethodSpec . Builder methodBuilder ) { addStubMethods ( classBuilder , member , methodIndex , methodBuilder ) ; } } , methodBuilder ) ; methodBuilder . beginControlFlow ( "case TRANSACTION__getStubID:" ) ; methodBuilder . addStatement ( "data.enforceInterface(DESCRIPTOR)" ) ; methodBuilder . addStatement ( "reply.writeNoException()" ) ; methodBuilder . addStatement ( "reply.writeInt(serviceImpl.hashCode())" ) ; methodBuilder . addStatement ( "return true" ) ; methodBuilder . endControlFlow ( ) ; //end switch methodBuilder . endControlFlow ( ) ; //end of try methodBuilder . endControlFlow ( ) ; //catch rethrow methodBuilder . beginControlFlow ( "catch ($T re)" , Throwable . class ) ; methodBuilder . beginControlFlow ( "if ((flags & FLAG_ONEWAY) == 0)" ) ; methodBuilder . addStatement ( "reply.setDataPosition(0)" ) ; methodBuilder . addStatement ( "reply.writeInt(REMOTER_EXCEPTION_CODE)" ) ; methodBuilder . addStatement ( "reply.writeString(re.getMessage())" ) ; methodBuilder . addStatement ( "reply.writeSerializable(re)" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . beginControlFlow ( "else" ) ; methodBuilder . addStatement ( "$T.w(serviceImpl.getClass().getName(), \"Binder call failed.\", re)" , ClassName . get ( "android.util" , "Log" ) ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addStatement ( "return true" ) ; methodBuilder . endControlFlow ( ) ; methodBuilder . addStatement ( "return super.onTransact(code, data, reply, flags)" ) ; classBuilder . addMethod ( methodBuilder . build ( ) ) ; addCommonExtras ( classBuilder ) ; addStubExtras ( classBuilder ) ; }
Build the stub methods