static MethodAnnotation obfuscate(MethodAnnotation m) { String className = m.getClassName(); if (className.startsWith("java")) { return m; } String methodName = m.getMethodName(); String methodSignature = m.getMethodSignature(); if ("hashCode".equals(methodName) && "()I".equals(methodSignature) || "equals".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSignature) || "compareTo".equals(methodName) && "(Ljava/lang/Object;)I".equals(methodSignature) || "<init>".equals(methodName) || "<clinit>".equals(methodName)) { // don't need to obfuscate method name } else { methodName = hashData(methodName); } MethodAnnotation result = new MethodAnnotation(hashClass(className), methodName, hashMethodSignature(methodSignature), m.isStatic()); result.setDescription(m.getDescription()); return result; }
public static String hashSignature(String signature) { char firstChar = signature.charAt(0); switch (firstChar) { case '[': return '[' + hashSignature(signature.substring(1)); case 'V': case 'Z': case 'B': case 'S': case 'C': case 'I': case 'J': case 'D': case 'F': if (signature.length() == 1) { return signature; } throw new IllegalArgumentException("bad signature: " + signature); case 'L': if (!signature.endsWith(";")) { throw new IllegalArgumentException("bad signature: " + signature); } return hashFieldSignature(signature); default: throw new IllegalArgumentException("bad signature: " + signature); } }
@Override public void visitClassAnnotation(ClassAnnotation classAnnotation) { result.add(obfuscate(classAnnotation)); } };
static FieldAnnotation obfuscate(FieldAnnotation m) { FieldAnnotation result = new FieldAnnotation(hashClass(m.getClassName()), hashData(m.getFieldName()), hashSignature(m.getFieldSignature()), m.isStatic()); result.setDescription(m.getDescription()); return result; }
static SourceLineAnnotation obfuscate(SourceLineAnnotation m) { SourceLineAnnotation result = new SourceLineAnnotation(hashClass(m.getClassName()), hashFilename(m.getSourceFile()), m.getStartLine(), m.getEndLine(), m.getStartBytecode(), m.getEndBytecode()); result.setDescription(m.getDescription()); return result; }
@Test public void testMethodSignature() { String sig = "([Lcom.google.Search;I)Lcom.google.Money;"; String result = Obfuscate.hashMethodSignature(sig); assertTrue("hash of " + sig + " gives " + result, result.indexOf("google") == -1); System.out.println(result); }
public static String hashMethodSignature(String signature) { SignatureParser parser = new SignatureParser(signature); StringBuilder buf = new StringBuilder("("); for (Iterator<String> i = parser.parameterSignatureIterator(); i.hasNext();) { String param = i.next(); buf.append(hashSignature(param)); } buf.append(")"); buf.append(hashSignature(parser.getReturnTypeSignature())); return buf.toString(); }
static ClassAnnotation obfuscate(ClassAnnotation m) { ClassAnnotation result = new ClassAnnotation(hashClass(m.getClassName())); result.setDescription(m.getDescription()); return result; }
static FieldAnnotation obfuscate(FieldAnnotation m) { FieldAnnotation result = new FieldAnnotation(hashClass(m.getClassName()), hashData(m.getFieldName()), hashSignature(m.getFieldSignature()), m.isStatic()); result.setDescription(m.getDescription()); return result; }
public static String hashFieldSignature(String signature) { signature = signature.substring(1, signature.length() - 1); if (!signature.startsWith("java")) { signature = "obfuscated/H" + hashData(signature); } return "L" + signature + ";"; }
static SourceLineAnnotation obfuscate(SourceLineAnnotation m) { SourceLineAnnotation result = new SourceLineAnnotation(hashClass(m.getClassName()), hashFilename(m.getSourceFile()), m.getStartLine(), m.getEndLine(), m.getStartBytecode(), m.getEndBytecode()); result.setDescription(m.getDescription()); return result; }
@Test public void testMethodSignatureDoesntChangeForCoreTypes() { String sig = "([Ljava/lang/String;I)Ljava/util/Map;"; String result = Obfuscate.hashMethodSignature(sig); assertEquals(sig, result); } }
static TypeAnnotation obfuscate(TypeAnnotation m) { TypeAnnotation result = new TypeAnnotation(hashSignature(m.getTypeDescriptor())); result.setDescription(m.getDescription()); return result; }
static ClassAnnotation obfuscate(ClassAnnotation m) { ClassAnnotation result = new ClassAnnotation(hashClass(m.getClassName())); result.setDescription(m.getDescription()); return result; }
static MethodAnnotation obfuscate(MethodAnnotation m) { String className = m.getClassName(); if (className.startsWith("java")) { return m; } String methodName = m.getMethodName(); String methodSignature = m.getMethodSignature(); if ("hashCode".equals(methodName) && "()I".equals(methodSignature) || "equals".equals(methodName) && "(Ljava/lang/Object;)Z".equals(methodSignature) || "compareTo".equals(methodName) && "(Ljava/lang/Object;)I".equals(methodSignature) || "<init>".equals(methodName) || "<clinit>".equals(methodName)) { // don't need to obfuscate method name } else { methodName = hashData(methodName); } MethodAnnotation result = new MethodAnnotation(hashClass(className), methodName, hashMethodSignature(methodSignature), m.isStatic()); result.setDescription(m.getDescription()); return result; }
@Override public void visitMethodAnnotation(MethodAnnotation methodAnnotation) { result.add(obfuscate(methodAnnotation)); }
public static String hashClass(@DottedClassName String className) { if (className.startsWith("java")) { return className; } return "obfuscated.H" + hashData(className); }
public static String hashSignature(String signature) { char firstChar = signature.charAt(0); switch (firstChar) { case '[': return '[' + hashSignature(signature.substring(1)); case 'V': case 'Z': case 'B': case 'S': case 'C': case 'I': case 'J': case 'D': case 'F': if (signature.length() == 1) { return signature; } throw new IllegalArgumentException("bad signature: " + signature); case 'L': if (!signature.endsWith(";")) { throw new IllegalArgumentException("bad signature: " + signature); } return hashFieldSignature(signature); default: throw new IllegalArgumentException("bad signature: " + signature); } }
public static String hashMethodSignature(String signature) { SignatureParser parser = new SignatureParser(signature); StringBuilder buf = new StringBuilder("("); for (Iterator<String> i = parser.parameterSignatureIterator(); i.hasNext();) { String param = i.next(); buf.append(hashSignature(param)); } buf.append(")"); buf.append(hashSignature(parser.getReturnTypeSignature())); return buf.toString(); }