Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AsmDelegateTests5.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* *******************************************************************
  2. * Copyright (c) 2006 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * Matthew Webster move Java 5 tests
  12. * ******************************************************************/
  13. package org.aspectj.weaver.bcel;
  14. import junit.framework.Test;
  15. import junit.framework.TestSuite;
  16. import org.aspectj.weaver.AbstractWorldTestCase;
  17. import org.aspectj.weaver.BcweaverTests;
  18. import org.aspectj.weaver.ResolvedMember;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.TypeVariable;
  21. import org.aspectj.weaver.UnresolvedType;
  22. import org.aspectj.weaver.World;
  23. /**
  24. * This is a test case for the nameType parts of worlds.
  25. */
  26. public class AsmDelegateTests5 extends AbstractWorldTestCase {
  27. public static Test suite() {
  28. TestSuite suite = new TestSuite(AsmDelegateTests5.class.getName());
  29. suite.addTestSuite(AsmDelegateTests5.class);
  30. return suite;
  31. }
  32. private final BcelWorld world = new BcelWorld(BcweaverTests.TESTDATA_PATH+"/forAsmDelegateTesting/stuff.jar");
  33. public AsmDelegateTests5(String name) {
  34. super(name);
  35. }
  36. protected World getWorld() {
  37. return world;
  38. }
  39. // --- testcode
  40. /**
  41. * Methods are transformed according to generic signatures - this checks
  42. * that some of the generic methods in java.lang.Class appear the same
  43. * whether viewed through an ASM or a BCEL delegate.
  44. */
  45. public void testCompareGenericMethods() {
  46. BcelWorld slowWorld = new BcelWorld();
  47. slowWorld.setFastDelegateSupport(false);
  48. slowWorld.setBehaveInJava5Way(true);
  49. BcelWorld fastWorld = new BcelWorld();
  50. fastWorld.setBehaveInJava5Way(true);
  51. ResolvedType bcelJavaLangClass = slowWorld.resolve(UnresolvedType.forName("java.lang.Class"));
  52. ResolvedType asmJavaLangClass = fastWorld.resolve(UnresolvedType.forName("java.lang.Class"));
  53. bcelJavaLangClass = bcelJavaLangClass.getGenericType();
  54. asmJavaLangClass = asmJavaLangClass.getGenericType();
  55. ResolvedMember[] bcelMethods = bcelJavaLangClass.getDeclaredMethods();
  56. ResolvedMember[] asmMethods = asmJavaLangClass.getDeclaredMethods();
  57. for (int i = 0; i < bcelMethods.length; i++) {
  58. bcelMethods[i].setParameterNames(null); // forget them, asm delegates dont currently know them
  59. String one = bcelMethods[i].toDebugString();
  60. String two = asmMethods[i].toDebugString();
  61. if (!one.equals(two)) {
  62. fail("These methods look different when viewed through ASM or BCEL\nBCEL='"+bcelMethods[i].toDebugString()+
  63. "'\n ASM='"+asmMethods[i].toDebugString()+"'");
  64. }
  65. // If one is parameterized, check the other is...
  66. if (bcelMethods[i].canBeParameterized()) {
  67. assertTrue("ASM method '"+asmMethods[i].toDebugString()+"' can't be parameterized whereas its' BCEL variant could",
  68. asmMethods[i].canBeParameterized());
  69. }
  70. }
  71. // Let's take a special look at:
  72. // public <U> Class<? extends U> asSubclass(Class<U> clazz)
  73. ResolvedMember bcelSubclassMethod = null;
  74. for (int i = 0; i < bcelMethods.length; i++) {
  75. if (bcelMethods[i].getName().equals("asSubclass")) { bcelSubclassMethod = bcelMethods[i]; break; }
  76. }
  77. ResolvedMember asmSubclassMethod = null;
  78. for (int i = 0; i < asmMethods.length; i++) {
  79. if (asmMethods[i].getName().equals("asSubclass")) { asmSubclassMethod = asmMethods[i];break; }
  80. }
  81. TypeVariable[] tvs = bcelSubclassMethod.getTypeVariables();
  82. assertTrue("should have one type variable on the bcel version but found: "+format(tvs),tvs!=null && tvs.length==1);
  83. tvs = asmSubclassMethod.getTypeVariables();
  84. assertTrue("should have one type variable on the asm version but found: "+format(tvs),tvs!=null && tvs.length==1);
  85. }
  86. private String format(TypeVariable[] tvs) {
  87. if (tvs==null) return "null";
  88. StringBuffer s = new StringBuffer();
  89. s.append("[");
  90. for (int i = 0; i < tvs.length; i++) {
  91. s.append(tvs[i]);
  92. if ((i+1)<tvs.length) s.append(",");
  93. }
  94. s.append("]");
  95. return s.toString();
  96. }
  97. public void testCompareGenericFields() {
  98. BcelWorld slowWorld = new BcelWorld();
  99. slowWorld.setFastDelegateSupport(false);
  100. slowWorld.setBehaveInJava5Way(true);
  101. BcelWorld fastWorld = new BcelWorld();
  102. fastWorld.setBehaveInJava5Way(true);
  103. ResolvedType bcelJavaLangClass = slowWorld.resolve(UnresolvedType.forName("java.lang.Class"));
  104. ResolvedType asmJavaLangClass = fastWorld.resolve(UnresolvedType.forName("java.lang.Class"));
  105. bcelJavaLangClass = bcelJavaLangClass.getGenericType();
  106. asmJavaLangClass = asmJavaLangClass.getGenericType();
  107. ResolvedMember[] bcelFields = bcelJavaLangClass.getDeclaredFields();
  108. ResolvedMember[] asmFields = asmJavaLangClass.getDeclaredFields();
  109. for (int i = 0; i < bcelFields.length; i++) {
  110. UnresolvedType bcelFieldType = bcelFields[i].getGenericReturnType();
  111. UnresolvedType asmFieldType = asmFields[i].getGenericReturnType();
  112. if (!bcelFields[i].getGenericReturnType().toDebugString().equals(asmFields[i].getGenericReturnType().toDebugString())) {
  113. fail("These fields look different when viewed through ASM or BCEL\nBCEL='"+bcelFieldType.toDebugString()+
  114. "'\n ASM='"+asmFieldType.toDebugString()+"'");
  115. }
  116. }
  117. }
  118. }