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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-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. protected void setUp() throws Exception {
  22. super.setUp();
  23. }
  24. /**
  25. * Verify for an inner class declared inside the 'main' method that the enclosing method
  26. * 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,
  34. encMethodAttrs.length==1);
  35. EnclosingMethod em = (EnclosingMethod)encMethodAttrs[0];
  36. String enclosingClassName = em.getEnclosingClass().getBytes(pool);
  37. String enclosingMethodName= em.getEnclosingMethod().getName(pool);
  38. assertTrue("Expected class name to be 'AttributeTestClassEM01' but was "+enclosingClassName,
  39. enclosingClassName.equals("AttributeTestClassEM01"));
  40. assertTrue("Expected method name to be 'main' but was "+enclosingMethodName,
  41. enclosingMethodName.equals("main"));
  42. }
  43. /**
  44. * Verify for an inner class declared at the type level that the EnclosingMethod attribute
  45. * is set correctly (i.e. to a null value)
  46. */
  47. public void testCheckClassLevelNamedInnerClass() throws ClassNotFoundException {
  48. SyntheticRepository repos = createRepos("testcode.jar");
  49. JavaClass clazz = repos.loadClass("AttributeTestClassEM02$1");
  50. ConstantPool pool = clazz.getConstantPool();
  51. Attribute[] encMethodAttrs = findAttribute("EnclosingMethod",clazz);
  52. assertTrue("Expected 1 EnclosingMethod attribute but found "+encMethodAttrs.length,
  53. encMethodAttrs.length==1);
  54. EnclosingMethod em = (EnclosingMethod)encMethodAttrs[0];
  55. String enclosingClassName = em.getEnclosingClass().getBytes(pool);
  56. assertTrue("The class is not within a method, so method_index should be null, but it is "+
  57. em.getEnclosingMethodIndex(),em.getEnclosingMethodIndex() == 0);
  58. assertTrue("Expected class name to be 'AttributeTestClassEM02' but was "+enclosingClassName,
  59. enclosingClassName.equals("AttributeTestClassEM02"));
  60. }
  61. /**
  62. * Check that we can save and load the attribute correctly.
  63. */
  64. public void testAttributeSerializtion() throws ClassNotFoundException,IOException {
  65. // Read in the class
  66. SyntheticRepository repos = createRepos("testcode.jar");
  67. JavaClass clazz = repos.loadClass("AttributeTestClassEM02$1");
  68. ConstantPool pool = clazz.getConstantPool();
  69. Attribute[] encMethodAttrs = findAttribute("EnclosingMethod",clazz);
  70. assertTrue("Expected 1 EnclosingMethod attribute but found "+encMethodAttrs.length,
  71. encMethodAttrs.length==1);
  72. // Write it out
  73. File tfile = createTestdataFile("AttributeTestClassEM02$1.class");
  74. clazz.dump(tfile);
  75. // Read in the new version and check it is OK
  76. SyntheticRepository repos2 = createRepos(".");
  77. JavaClass clazz2 = repos2.loadClass("AttributeTestClassEM02$1");
  78. EnclosingMethod em = (EnclosingMethod)encMethodAttrs[0];
  79. String enclosingClassName = em.getEnclosingClass().getBytes(pool);
  80. assertTrue("The class is not within a method, so method_index should be null, but it is "+
  81. em.getEnclosingMethodIndex(),em.getEnclosingMethodIndex() == 0);
  82. assertTrue("Expected class name to be 'AttributeTestClassEM02' but was "+enclosingClassName,
  83. enclosingClassName.equals("AttributeTestClassEM02"));
  84. assertTrue(tfile.delete());
  85. }
  86. protected void tearDown() throws Exception {
  87. super.tearDown();
  88. }
  89. }