001: /**
002: * YGuard -- an obfuscation library for Java(TM) classfiles.
003: *
004: * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
005: * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library 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 library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * The author may be contacted at yguard@yworks.com
022: *
023: * Java and all Java-based marks are trademarks or registered
024: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
025: */package com.yworks.yguard.obf;
026:
027: import java.io.*;
028: import java.lang.reflect.*;
029: import java.util.*;
030: import com.yworks.yguard.obf.classfile.*;
031:
032: /**
033: * Base to method and field tree items.
034: *
035: * @author Mark Welsh
036: */
037: abstract public class MdFd extends TreeItem {
038: // Constants -------------------------------------------------------------
039:
040: // Fields ----------------------------------------------------------------
041: private String descriptor = null;
042:
043: // Class Methods ---------------------------------------------------------
044:
045: // Instance Methods ------------------------------------------------------
046: /** Ctor. */
047: public MdFd(TreeItem parent, boolean isSynthetic, String name,
048: String descriptor, int access) {
049: super (parent, name);
050: this .descriptor = descriptor;
051: this .access = access;
052: this .isSynthetic = isSynthetic;
053: if (name.equals("") || descriptor.equals("")
054: || !(parent instanceof Cl)) {
055: System.err
056: .println("Internal error: method/field must have name and descriptor, and have Class or Interface as parent");
057: }
058:
059: // Disallow obfuscation of 'Synthetic' and native methods and fields
060: if (isSynthetic || Modifier.isNative(access)) {
061: setOutName(getInName());
062: }
063: }
064:
065: /** Return the method or field descriptor String. */
066: public String getDescriptor() {
067: return descriptor;
068: }
069:
070: /** Return the display name for field. */
071: public String toString() {
072: StringBuffer sb = new StringBuffer();
073: int modifiers = getModifiers();
074: if (Modifier.isAbstract(modifiers)) {
075: sb.append("abstract ");
076: }
077: if (Modifier.isSynchronized(modifiers)) {
078: sb.append("synchronized ");
079: }
080: if (Modifier.isTransient(modifiers)) {
081: sb.append("transient ");
082: }
083: if (Modifier.isVolatile(modifiers)) {
084: sb.append("volatile ");
085: }
086: if (Modifier.isNative(modifiers)) {
087: sb.append("native ");
088: }
089: if (Modifier.isPublic(modifiers)) {
090: sb.append("public ");
091: }
092: if (Modifier.isProtected(modifiers)) {
093: sb.append("protected ");
094: }
095: if (Modifier.isPrivate(modifiers)) {
096: sb.append("private ");
097: }
098: if (Modifier.isStatic(modifiers)) {
099: sb.append("static ");
100: }
101: if (Modifier.isFinal(modifiers)) {
102: sb.append("final ");
103: }
104: sb.append(getReturnTypeName());
105: sb.append(getInName());
106: sb.append(getDescriptorName());
107: return sb.toString();
108: }
109:
110: /** Return the display name of the return type. */
111: protected String getReturnTypeName() {
112: String[] types = parseTypes();
113: return (types.length > 0 ? types[types.length - 1] : "") + " ";
114: }
115:
116: /** Return the display name of the descriptor types. */
117: abstract protected String getDescriptorName();
118:
119: /** Return the parsed descriptor types array. */
120: private String[] parsedTypes = null;
121:
122: protected String[] parseTypes() {
123: if (parsedTypes == null) {
124: try {
125: parsedTypes = ClassFile.parseDescriptor(
126: getDescriptor(), true);
127: } catch (Exception e) {
128: parsedTypes = null;
129: }
130: }
131: return parsedTypes;
132: }
133: }
|