You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EnclosingMethodAttributeTest.java 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  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. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.tests;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import org.aspectj.apache.bcel.classfile.Attribute;
  16. import org.aspectj.apache.bcel.classfile.ConstantPool;
  17. import org.aspectj.apache.bcel.classfile.EnclosingMethod;
  18. import org.aspectj.apache.bcel.classfile.JavaClass;
  19. import org.aspectj.apache.bcel.util.SyntheticRepository;
  20. public class EnclosingMethodAttributeTest extends BcelTestCase {
  21. @Override
  22. protected void setUp() throws Exception {
  23. super.setUp();
  24. }
  25. /**
  26. * Verify for an inner class declared inside the 'main' method that the enclosing method attribute is set correctly.
  27. */
  28. public void testCheckMethodLevelNamedInnerClass() throws ClassNotFoundException {
  29. SyntheticRepository repos = createRepos("testcode.jar");
  30. JavaClass clazz = repos.loadClass("AttributeTestClassEM01$1S");
  31. ConstantPool pool = clazz.getConstantPool();
  32. Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
  33. assertTrue("Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length, encMethodAttrs.length == 1);
  34. EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
  35. String enclosingClassName = em.getEnclosingClass().getClassname(pool);
  36. String enclosingMethodName = em.getEnclosingMethod().getName(pool);
  37. assertTrue("Expected class name to be 'AttributeTestClassEM01' but was " + enclosingClassName, enclosingClassName
  38. .equals("AttributeTestClassEM01"));
  39. assertTrue("Expected method name to be 'main' but was " + enclosingMethodName, enclosingMethodName.equals("main"));
  40. }
  41. /**
  42. * Verify for an inner class declared at the type level that the EnclosingMethod attribute is set correctly (i.e. to a null
  43. * value)
  44. */
  45. public void testCheckClassLevelNamedInnerClass() throws ClassNotFoundException {
  46. SyntheticRepository repos = createRepos("testcode.jar");
  47. JavaClass clazz = repos.loadClass("AttributeTestClassEM02$1");
  48. ConstantPool pool = clazz.getConstantPool();
  49. Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
  50. assertTrue("Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length, encMethodAttrs.length == 1);
  51. EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
  52. String enclosingClassName = em.getEnclosingClass().getClassname(pool);
  53. assertTrue("The class is not within a method, so method_index should be null, but it is " + em.getEnclosingMethodIndex(),
  54. em.getEnclosingMethodIndex() == 0);
  55. assertTrue("Expected class name to be 'AttributeTestClassEM02' but was " + enclosingClassName, enclosingClassName
  56. .equals("AttributeTestClassEM02"));
  57. }
  58. /**
  59. * Check that we can save and load the attribute correctly.
  60. */
  61. public void testAttributeSerializtion() throws ClassNotFoundException, IOException {
  62. // Read in the class
  63. SyntheticRepository repos = createRepos("testcode.jar");
  64. JavaClass clazz = repos.loadClass("AttributeTestClassEM02$1");
  65. ConstantPool pool = clazz.getConstantPool();
  66. Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz);
  67. assertTrue("Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length, encMethodAttrs.length == 1);
  68. // Write it out
  69. File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
  70. clazz.dump(tfile);
  71. // Read in the new version and check it is OK
  72. SyntheticRepository repos2 = createRepos(".");
  73. JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1");
  74. EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0];
  75. String enclosingClassName = em.getEnclosingClass().getClassname(pool);
  76. assertTrue("The class is not within a method, so method_index should be null, but it is " + em.getEnclosingMethodIndex(),
  77. em.getEnclosingMethodIndex() == 0);
  78. assertTrue("Expected class name to be 'AttributeTestClassEM02' but was " + enclosingClassName, enclosingClassName
  79. .equals("AttributeTestClassEM02"));
  80. assertTrue(tfile.delete());
  81. }
  82. @Override
  83. protected void tearDown() throws Exception {
  84. super.tearDown();
  85. }
  86. }