private static SerialVersionUidResult computeSerialVersionUid( JarArchiveComparatorOptions options, Optional<CtClass> ctClassOptional, JarArchiveComparator jarArchiveComparator) { SerialVersionUidResult result = new SerialVersionUidResult(); if (ctClassOptional.isPresent()) { CtClass ctClass = ctClassOptional.get(); if (isCtClassSerializable(options, ctClass, jarArchiveComparator)) { result.serializable = true; try { CtField declaredField = ctClass.getDeclaredField(SERIAL_VERSION_UID); Object constantValue = declaredField.getConstantValue(); if (constantValue instanceof Long) { result.serialVersionUid = Optional.of((Long) constantValue); } } catch (Exception e) { try { SerialVersionUID.setSerialVersionUID(ctClass); CtField declaredField = ctClass.getDeclaredField(SERIAL_VERSION_UID); Object constantValue = declaredField.getConstantValue(); if (constantValue instanceof Long) { result.serialVersionUidDefault = Optional.of((Long) constantValue); } ctClass.removeField(declaredField); } catch (Exception ignored) { } } if (!result.serialVersionUidDefault.isPresent()) { try { CtField declaredFieldOriginal = ctClass.getDeclaredField(SERIAL_VERSION_UID); ctClass.removeField(declaredFieldOriginal); SerialVersionUID.setSerialVersionUID(ctClass); CtField declaredField = ctClass.getDeclaredField(SERIAL_VERSION_UID); Object constantValue = declaredField.getConstantValue(); if (constantValue instanceof Long) { result.serialVersionUidDefault = Optional.of((Long) constantValue); } ctClass.removeField(declaredField); ctClass.addField(declaredFieldOriginal); } catch (Exception ignored) { } } } } return result; }
private void testRemove2(CtClass cc, String fieldName) throws Exception { CtField f = cc.getField(fieldName); cc.removeField(f); try { CtField f2 = cc.getField(fieldName); fail("the removed field still exists"); } catch (NotFoundException e) { } }
public void testRemove() throws Exception { CtClass cc = sloader.get("test2.Remove"); testRemove2(cc, "f1"); testRemove2(cc, "f6"); testRemove2(cc, "f3"); CtField p = cc.getField("p"); try { cc.removeField(p); fail("non-existing field has been removed"); } catch (NotFoundException e) { } testRemove3(cc, "bar"); testRemove3(cc, "bar2"); testRemove4(cc, "(I)V"); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(7, invoke(obj, "foo")); }