diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java b/src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java deleted file mode 100644 index ddc5af4..0000000 --- a/src/main/java/cuchaz/enigma/mapping/SignatureUpdater.java +++ /dev/null | |||
| @@ -1,92 +0,0 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | |||
| 12 | package cuchaz.enigma.mapping; | ||
| 13 | |||
| 14 | import com.google.common.collect.Lists; | ||
| 15 | |||
| 16 | import java.io.IOException; | ||
| 17 | import java.io.StringReader; | ||
| 18 | import java.util.List; | ||
| 19 | |||
| 20 | public class SignatureUpdater { | ||
| 21 | |||
| 22 | public static String update(String signature, ClassNameUpdater updater) { | ||
| 23 | try { | ||
| 24 | StringBuilder buf = new StringBuilder(); | ||
| 25 | |||
| 26 | // read the signature character-by-character | ||
| 27 | StringReader reader = new StringReader(signature); | ||
| 28 | int i; | ||
| 29 | while ((i = reader.read()) != -1) { | ||
| 30 | char c = (char) i; | ||
| 31 | |||
| 32 | // does this character start a class name? | ||
| 33 | if (c == 'L') { | ||
| 34 | // update the class name and add it to the buffer | ||
| 35 | buf.append('L'); | ||
| 36 | String className = readClass(reader); | ||
| 37 | if (className == null) { | ||
| 38 | throw new IllegalArgumentException("Malformed signature: " + signature); | ||
| 39 | } | ||
| 40 | buf.append(updater.update(className)); | ||
| 41 | buf.append(';'); | ||
| 42 | } else { | ||
| 43 | // copy the character into the buffer | ||
| 44 | buf.append(c); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | return buf.toString(); | ||
| 49 | } catch (IOException ex) { | ||
| 50 | // I'm pretty sure a StringReader will never throw one of these | ||
| 51 | throw new Error(ex); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | private static String readClass(StringReader reader) throws IOException { | ||
| 56 | // read all the characters in the buffer until we hit a ';' | ||
| 57 | // remember to treat generics correctly | ||
| 58 | StringBuilder buf = new StringBuilder(); | ||
| 59 | int depth = 0; | ||
| 60 | int i; | ||
| 61 | while ((i = reader.read()) != -1) { | ||
| 62 | char c = (char) i; | ||
| 63 | |||
| 64 | if (c == '<') { | ||
| 65 | depth++; | ||
| 66 | } else if (c == '>') { | ||
| 67 | depth--; | ||
| 68 | } else if (depth == 0) { | ||
| 69 | if (c == ';') { | ||
| 70 | return buf.toString(); | ||
| 71 | } else { | ||
| 72 | buf.append(c); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | return null; | ||
| 78 | } | ||
| 79 | |||
| 80 | public static List<String> getClasses(String signature) { | ||
| 81 | final List<String> classNames = Lists.newArrayList(); | ||
| 82 | update(signature, className -> { | ||
| 83 | classNames.add(className); | ||
| 84 | return className; | ||
| 85 | }); | ||
| 86 | return classNames; | ||
| 87 | } | ||
| 88 | |||
| 89 | public interface ClassNameUpdater { | ||
| 90 | String update(String className); | ||
| 91 | } | ||
| 92 | } | ||