private static String normalize(Type type) { return type.getClassName().replace('$', '.'); }
@Override public void visit( String name, Object value ) { if ( value instanceof Type ) { if ( ( ( Type ) value ).getClassName().contains( "AndroidJUnit4" ) ) { flagAsFound(); } } }
private static int parameterTypeUnsafe(MethodVisitor methodVisitor, Class<?> parameterType, boolean write) { parameterType = parameterType.isPrimitive() ? parameterType : Object.class; Type type = Type.getType(parameterType); String boolDescriptor = parameterType == boolean.class ? "Ljava/lang/Object;" : ""; methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Unsafe.class), (write ? "put" : "get") + Character.toUpperCase(type.getClassName().charAt(0)) + type.getClassName().substring(1), write ? ("(" + boolDescriptor + "J" + type.getDescriptor() + ")V") : ("(" + boolDescriptor + "J)" + type.getDescriptor()), false); return type.getSize(); }
public static MethodSignature parse(String internalString) { int parenStart = internalString.indexOf('('); int methodStart = internalString.lastIndexOf('/', parenStart); String className = internalString.substring(0, methodStart).replace('/', '.'); String methodName = internalString.substring(methodStart + 1, parenStart); String methodDescriptor = internalString.substring(parenStart); Type[] argumentTypes = Type.getArgumentTypes(methodDescriptor); String[] paramTypes = new String[argumentTypes.length]; for (int i = 0; i < argumentTypes.length; i++) { paramTypes[i] = argumentTypes[i].getClassName(); } final String returnType = Type.getReturnType(methodDescriptor).getClassName(); return new MethodSignature(className, methodName, paramTypes, returnType); }
public String toString() { // TODO: include modifiers, superType, interfaces return getType().getClassName(); } }
FieldInfo getFieldInfo(String name) { FieldInfo field = (FieldInfo)fieldInfo.get(name); if (field == null) { throw new IllegalArgumentException("Field " + name + " is not declared in " + getClassType().getClassName()); } return field; }
public boolean matches(final Signature signature) { if (this.owners.stream().filter(signature.owners::contains).findFirst().isPresent()) { if (this.name == null || signature.name.equals(this.name)) { if (this.args.length == signature.args.length) { for (int i = 0; i < this.args.length; i++) { if (!this.args[i].getClassName().equals(signature.args[i].getClassName())) { // return false; } } return true; } } } return false; }
@Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { String className = Type.getType(desc).getClassName(); annotations.add(className); return super.visitAnnotation(desc, visible); }
FieldInfo getFieldInfo(String name) { FieldInfo field = (FieldInfo)fieldInfo.get(name); if (field == null) { throw new IllegalArgumentException("Field " + name + " is not declared in " + getClassType().getClassName()); } return field; }
public String toString() { // TODO: include modifiers, superType, interfaces return getType().getClassName(); } }
private String getBehaviorSignCode(final String name, final String desc) { final Type methodType = Type.getMethodType(desc); final Collection<String> parameterClassNameArray = new ArrayList<String>(); if (null != methodType.getArgumentTypes()) { for (final Type parameterType : methodType.getArgumentTypes()) { parameterClassNameArray.add(parameterType.getClassName()); } } final String signCode = String.format( "%s#%s(%s)", targetJavaClassName, name, join(parameterClassNameArray, ",") ); return signCode; }
Class<?> toJavaType(Type t) throws ClassNotFoundException { switch (t.getSort()) { case Type.BOOLEAN: return boolean.class; case Type.BYTE: return byte.class; case Type.SHORT: return short.class; case Type.CHAR: return char.class; case Type.INT: return int.class; case Type.FLOAT: return float.class; case Type.LONG: return long.class; case Type.DOUBLE: return double.class; case Type.OBJECT: return Class.forName(t.getClassName()); case Type.ARRAY: return Class.forName(t.getDescriptor()); case Type.VOID: return void.class; } throw new RuntimeException(); } }
/** * Intercepts the method using the invokedynamic bytecode instruction available in Java 7+. * Should be called through interceptInvokeVirtualMethod, not directly. */ private void interceptInvokeVirtualMethodWithInvokeDynamic( ListIterator<AbstractInsnNode> instructions, MethodInsnNode targetMethod) { instructions.remove(); // remove the method invocation Type type = Type.getObjectType(targetMethod.owner); String description = targetMethod.desc; String owner = type.getClassName(); if (targetMethod.getOpcode() != Opcodes.INVOKESTATIC) { String thisType = type.getDescriptor(); description = "(" + thisType + description.substring(1); } instructions.add(new InvokeDynamicInsnNode(targetMethod.name, description, BOOTSTRAP_INTRINSIC, owner)); } }
@Override public void visitEnum(String name, String enumDesc, String value) { if (Type.getType(enumDesc).getClassName().equals(RedefinitionPolicy.class.getName())) { RedefinitionPolicy valueAsEnum = RedefinitionPolicy.valueOf(value); if (Type.getType(desc).getClassName().equals(FieldRedefinitionPolicy.class.getName())) { cv.visitAttribute(new SingleByteAttribute(FieldRedefinitionPolicy.class.getSimpleName(), (byte) valueAsEnum.ordinal())); } if (Type.getType(desc).getClassName().equals(MethodRedefinitionPolicy.class.getName())) { cv.visitAttribute(new SingleByteAttribute(MethodRedefinitionPolicy.class.getSimpleName(), (byte) valueAsEnum.ordinal())); } } super.visitEnum(name, desc, value); } };
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (isProtect(access)) { access = setPublic(access); } MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); if (!isQualifiedMethod(access, name, desc, methodInstructionTypeMap)) { return mv; } StringBuilder parameters = new StringBuilder(); Type[] types = Type.getArgumentTypes(desc); for (Type type : types) { parameters.append(type.getClassName()).append(","); } //remove the last "," if (parameters.length() > 0 && parameters.charAt(parameters.length() - 1) == ',') { parameters.deleteCharAt(parameters.length() - 1); } //record method number methodMap.put(className.replace('/', '.') + "." + name + "(" + parameters.toString() + ")", insertMethodCount.incrementAndGet()); return new MethodBodyInsertor(mv, className, desc, isStatic(access), String.valueOf(insertMethodCount.get()), name, access); }
public static String getClassName(Type type) { if (isPrimitive(type)) { return (String)rtransforms.get(type.getDescriptor()); } else if (isArray(type)) { return getClassName(getComponentType(type)) + "[]"; } else { return type.getClassName(); } }
public static String getClassName(Type type) { if (isPrimitive(type)) { return (String)rtransforms.get(type.getDescriptor()); } else if (isArray(type)) { return getClassName(getComponentType(type)) + "[]"; } else { return type.getClassName(); } }
private void addType(Type t) { switch (t.getSort()) { case Type.ARRAY: addType(t.getElementType()); break; case Type.OBJECT: parseClassName(t.getClassName().replace('.', '/')); break; default: // Do nothing break; } }
private void chooseMethodSignature() throws Exception { methodSignature = choice(getMethodSignatures()); int splitAt = methodSignature.indexOf('('); methodName = methodSignature.substring(0, splitAt); String methodDescriptor = methodSignature.substring(splitAt); Type[] asmTypes = Type.getArgumentTypes(methodDescriptor); params = new Class<?>[asmTypes.length]; //Get the corresponding class for each param type name for (int i = 0; i < asmTypes.length; i++) { params[i] = classForName(asmTypes[i].getClassName()); } }
private void chooseMethodSignature() throws Exception { methodDescriptor = choice(getMethodSignatures()); Type[] asmTypes = Type.getArgumentTypes(methodDescriptor); params = new Class<?>[asmTypes.length]; //Get the corresponding class for each param type name for (int i = 0; i < asmTypes.length; i++) { params[i] = classForName(asmTypes[i].getClassName()); } }