001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.tools;
023:
024: import java.io.ObjectStreamClass;
025: import java.io.Serializable;
026: import java.io.FileInputStream;
027: import java.io.ObjectInputStream;
028: import java.util.Map;
029:
030: /**
031: * Encapsulates a class serialVersionUID and codebase.
032: *
033: * @author Scott.Stark@jboss.org
034: * @version $Revision: 57210 $
035: */
036: public class ClassVersionInfo implements Serializable {
037: static final long serialVersionUID = 2036506209171911437L;
038:
039: /** The named class serialVersionUID as returned by ObjectStreamClass */
040: private long serialVersion;
041: /** The binary class name */
042: private String name;
043: private boolean hasExplicitSerialVersionUID;
044:
045: public ClassVersionInfo(String name, ClassLoader loader)
046: throws ClassNotFoundException {
047: this .name = name;
048: Class c = loader.loadClass(name);
049: if (c.isInterface() == false) {
050: ObjectStreamClass osc = ObjectStreamClass.lookup(c);
051: if (osc != null) {
052: serialVersion = osc.getSerialVersionUID();
053: try {
054: c.getDeclaredField("serialVersionUID");
055: hasExplicitSerialVersionUID = true;
056: } catch (NoSuchFieldException e) {
057: hasExplicitSerialVersionUID = false;
058: }
059: }
060: }
061: }
062:
063: public long getSerialVersion() {
064: return serialVersion;
065: }
066:
067: public boolean getHasExplicitSerialVersionUID() {
068: return hasExplicitSerialVersionUID;
069: }
070:
071: public String getName() {
072: return name;
073: }
074:
075: public String toString() {
076: StringBuffer tmp = new StringBuffer("ClassVersionInfo");
077: tmp.append('{');
078: tmp.append("serialVersion=");
079: tmp.append(serialVersion);
080: tmp.append(", hasExplicitSerialVersionUID=");
081: tmp.append(hasExplicitSerialVersionUID);
082: tmp.append(", name=");
083: tmp.append(name);
084: tmp.append('}');
085: return tmp.toString();
086: }
087:
088: /**
089: * Utility main entry point that allows one to load a Map<String,ClassVersionInfo>
090: * from a serialized object file to print a specific ClassVersionInfo
091: *
092: * @param args [0] = map.ser file for serialized object image
093: * [1] = class name of the ClassVersionInfo to print
094: * @throws Exception
095: */
096: public static void main(String[] args) throws Exception {
097: if (args.length != 2) {
098: throw new IllegalArgumentException(
099: "Usage ClassVersionInfo map.ser [-list|cvi_fqn]");
100: }
101: FileInputStream fis = new FileInputStream(args[0]);
102: ObjectInputStream ois = new ObjectInputStream(fis);
103: Map cvis = (Map) ois.readObject();
104: ois.close();
105:
106: if (args[1].equals("-list")) {
107: System.out.println(cvis.keySet());
108: System.exit(0);
109: }
110:
111: String fqn = args[1];
112: if (cvis.containsKey(fqn) == false) {
113: System.err.println("No entry found for: " + fqn);
114: System.exit(1);
115: }
116: ClassVersionInfo cvi = (ClassVersionInfo) cvis.get(fqn);
117: System.out.println(cvi);
118: System.exit(0);
119: }
120: }
|