1 package com.xtremelabs.robolectric.shadows; 2 3 import android.accounts.Account; 4 import android.os.Parcel; 5 import android.text.TextUtils; 6 import com.xtremelabs.robolectric.internal.Implementation; 7 import com.xtremelabs.robolectric.internal.Implements; 8 import com.xtremelabs.robolectric.internal.RealObject; 9 10 import java.lang.reflect.Field; 11 12 @Implements(Account.class) 13 public class ShadowAccount { 14 @RealObject 15 private Account realObject; 16 __constructor__(String name, String type)17 public void __constructor__(String name, String type) throws Exception { 18 set(name, type); 19 } 20 __constructor__(Parcel parcel)21 public void __constructor__(Parcel parcel) throws Exception { 22 set(parcel.readString(), parcel.readString()); 23 } 24 25 @Implementation toString()26 public String toString() { 27 return "Account {name=" + realObject.name + ", type=" + realObject.type + "}"; 28 } 29 set(String name, String type)30 private void set(String name, String type) throws Exception { 31 if (TextUtils.isEmpty(name) || TextUtils.isEmpty(type)) throw new IllegalArgumentException(); 32 33 Field nameF = realObject.getClass().getField("name"); 34 nameF.setAccessible(true); 35 nameF.set(realObject, name); 36 37 Field typeF = realObject.getClass().getField("type"); 38 typeF.setAccessible(true); 39 typeF.set(realObject, type); 40 } 41 42 @Implementation equals(Object o)43 public boolean equals(Object o) { 44 if (o == this) return true; 45 if (!(o instanceof Account)) return false; 46 final Account other = (Account)o; 47 return realObject.name.equals(other.name) && realObject.type.equals(other.type); 48 } 49 50 @Implementation hashCode()51 public int hashCode() { 52 int result = 17; 53 result = 31 * result + realObject.name.hashCode(); 54 result = 31 * result + realObject.type.hashCode(); 55 return result; 56 } 57 } 58