KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > obfuscator > modules > SerializePreserver


1 /* SerializePreserver Copyright (C) 1999-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: SerializePreserver.java.in,v 1.1.2.2 2002/05/28 17:34:17 hoenicke Exp $
18  */

19
20 package jode.obfuscator.modules;
21 import jode.obfuscator.*;
22
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 public class SerializePreserver implements IdentifierMatcher, OptionHandler {
27     boolean onlySUID = true;
28
29     public SerializePreserver() {
30     }
31
32     public void setOption(String JavaDoc option, Collection JavaDoc values) {
33     if (option.equals("all")) {
34         onlySUID = false;
35     } else
36         throw new IllegalArgumentException JavaDoc("Invalid option `"+option+"'.");
37     }
38
39     public final boolean matchesSub(Identifier ident, String JavaDoc name) {
40     if (ident instanceof PackageIdentifier)
41         return true;
42     if (ident instanceof ClassIdentifier) {
43         ClassIdentifier clazz = (ClassIdentifier) ident;
44         return (clazz.isSerializable()
45             && (!onlySUID || clazz.hasSUID()));
46     }
47     return false;
48     }
49
50     public final boolean matches(Identifier ident) {
51     ClassIdentifier clazz;
52     if (ident instanceof ClassIdentifier)
53         clazz = (ClassIdentifier) ident;
54     else if (ident instanceof FieldIdentifier)
55         clazz = (ClassIdentifier) ident.getParent();
56     else
57         return false;
58
59     if (!clazz.isSerializable()
60         || (onlySUID && !clazz.hasSUID()))
61         return false;
62
63     if (ident instanceof FieldIdentifier) {
64         FieldIdentifier field = (FieldIdentifier) ident;
65         if ((field.getModifiers()
66          & (Modifier.TRANSIENT | Modifier.STATIC)) == 0)
67         return true;
68         if (ident.getName().equals("serialPersistentFields")
69         || ident.getName().equals("serialVersionUID"))
70         return true;
71     } else if (ident instanceof MethodIdentifier) {
72         if (ident.getName().equals("writeObject")
73         && ident.getType().equals("(Ljava.io.ObjectOutputStream)V"))
74         return true;
75         if (ident.getName().equals("readObject")
76         && ident.getType().equals("(Ljava.io.ObjectInputStream)V"))
77         return true;
78     } else if (ident instanceof ClassIdentifier) {
79         if (!clazz.hasSUID())
80         clazz.addSUID();
81         return true;
82     }
83     return false;
84     }
85
86     public final String JavaDoc getNextComponent(Identifier ident) {
87     return null;
88     }
89 }
90
Popular Tags