Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ArrayCloning.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. public class ArrayCloning {
  2. public static void main(String[] args) {
  3. ArrayCloning ArrayCloning = new ArrayCloning();
  4. Integer[] clonedStaticField = ArrayCloning.clone1();
  5. checkIdentical(clonedStaticField,ArrayCloning.staticField);
  6. Integer[] clonedField = ArrayCloning.clone2();
  7. checkIdentical(clonedField,ArrayCloning.nonStaticField);
  8. Integer[] clown = null;
  9. clown = ArrayCloning.clone3();
  10. clown = ArrayCloning.clone4();
  11. Integer[][] ArrayCloningArrayCloning = ArrayCloning.clone5();
  12. }
  13. public static void checkIdentical(Integer[] one, Integer[] two) {
  14. if (one[0]!=two[0]) throw new RuntimeException("Not the same (a)");
  15. if (one[1]!=two[1]) throw new RuntimeException("Not the same (b)");
  16. }
  17. private static Integer[] staticField = new Integer[2];
  18. private Integer[] nonStaticField = new Integer[2];
  19. public ArrayCloning() {
  20. nonStaticField[0] = new Integer(32);
  21. nonStaticField[1] = new Integer(64);
  22. }
  23. static {
  24. staticField[0] = new Integer(1);
  25. staticField[1] = new Integer(2);
  26. }
  27. public Integer[] clone1() {
  28. System.err.println("Clone call on a static field");
  29. return (Integer[])staticField.clone();
  30. }
  31. public Integer[] clone2() {
  32. System.err.println("Clone call on a non-static field");
  33. return (Integer[])nonStaticField.clone();
  34. }
  35. public Integer[] clone3() {
  36. System.err.println("Clone call on a local variable");
  37. Integer[] ArrayCloningArrayCloning = staticField;
  38. return (Integer[])ArrayCloningArrayCloning.clone();
  39. }
  40. // Clone call on anonymous 'thing' !
  41. public Integer[] clone4() {
  42. System.err.println("Clone call on a 1 dimensional anonymous integer array");
  43. return (Integer[])new Integer[5].clone();
  44. }
  45. // Sick
  46. public Integer[][] clone5() {
  47. System.err.println("Clone call on a 2 dimensional anonymous integer array");
  48. return (Integer[][])new Integer[5][3].clone();
  49. }
  50. }
  51. aspect HelloWorldAspect {
  52. Object[] around(): call(* java.lang.Object.clone()) && within(ArrayCloning) {
  53. Object[] ret = proceed();
  54. return (Object[])ret.clone();
  55. }
  56. }