|
| 1 | +/* |
| 2 | + * Copyright 2016 Sam Sun <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.javadeobfuscator.deobfuscator.transformers.normalizer; |
| 18 | + |
| 19 | +import com.javadeobfuscator.deobfuscator.config.TransformerConfig; |
| 20 | + |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.apache.commons.lang3.StringUtils; |
| 25 | + |
| 26 | +@TransformerConfig.ConfigOptions(configClass = PackageTruncator.Config.class) |
| 27 | +public class PackageTruncator extends AbstractNormalizer<PackageTruncator.Config> { |
| 28 | + @Override |
| 29 | + public void remap(CustomRemapper remapper) { |
| 30 | + Set<String> mappedNames = new HashSet<>(); |
| 31 | + remapper.setIgnorePackages(true); |
| 32 | + classNodes().forEach(classNode -> { |
| 33 | + int matches = StringUtils.countMatches(classNode.name, "/"); |
| 34 | + if(matches > getConfig().getPackageLayers()) |
| 35 | + { |
| 36 | + int lastIndex = classNode.name.lastIndexOf("/"); |
| 37 | + String packages = classNode.name.substring(0, lastIndex); |
| 38 | + String name = classNode.name.substring(lastIndex, classNode.name.length()); |
| 39 | + int indexFromStart = StringUtils.ordinalIndexOf(packages, "/", getConfig().getPackageLayers()) + 1; |
| 40 | + int indexFromEnd = StringUtils.lastOrdinalIndexOf(packages, "/", getConfig().getPackageLayers()); |
| 41 | + String newName = indexFromStart > packages.length() - indexFromEnd ? |
| 42 | + packages.substring(0, indexFromStart) : packages.substring(indexFromEnd + 1, packages.length()); |
| 43 | + newName += name; |
| 44 | + int counter = 1; |
| 45 | + boolean useCounter = false; |
| 46 | + if(classes.containsKey(newName) || mappedNames.contains(newName)) |
| 47 | + { |
| 48 | + useCounter = true; |
| 49 | + while(classes.containsKey(newName + "_" + counter) |
| 50 | + || mappedNames.contains(newName + "_" + counter)) |
| 51 | + counter++; |
| 52 | + } |
| 53 | + remapper.map(classNode.name, useCounter ? newName + "_" + counter : newName); |
| 54 | + mappedNames.add(newName); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public static class Config extends AbstractNormalizer.Config { |
| 60 | + /** |
| 61 | + * The number of package layers to allow before the class's packages should be truncated. |
| 62 | + */ |
| 63 | + private int packageLayers = 15; |
| 64 | + |
| 65 | + public Config() { |
| 66 | + super(PackageTruncator.class); |
| 67 | + } |
| 68 | + |
| 69 | + public int getPackageLayers() { |
| 70 | + return packageLayers; |
| 71 | + } |
| 72 | + |
| 73 | + public void setPackageLayers(int packageLayers) { |
| 74 | + this.packageLayers = packageLayers; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments