1 /****************************************************************************
2 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the LGPL License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 ****************************************************************************/
8
9 package it.imolinfo.jbi4corba.webservice.generator.bcm;
10
11 import it.imolinfo.jbi4corba.Logger;
12 import it.imolinfo.jbi4corba.LoggerFactory;
13
14 import org.objectweb.asm.ClassAdapter;
15 import org.objectweb.asm.ClassVisitor;
16 import org.objectweb.asm.ClassWriter;
17 import org.objectweb.asm.FieldVisitor;
18 import org.objectweb.asm.Opcodes;
19
20 /**
21 * bytecode manipulation.
22 *
23 * Add the serialVersionUID.
24 */
25 public class UIDAdapter extends ClassAdapter {
26
27 /**
28 * Logger.
29 */
30 private static final Logger LOG = LoggerFactory.getLogger(UIDAdapter.class);
31
32 /**
33 * The ClassWriter.
34 */
35 protected ClassWriter classWriter = null;
36
37 /**
38 * The new serialVersionUID of the class.
39 * If this value is null then no changes are performed;
40 */
41 protected Long newSerialVersionUID = null;
42
43
44 /**
45 * The adapater used to manipulate the code.
46 *
47 * @param cv The ClassVisitor used in this object.
48 * @param cw The ClassWriter used in this object.
49 * @param uid The new serialVersionUID.
50 */
51 public UIDAdapter(ClassVisitor cv,
52 ClassWriter cw,
53 Long uid) {
54 super(cv);
55
56 classWriter = cw;
57 newSerialVersionUID = uid;
58
59 LOG.debug("new UIDAdapter; ClassVisitor=" + cv
60 + "; ClassWriter=" + cw
61 + "; newSerialVersionUID=" + uid);
62 }
63
64
65 /**
66 * This method append the serialVersionUID to the class if the attribute
67 * newSerialVersionUID is not null.
68 */
69 @Override
70 public void visitEnd() {
71
72 if (newSerialVersionUID != null) {
73 LOG.debug("addSerialVersionUid. uid=" + newSerialVersionUID
74 + "; classWriter=" + classWriter);
75
76 FieldVisitor fieldVisitor = super.visitField(
77 Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
78 "serialVersionUID",
79 "J",
80 null,
81 newSerialVersionUID);
82
83 fieldVisitor.visitEnd();
84
85 }
86
87 super.visitEnd();
88 }
89
90 }