-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weaver returns null instead of original bytes for unwoven classes
This change makes sense independently of #277, but also enables using - cp "my.jar;aspectjweaver.jar" -XX:+AllowArchivingWithJavaAgent -javaagent:aspectjweaver.jar while creating a CDS archive. Afterward, the application can be run in its woven state from the CDS archive even without '-javaagent', because the byte code was archived in its woven state ("poor man's AJC"). See #277 (comment) for details. Fixes #277. Signed-off-by: Alexander Kriegisch <[email protected]>
- Loading branch information
Showing
5 changed files
with
27 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,20 +324,21 @@ protected Boolean initialValue() { | |
* @return the woven bytes | ||
* @exception IOException weave failed | ||
*/ | ||
public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IOException { | ||
public byte[] weaveClass(String name, final byte[] bytes, boolean mustWeave) throws IOException { | ||
if (trace == null) { | ||
// Pr231945: we are likely to be under tomcat and ENABLE_CLEAR_REFERENCES hasn't been set | ||
System.err | ||
.println("AspectJ Weaver cannot continue to weave, static state has been cleared. Are you under Tomcat? In order to weave '" | ||
+ name | ||
+ "' during shutdown, 'org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false' must be set (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=231945)."); | ||
return bytes; | ||
return null; | ||
} | ||
if (weaverRunning.get()) { | ||
// System.out.println("AJC: avoiding re-entrant call to transform " + name); | ||
return bytes; | ||
return null; | ||
} | ||
try { | ||
byte[] newBytes = null; | ||
weaverRunning.set(true); | ||
if (trace.isTraceEnabled()) { | ||
trace.enter("weaveClass", this, new Object[] { name, bytes }); | ||
|
@@ -347,7 +348,7 @@ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IO | |
if (trace.isTraceEnabled()) { | ||
trace.exit("weaveClass", false); | ||
} | ||
return bytes; | ||
return null; | ||
} | ||
|
||
boolean debugOn = !messageHandler.isIgnoring(Message.DEBUG); | ||
|
@@ -360,15 +361,14 @@ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IO | |
|
||
// Determine if we have the weaved class cached | ||
CachedClassReference cacheKey = null; | ||
final byte[] original_bytes = bytes; | ||
if (cache != null && !mustWeave) { | ||
cacheKey = cache.createCacheKey(name, original_bytes); | ||
CachedClassEntry entry = cache.get(cacheKey, original_bytes); | ||
cacheKey = cache.createCacheKey(name, bytes); | ||
CachedClassEntry entry = cache.get(cacheKey, bytes); | ||
if (entry != null) { | ||
// If the entry has been explicitly ignored | ||
// return the original bytes | ||
if (entry.isIgnored()) { | ||
return bytes; | ||
return null; | ||
} | ||
return entry.getBytes(); | ||
} | ||
|
@@ -382,7 +382,12 @@ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IO | |
if (debugOn) { | ||
debug("weaving '" + name + "'"); | ||
} | ||
bytes = getWovenBytes(name, bytes); | ||
newBytes = getWovenBytes(name, bytes); | ||
// TODO: Is this OK performance-wise? | ||
if (Arrays.equals(bytes, newBytes)) { | ||
// null means unchanged in java.lang.instrument.ClassFileTransformer::transform | ||
newBytes = null; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kriegaex
Author
Contributor
|
||
} | ||
// temporarily out - searching for @Aspect annotated types is a slow thing to do - we should | ||
// expect the user to name them if they want them woven - just like code style | ||
// } else if (shouldWeaveAnnotationStyleAspect(name, bytes)) { | ||
|
@@ -405,10 +410,10 @@ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IO | |
if (cacheKey != null) { | ||
// If no transform has been applied, mark the class | ||
// as ignored. | ||
if (Arrays.equals(original_bytes, bytes)) { | ||
cache.ignore(cacheKey, original_bytes); | ||
if (newBytes == null) { | ||
cache.ignore(cacheKey, bytes); | ||
} else { | ||
cache.put(cacheKey, original_bytes, bytes); | ||
cache.put(cacheKey, bytes, newBytes); | ||
} | ||
} | ||
} else if (debugOn) { | ||
|
@@ -422,9 +427,9 @@ public byte[] weaveClass(String name, byte[] bytes, boolean mustWeave) throws IO | |
} | ||
|
||
if (trace.isTraceEnabled()) { | ||
trace.exit("weaveClass", bytes); | ||
trace.exit("weaveClass", newBytes); | ||
} | ||
return bytes; | ||
return newBytes; | ||
} finally { | ||
weaverRunning.remove(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I think the performance-wise is ok, because if a class is enhanced, there is a high probability that the lengths of the arrays are different. Arrays.equals has this fast judgment.