From 5c82acb58e2d665a46529290ae85ae717cbf2bd3 Mon Sep 17 00:00:00 2001 From: eli-fin Date: Sun, 19 May 2019 14:10:35 +0300 Subject: Fix serialVersionUID initialization for java9+ Move initialization of static final serialVersionUID field to clinit, since as of java9+, the runtime won't allow setting final fields outside clinit Signed-off-by: Eli Finkel --- .../java/org/aspectj/weaver/bcel/LazyClassGen.java | 37 ++++++++-------------- 1 file changed, 13 insertions(+), 24 deletions(-) (limited to 'weaver/src') diff --git a/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java b/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java index 83189e1d4..57761bbdf 100644 --- a/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java +++ b/weaver/src/main/java/org/aspectj/weaver/bcel/LazyClassGen.java @@ -1018,7 +1018,15 @@ public final class LazyClassGen { NameMangler.AJC_PRE_CLINIT_NAME, Type.NO_ARGS, NO_STRINGS, this); ajcPreClinit.getBody().insert(InstructionConstants.RETURN); methodGens.add(ajcPreClinit); - getStaticInitializer().getBody().insert(Utility.createInvoke(fact, ajcPreClinit)); + InstructionList clinitBody = getStaticInitializer().getBody(); + clinitBody.insert(Utility.createInvoke(fact, ajcPreClinit)); + if (serialVersionUIDRequiresInitialization) { + InstructionList il = new InstructionList(); + il.append(InstructionFactory.PUSH(getConstantPool(), calculatedSerialVersionUID)); + il.append(getFactory().createFieldAccess(getClassName(), "serialVersionUID", BasicType.LONG, + Constants.PUTSTATIC)); + clinitBody.insert(il); + } return ajcPreClinit; } @@ -1204,31 +1212,12 @@ public final class LazyClassGen { // } private void addAjcInitializers() { - if (tjpFields.size() == 0 && !serialVersionUIDRequiresInitialization) { + if (tjpFields.size() == 0) { return; } - InstructionList[] il = null; - - if (tjpFields.size() > 0) { - il = initializeAllTjps(); - } - - if (serialVersionUIDRequiresInitialization) { - InstructionList[] ilSVUID = new InstructionList[1]; - ilSVUID[0] = new InstructionList(); - ilSVUID[0].append(InstructionFactory.PUSH(getConstantPool(), calculatedSerialVersionUID)); - ilSVUID[0].append(getFactory().createFieldAccess(getClassName(), "serialVersionUID", BasicType.LONG, - Constants.PUTSTATIC)); - if (il == null) { - il = ilSVUID; - } else { - InstructionList[] newIl = new InstructionList[il.length + ilSVUID.length]; - System.arraycopy(il, 0, newIl, 0, il.length); - System.arraycopy(ilSVUID, 0, newIl, il.length, ilSVUID.length); - il = newIl; - } - } - + + InstructionList[] il = initializeAllTjps(); + LazyMethodGen prevMethod; LazyMethodGen nextMethod = null; if (this.isInterface()) { // Cannot sneak stuff into another static method in an interface -- cgit v1.2.3