From 080ff49c8d75cba626d324cc3aff4fe3f1bda68f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 20 Jan 2017 16:47:14 +0000 Subject: [PATCH] Prevent AspectJ from loading aspects too early Previously, AspectJ would find META-INF/aop.xml almost as soon as the jar was launched, and before LaunchedURLClassLoader had been created. This meant that AspectJ would attempt to load aspects listed in META-INF/aop.xml but that were package in BOOT-INF/classes and, therefore, could not be loaded. This commit updates the Repackager so that a META-INF/aop.xml file is moved into BOOT-INF/classes. This ensures that it isn't visible to the app class loader so it won't be loaded to early. It will now be loaded by LaunchedURLClassLoader which can also load the compiled aspects that aop.xml references. Closes gh-7587 --- .../boot/loader/tools/Repackager.java | 5 +++-- .../boot/loader/tools/RepackagerTests.java | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index acf24c6e83f..eab4b509214 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -432,7 +432,8 @@ public class Repackager { if (entry.getName().equals("META-INF/INDEX.LIST")) { return null; } - if (entry.getName().startsWith("META-INF/") + if ((entry.getName().startsWith("META-INF/") + && !entry.getName().equals("META-INF/aop.xml")) || entry.getName().startsWith("BOOT-INF/")) { return entry; } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index 56e24c5b2eb..91a2b88ced3 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -593,6 +593,26 @@ public class RepackagerTests { jarFile.close(); } + @Test + public void metaInfAopXmlIsMovedBeneathBootInfClassesWhenRepackaged() + throws Exception { + this.testJarFile.addClass("A.class", ClassWithMainMethod.class); + this.testJarFile.addFile("META-INF/aop.xml", + this.temporaryFolder.newFile("aop.xml")); + File source = this.testJarFile.getFile(); + File dest = this.temporaryFolder.newFile("dest.jar"); + Repackager repackager = new Repackager(source); + repackager.repackage(dest, NO_LIBRARIES); + JarFile jarFile = new JarFile(dest); + try { + assertThat(jarFile.getEntry("META-INF/aop.xml")).isNull(); + assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/aop.xml")).isNotNull(); + } + finally { + jarFile.close(); + } + } + private boolean hasLauncherClasses(File file) throws IOException { return hasEntry(file, "org/springframework/boot/") && hasEntry(file, "org/springframework/boot/loader/JarLauncher.class");