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.

CompileWithReleaseTests.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*******************************************************************************
  2. * Copyright (c) 2021 Contributors
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *******************************************************************************/
  8. package org.aspectj.systemtest.ajc198;
  9. import junit.framework.Test;
  10. import org.aspectj.apache.bcel.Constants;
  11. import org.aspectj.apache.bcel.classfile.JavaClass;
  12. import org.aspectj.apache.bcel.classfile.Method;
  13. import org.aspectj.testing.JavaVersionSpecificXMLBasedAjcTestCase;
  14. import org.aspectj.testing.XMLBasedAjcTestCase;
  15. import java.util.Objects;
  16. /**
  17. * @author Alexander Kriegisch
  18. */
  19. public class CompileWithReleaseTests extends JavaVersionSpecificXMLBasedAjcTestCase {
  20. public CompileWithReleaseTests() {
  21. super(9);
  22. }
  23. /**
  24. * In order to avoid a complicated test involving two different JDKs (9+ for compilation, 8 for runtime), we inspect
  25. * the byte code of test class {@code Buffers} with BCEL, simply grepping on the disassembled byte code. If compiled
  26. * correctly with {@code --release 8}, the byte code should contain the equivalent of a {@code Buffer.flip()} call,
  27. * not a {@code ByteBuffer.flip()} one.
  28. */
  29. public void testCompileToOlderJDKRelease() {
  30. runTest("compile to older JDK release");
  31. // Check compiled byte code version
  32. String className = "Buffers";
  33. checkVersion(className, Constants.ClassFileVersion.of(8).MAJOR, Constants.ClassFileVersion.of(8).MINOR);
  34. // Disassemble method and check if Java 8 API is used as expected
  35. JavaClass javaClass;
  36. try {
  37. javaClass = getClassFrom(ajc.getSandboxDirectory(), className);
  38. }
  39. catch (ClassNotFoundException e) {
  40. throw new IllegalStateException("Cannot find class " + className, e);
  41. }
  42. Method method = Objects.requireNonNull(getMethodFromClass(javaClass, "flip"));
  43. String disassembledMethod = method.getCode().toString();
  44. final String JAVA8_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/Buffer;";
  45. final String JAVA9_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/ByteBuffer;";
  46. if (disassembledMethod.contains(JAVA9_API_CALL))
  47. fail(
  48. "Class '" + className + "' was compiled against Java 9+ API. " +
  49. "There seems to be a problem with the '--release' compiler option.\n" +
  50. "Disassembled method:\n" + disassembledMethod
  51. );
  52. else if (!disassembledMethod.contains(JAVA8_API_CALL))
  53. fail(
  54. "Cannot determine if class '" + className + "' was compiled against Java 8 or 9+ API. " +
  55. "This should never happen.\n" +
  56. "Disassembled method:\n" + disassembledMethod
  57. );
  58. }
  59. public static Test suite() {
  60. return XMLBasedAjcTestCase.loadSuite(CompileWithReleaseTests.class);
  61. }
  62. @Override
  63. protected java.net.URL getSpecFile() {
  64. return getClassResource("ajc198.xml");
  65. }
  66. }