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 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.XMLBasedAjcTestCase;
  14. import org.aspectj.testing.XMLBasedAjcTestCaseForJava9OrLater;
  15. import java.util.Objects;
  16. /**
  17. * @author Alexander Kriegisch
  18. */
  19. public class CompileWithReleaseTests extends XMLBasedAjcTestCaseForJava9OrLater {
  20. /**
  21. * In order to avoid a complicated test involving two different JDKs (9+ for compilation, 8 for runtime), we inspect
  22. * the byte code of test class {@code Buffers} with BCEL, simply grepping on the disassembled byte code. If compiled
  23. * correctly with {@code --release 8}, the byte code should contain the equivalent of a {@code Buffer.flip()} call,
  24. * not a {@code ByteBuffer.flip()} one.
  25. */
  26. public void testCompileToOlderJDKRelease() {
  27. runTest("compile to older JDK release");
  28. // Check compiled byte code version
  29. String className = "Buffers";
  30. checkVersion(className, Constants.MAJOR_1_8, Constants.MINOR_1_8);
  31. // Disassemble method and check if Java 8 API is used as expected
  32. JavaClass javaClass;
  33. try {
  34. javaClass = getClassFrom(ajc.getSandboxDirectory(), className);
  35. }
  36. catch (ClassNotFoundException e) {
  37. throw new IllegalStateException("Cannot find class " + className, e);
  38. }
  39. Method method = Objects.requireNonNull(getMethodFromClass(javaClass, "flip"));
  40. String disassembledMethod = method.getCode().toString();
  41. final String JAVA8_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/Buffer;";
  42. final String JAVA9_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/ByteBuffer;";
  43. if (disassembledMethod.contains(JAVA9_API_CALL))
  44. fail(
  45. "Class '" + className + "' was compiled against Java 9+ API. " +
  46. "There seems to be a problem with the '--release' compiler option.\n" +
  47. "Disassembled method:\n" + disassembledMethod
  48. );
  49. else if (!disassembledMethod.contains(JAVA8_API_CALL))
  50. fail(
  51. "Cannot determine if class '" + className + "' was compiled against Java 8 or 9+ API. " +
  52. "This should never happen.\n" +
  53. "Disassembled method:\n" + disassembledMethod
  54. );
  55. }
  56. public static Test suite() {
  57. return XMLBasedAjcTestCase.loadSuite(CompileWithReleaseTests.class);
  58. }
  59. @Override
  60. protected java.net.URL getSpecFile() {
  61. return getClassResource("ajc198.xml");
  62. }
  63. }