Guest User

Untitled

a guest
Jan 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. public class MyParcelable implements Parcelable {
  2. private int mData;
  3.  
  4. public int describeContents() {
  5. return 0;
  6. }
  7.  
  8. public void writeToParcel(Parcel out, int flags) {
  9. out.writeInt(mData);
  10. }
  11.  
  12. public static final Parcelable.Creator<MyParcelable> CREATOR
  13. = new Parcelable.Creator<MyParcelable>() {
  14. public MyParcelable createFromParcel(Parcel in) {
  15. return new MyParcelable(in);
  16. }
  17.  
  18. public MyParcelable[] newArray(int size) {
  19. return new MyParcelable[size];
  20. }
  21. };
  22.  
  23. private MyParcelable(Parcel in) {
  24. mData = in.readInt();
  25. }
  26. }
  27.  
  28. public class ShushContainer<T extends Parcelable> implements Parcelable
  29.  
  30. public class ShushContainer<T extends Parcelable> implements Parcelable {
  31. Vector<T> items;
  32. String someField;
  33.  
  34. @Override
  35. public int describeContents() {
  36. return 0;
  37. }
  38.  
  39. @Override
  40. public void writeToParcel(Parcel dest, int flags) {
  41. dest.writeString(someField);
  42. dest.writeInt(items.size());
  43. for(int i=0;i<items.size();i++)
  44. items.get(i).writeToParcel(dest, flags);
  45. }
  46.  
  47. public ShushContainer(Parcel in) {
  48. someField = in.readString();
  49. int size = in.readInt();
  50. for(int i=0;i<size;i++)
  51. //this does not work
  52. items.add(T.CREATOR.createFromParcel(in));
  53. }
  54.  
  55. public static final Parcelable.Creator<ShushContainer> CREATOR = new Parcelable.Creator<ShushContainer>() {
  56. public ShushContainer createFromParcel(Parcel in) {
  57. return new ShushContainer(in);
  58. }
  59.  
  60. public ShushContainer[] newArray(int size) {
  61. return new ShushContainer[size];
  62. }
  63. };
  64. }
  65.  
  66. public class ShushContainer<T extends ShushContainer<T> & Parcelable>
  67. implements Parcelable
  68.  
  69. class Sample<T extends Sample<T> & Serializable> implements Serializable {
  70.  
  71. public static int CONST = 0;
  72.  
  73. public void foo()
  74. {
  75. T.CONST = 5;
  76. }
  77. }
  78.  
  79. public class Base {
  80. public static int count = 10;
  81. }
  82.  
  83. public class Child extends Base {
  84. public static int count = 20;
  85. }
  86.  
  87.  
  88. class Sample<T extends Base> {
  89. T t = null;
  90. public void printCount() {
  91. System.out.println(T.count);
  92. }
  93. public Sample(T t) {
  94. this.t = t;
  95. }
  96.  
  97. public static void main(String[] args) {
  98. Sample<Child> sample = new Sample<Child>(new Child());
  99. Sample<Base> sample1 = new Sample<Base>(new Base());
  100. sample.printCount();//Child value printed as 10
  101. sample1.printCount();//Same for parent value printed as 10
  102. }
  103.  
  104. }
  105.  
  106. class Sample<T extends Serializable> implements Serializable {
  107.  
  108. public int abc = 0;
  109. public void foo() {
  110. }
  111. public static void main(String[] args) throws SecurityException, NoSuchFieldException {
  112. TypeToken<Sample<Integer>> token = new TypeToken<Sample<Integer>>() {
  113. };
  114. Class<?> t = token.getRawType();
  115. Field field = t.getDeclaredField("abc");
  116. field.setAccessible(true);
  117. System.out.println(field.getName());
  118.  
  119. }
  120. }
Add Comment
Please, Sign In to add comment