001: /* ===========================================================================
002: * $RCSfile: MdFd.java,v $
003: * ===========================================================================
004: *
005: * RetroGuard -- an obfuscation package for Java classfiles.
006: *
007: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
008: *
009: * This program can be redistributed and/or modified under the terms of the
010: * Version 2 of the GNU General Public License as published by the Free
011: * Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: */
019:
020: package COM.rl.obf;
021:
022: import java.io.*;
023: import java.lang.reflect.*;
024: import java.util.*;
025: import COM.rl.util.*;
026: import COM.rl.obf.classfile.*;
027:
028: /**
029: * Base to method and field tree items.
030: *
031: * @author Mark Welsh
032: */
033: abstract public class MdFd extends TreeItem {
034: // Constants -------------------------------------------------------------
035:
036: // Fields ----------------------------------------------------------------
037: private String descriptor = null;
038: private boolean isOverride = false;
039:
040: // Class Methods ---------------------------------------------------------
041:
042: // Instance Methods ------------------------------------------------------
043: /** Ctor. */
044: public MdFd(TreeItem parent, boolean isSynthetic, String name,
045: String descriptor, int access) throws Exception {
046: super (parent, name);
047: this .descriptor = descriptor;
048: this .access = access;
049: this .isSynthetic = isSynthetic;
050: if (name.equals("") || descriptor.equals("")
051: || !(parent instanceof Cl)) {
052: System.err
053: .println("Internal error: method/field must have name and descriptor, and have Class or Interface as parent");
054: }
055:
056: // Disallow obfuscation of 'Synthetic' methods
057: if (isSynthetic) {
058: setOutName(getInName());
059: }
060: }
061:
062: /** Return the method or field descriptor String. */
063: public String getDescriptor() {
064: return descriptor;
065: }
066:
067: /** Is this member static? */
068: public boolean isStatic() {
069: return Modifier.isStatic(getModifiers());
070: }
071:
072: /** Set that this method or field is an override. */
073: public void setIsOverride() {
074: isOverride = true;
075: }
076:
077: /** Is this method or field an override? */
078: public boolean isOverride() {
079: return isOverride;
080: }
081:
082: /** Return the display name for field. */
083: public String toString() {
084: StringBuffer sb = new StringBuffer();
085: int modifiers = getModifiers();
086: // NOTE - could update with new JDK1.5 modifiers, but that
087: // would cause incompatibility with earlier systems for RG
088: if (Modifier.isAbstract(modifiers)) {
089: sb.append("abstract ");
090: }
091: if (Modifier.isSynchronized(modifiers)) {
092: sb.append("synchronized ");
093: }
094: if (Modifier.isTransient(modifiers)) {
095: sb.append("transient ");
096: }
097: if (Modifier.isVolatile(modifiers)) {
098: sb.append("volatile ");
099: }
100: if (Modifier.isNative(modifiers)) {
101: sb.append("native ");
102: }
103: if (Modifier.isPublic(modifiers)) {
104: sb.append("public ");
105: }
106: if (Modifier.isProtected(modifiers)) {
107: sb.append("protected ");
108: }
109: if (Modifier.isPrivate(modifiers)) {
110: sb.append("private ");
111: }
112: if (Modifier.isStatic(modifiers)) {
113: sb.append("static ");
114: }
115: if (Modifier.isFinal(modifiers)) {
116: sb.append("final ");
117: }
118: sb.append(getReturnTypeName());
119: sb.append(getInName());
120: sb.append(getDescriptorName());
121: return sb.toString();
122: }
123:
124: /** Return the display name of the return type. */
125: protected String getReturnTypeName() {
126: String[] types = parseTypes();
127: return (types.length > 0 ? types[types.length - 1] : "") + " ";
128: }
129:
130: /** Return the display name of the descriptor types. */
131: abstract protected String getDescriptorName();
132:
133: /** Return the parsed descriptor types array. */
134: private String[] parsedTypes = null;
135:
136: protected String[] parseTypes() {
137: if (parsedTypes == null) {
138: try {
139: parsedTypes = ClassFile.parseDescriptor(
140: getDescriptor(), true);
141: } catch (Exception e) {
142: parsedTypes = null;
143: }
144: }
145: return parsedTypes;
146: }
147:
148: /** Does this member match the wildcard pattern? (** and * supported) */
149: public boolean isWildcardMatch(String namePattern,
150: String descPattern) {
151: return isWildcardMatch(namePattern)
152: && isMatch(descPattern, getDescriptor());
153: }
154: }
|