001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.transformer;
032:
033: import net.sf.retrotranslator.runtime.asm.Type;
034: import net.sf.retrotranslator.runtime.asm.signature.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: class NameTranslator {
040:
041: protected String identifier(String s) {
042: return s;
043: }
044:
045: protected static String fixIdentifier(String s) {
046: return s == null ? null : s.replace('+', '$');
047: }
048:
049: protected String typeName(String s) {
050: return s;
051: }
052:
053: protected final Type type(Type type) {
054: if (type == null) {
055: return null;
056: }
057: if (type.getSort() == Type.OBJECT) {
058: return TransformerTools.getTypeByInternalName(typeName(type
059: .getInternalName()));
060: }
061: if (type.getSort() == Type.ARRAY) {
062: Type elementType = type.getElementType();
063: if (elementType.getSort() == Type.OBJECT) {
064: return TransformerTools.getArrayTypeByInternalName(
065: typeName(elementType.getInternalName()), type
066: .getDimensions());
067: }
068: }
069: return type;
070: }
071:
072: protected final String typeDescriptor(String s) {
073: return s == null ? null : type(Type.getType(s)).getDescriptor();
074: }
075:
076: protected final String methodDescriptor(String s) {
077: return s == null ? null : Type.getMethodDescriptor(type(Type
078: .getReturnType(s)), types(Type.getArgumentTypes(s)));
079: }
080:
081: protected final String[] typeNames(String[] names) {
082: if (names == null)
083: return null;
084: for (int i = 0; i < names.length; i++) {
085: names[i] = typeName(names[i]);
086: }
087: return names;
088: }
089:
090: protected final Type[] types(Type[] types) {
091: if (types == null)
092: return null;
093: for (int i = 0; i < types.length; i++) {
094: types[i] = type(types[i]);
095: }
096: return types;
097: }
098:
099: protected final Object typeOrValue(Object object) {
100: return object instanceof Type ? type((Type) object) : object;
101: }
102:
103: protected final String typeNameOrTypeDescriptor(String s) {
104: return s != null && s.startsWith("[") ? typeDescriptor(s)
105: : typeName(s);
106: }
107:
108: protected final String typeSignature(String s) {
109: if (s == null)
110: return null;
111: SignatureWriter writer = new SignatureWriter();
112: new SignatureReader(s)
113: .acceptType(new TranslatingSignatureVisitor(writer));
114: return writer.toString();
115: }
116:
117: protected final String declarationSignature(String s) {
118: if (s == null)
119: return null;
120: SignatureWriter writer = new SignatureWriter();
121: new SignatureReader(s).accept(new TranslatingSignatureVisitor(
122: writer));
123: return writer.toString();
124: }
125:
126: private class TranslatingSignatureVisitor extends SignatureAdapter {
127:
128: public TranslatingSignatureVisitor(SignatureVisitor visitor) {
129: super (visitor);
130: }
131:
132: protected SignatureVisitor visitStart(SignatureVisitor visitor) {
133: return new TranslatingSignatureVisitor(visitor);
134: }
135:
136: public void visitClassType(String name) {
137: super .visitClassType(typeNameOrTypeDescriptor(name));
138: }
139:
140: public void visitInnerClassType(String name) {
141: super.visitInnerClassType(identifier(name));
142: }
143: }
144:
145: }
|