Java Code Examples for org.objectweb.asm.Type#getMethodType()

The following examples show how to use org.objectweb.asm.Type#getMethodType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Remapper.java    From Concurnas with MIT License 6 votes vote down vote up
/**
 * Returns the given {@link Type}, remapped with {@link #map(String)} or {@link
 * #mapMethodDesc(String)}.
 *
 * @param type a type, which can be a method type.
 * @return the given type, with its [array element type] internal name remapped with {@link
 *     #map(String)} (if the type is an array or object type, otherwise the type is returned as
 *     is) or, of the type is a method type, with its descriptor remapped with {@link
 *     #mapMethodDesc(String)}.
 */
private Type mapType(final Type type) {
  switch (type.getSort()) {
    case Type.ARRAY:
      StringBuilder remappedDescriptor = new StringBuilder();
      for (int i = 0; i < type.getDimensions(); ++i) {
        remappedDescriptor.append('[');
      }
      remappedDescriptor.append(mapType(type.getElementType()).getDescriptor());
      return Type.getType(remappedDescriptor.toString());
    case Type.OBJECT:
      String remappedInternalName = map(type.getInternalName());
      return remappedInternalName != null ? Type.getObjectType(remappedInternalName) : type;
    case Type.METHOD:
      return Type.getMethodType(mapMethodDesc(type.getDescriptor()));
    default:
      return type;
  }
}
 
Example 2
Source File: PeripheralCodeGenerator.java    From OpenPeripheral with MIT License 6 votes vote down vote up
private static void createConstructor(ClassWriter writer, String clsName, Type targetType, Type baseType) {
	final Type ctorType = Type.getMethodType(Type.VOID_TYPE, targetType);

	MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>", ctorType.getDescriptor(), null, null);
	init.visitCode();
	init.visitVarInsn(Opcodes.ALOAD, 0);
	init.visitVarInsn(Opcodes.ALOAD, 1);
	init.visitInsn(Opcodes.DUP2);
	init.visitMethodInsn(Opcodes.INVOKESPECIAL, baseType.getInternalName(), "<init>", SUPER_CTOR_TYPE.getDescriptor(), false);
	init.visitFieldInsn(Opcodes.PUTFIELD, clsName, CommonMethodsBuilder.TARGET_FIELD_NAME, targetType.getDescriptor());
	init.visitInsn(Opcodes.RETURN);

	init.visitMaxs(0, 0);

	init.visitEnd();
}
 
Example 3
Source File: Remapper.java    From bytecode-viewer with GNU General Public License v3.0 6 votes vote down vote up
private Type mapType(Type t) {
    switch (t.getSort()) {
        case Type.ARRAY:
            String s = mapDesc(t.getElementType().getDescriptor());
            for (int i = 0; i < t.getDimensions(); ++i) {
                s = '[' + s;
            }
            return Type.getType(s);
        case Type.OBJECT:
            s = map(t.getInternalName());
            return s != null ? Type.getObjectType(s) : t;
        case Type.METHOD:
            return Type.getMethodType(mapMethodDesc(t.getDescriptor()));
    }
    return t;
}
 
Example 4
Source File: InterfaceDesugaring.java    From bazel with Apache License 2.0 6 votes vote down vote up
private void generateStub(MethodVisitor stubMethod, String calledMethodName, String desc) {
  int slot = 0;
  Type neededType = Type.getMethodType(desc);
  for (Type arg : neededType.getArgumentTypes()) {
    stubMethod.visitVarInsn(arg.getOpcode(Opcodes.ILOAD), slot);
    slot += arg.getSize();
  }
  stubMethod.visitMethodInsn(
      Opcodes.INVOKESTATIC,
      getCompanionClassName(internalName),
      calledMethodName,
      desc,
      /*isInterface=*/ false);
  stubMethod.visitInsn(neededType.getReturnType().getOpcode(Opcodes.IRETURN));
  stubMethod.visitMaxs(slot, slot);
  stubMethod.visitEnd();
}
 
Example 5
Source File: DescParser.java    From Recaf with MIT License 6 votes vote down vote up
@Override
public DescAST visit(int lineNo, String line) throws ASTParseException {
	try {
		String trim = line.trim();
		// Verify
		if(trim.contains("(")) {
			Type type = Type.getMethodType(trim);
			if(!validate(type.getReturnType().getDescriptor()))
				throw new ASTParseException(lineNo,
						"Invalid method return type " + type.getReturnType().getDescriptor());
			for(Type arg : type.getArgumentTypes())
				if(!validate(arg.getDescriptor()))
					throw new ASTParseException(lineNo,
							"Invalid method arg type " + arg.getDescriptor());
		} else {
			if(!validate(trim))
				throw new ASTParseException(lineNo, "Invalid field descriptor: " + trim);
		}
		// Create AST
		int start = line.indexOf(trim);
		return new DescAST(lineNo, getOffset() + start, trim);
	} catch(Exception ex) {
		throw new ASTParseException(ex, lineNo, "Bad format for descriptor: " + ex.getMessage());
	}
}
 
Example 6
Source File: LambdaDesugaring.java    From bazel with Apache License 2.0 6 votes vote down vote up
private Executable findTargetMethod(Handle invokedMethod) throws ClassNotFoundException {
  Type descriptor = Type.getMethodType(invokedMethod.getDesc());
  Class<?> owner = loadFromInternal(invokedMethod.getOwner());
  if (invokedMethod.getTag() == Opcodes.H_NEWINVOKESPECIAL) {
    for (Constructor<?> c : owner.getDeclaredConstructors()) {
      if (Type.getType(c).equals(descriptor)) {
        return c;
      }
    }
  } else {
    for (Method m : owner.getDeclaredMethods()) {
      if (m.getName().equals(invokedMethod.getName()) && Type.getType(m).equals(descriptor)) {
        return m;
      }
    }
  }
  throw new IllegalArgumentException("Referenced method not found: " + invokedMethod);
}
 
Example 7
Source File: Remapper.java    From JReFrameworker with MIT License 6 votes vote down vote up
/**
 * Returns the given {@link Type}, remapped with {@link #map(String)} or {@link
 * #mapMethodDesc(String)}.
 *
 * @param type a type, which can be a method type.
 * @return the given type, with its [array element type] internal name remapped with {@link
 *     #map(String)} (if the type is an array or object type, otherwise the type is returned as
 *     is) or, of the type is a method type, with its descriptor remapped with {@link
 *     #mapMethodDesc(String)}.
 */
private Type mapType(final Type type) {
  switch (type.getSort()) {
    case Type.ARRAY:
      StringBuilder remappedDescriptor = new StringBuilder();
      for (int i = 0; i < type.getDimensions(); ++i) {
        remappedDescriptor.append('[');
      }
      remappedDescriptor.append(mapType(type.getElementType()).getDescriptor());
      return Type.getType(remappedDescriptor.toString());
    case Type.OBJECT:
      String remappedInternalName = map(type.getInternalName());
      return remappedInternalName != null ? Type.getObjectType(remappedInternalName) : type;
    case Type.METHOD:
      return Type.getMethodType(mapMethodDesc(type.getDescriptor()));
    default:
      return type;
  }
}
 
Example 8
Source File: Remapper.java    From JReFrameworker with MIT License 6 votes vote down vote up
/**
 * Returns the given {@link Type}, remapped with {@link #map(String)} or {@link
 * #mapMethodDesc(String)}.
 *
 * @param type a type, which can be a method type.
 * @return the given type, with its [array element type] internal name remapped with {@link
 *     #map(String)} (if the type is an array or object type, otherwise the type is returned as
 *     is) or, of the type is a method type, with its descriptor remapped with {@link
 *     #mapMethodDesc(String)}.
 */
private Type mapType(final Type type) {
  switch (type.getSort()) {
    case Type.ARRAY:
      StringBuilder remappedDescriptor = new StringBuilder();
      for (int i = 0; i < type.getDimensions(); ++i) {
        remappedDescriptor.append('[');
      }
      remappedDescriptor.append(mapType(type.getElementType()).getDescriptor());
      return Type.getType(remappedDescriptor.toString());
    case Type.OBJECT:
      String remappedInternalName = map(type.getInternalName());
      return remappedInternalName != null ? Type.getObjectType(remappedInternalName) : type;
    case Type.METHOD:
      return Type.getMethodType(mapMethodDesc(type.getDescriptor()));
    default:
      return type;
  }
}
 
Example 9
Source File: JavaConstant.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Object asConstantPoolValue() {
    StringBuilder stringBuilder = new StringBuilder().append('(');
    for (TypeDescription parameterType : getParameterTypes()) {
        stringBuilder.append(parameterType.getDescriptor());
    }
    return Type.getMethodType(stringBuilder.append(')').append(getReturnType().getDescriptor()).toString());
}
 
Example 10
Source File: PublicSignatureCollector.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
    String[] exceptions) {
  if (isVisibleMember(access)) {
    Type method = Type.getMethodType(descriptor);
    List<String> argumentTypes = Arrays.stream(method.getArgumentTypes()).map(Type::getClassName)
        .collect(Collectors.toList());
    currentMethods
        .add(describeMethod(name, access, method.getReturnType().getClassName(), argumentTypes));
  }
  return null;
}
 
Example 11
Source File: RunMethod.java    From rembulan with Apache License 2.0 5 votes vote down vote up
private Type methodType(Type returnType) {
	ArrayList<Type> args = new ArrayList<>();

	args.add(Type.getType(ExecutionContext.class));
	args.add(Type.INT_TYPE);
	if (context.isVararg()) {
		args.add(ASMUtils.arrayTypeFor(Object.class));
	}
	for (int i = 0; i < numOfRegisters(); i++) {
		args.add(Type.getType(Object.class));
	}
	return Type.getMethodType(returnType, args.toArray(new Type[0]));
}
 
Example 12
Source File: RunMethod.java    From rembulan with Apache License 2.0 5 votes vote down vote up
private Type snapshotMethodType() {
	ArrayList<Type> args = new ArrayList<>();

	args.add(Type.INT_TYPE);
	if (context.isVararg()) {
		args.add(ASMUtils.arrayTypeFor(Object.class));
	}
	for (int i = 0; i < numOfRegisters(); i++) {
		args.add(Type.getType(Object.class));
	}
	return Type.getMethodType(context.savedStateClassType(), args.toArray(new Type[0]));
}
 
Example 13
Source File: ReflectionUtils.java    From rembulan with Apache License 2.0 5 votes vote down vote up
public Type getMethodType() {
	Type[] ts = new Type[args.length];
	for (int i = 0; i < args.length; i++) {
		ts[i] = Type.getType(args[i]);
	}
	return Type.getMethodType(
			Type.getType(returnType),
			ts);
}
 
Example 14
Source File: PreWorldRenderHookVisitor.java    From OpenModsLib with MIT License 5 votes vote down vote up
public PreWorldRenderHookVisitor(String rendererTypeCls, ClassVisitor cv, IResultListener listener) {
	super(Opcodes.ASM5, cv);
	this.listener = listener;

	Type injectedMethodType = Type.getMethodType(Type.VOID_TYPE, Type.INT_TYPE, Type.FLOAT_TYPE, Type.LONG_TYPE);
	modifiedMethod = new MethodMatcher(rendererTypeCls, injectedMethodType.getDescriptor(), "renderWorldPass", "func_175068_a");
}
 
Example 15
Source File: PlayerRendererHookVisitor.java    From OpenModsLib with MIT License 5 votes vote down vote up
public PlayerRendererHookVisitor(String rendererTypeCls, ClassVisitor cv, IResultListener listener) {
	super(Opcodes.ASM5, cv);
	this.listener = listener;

	Type injectedMethodType = Type.getMethodType(Type.VOID_TYPE, MappedType.of(AbstractClientPlayer.class).type(), Type.FLOAT_TYPE, Type.FLOAT_TYPE, Type.FLOAT_TYPE);
	modifiedMethod = new MethodMatcher(rendererTypeCls, injectedMethodType.getDescriptor(), "applyRotations", "func_77043_a");
}
 
Example 16
Source File: MethodGenerator.java    From SonarPet with GNU General Public License v3.0 4 votes vote down vote up
public MethodGenerator(MethodVisitor methodVisitor, int access, String name, String desc) {
    super(ASM5, access, desc, methodVisitor);
    this.access = access;
    this.name = name;
    this.methodType = Type.getMethodType(desc);
}
 
Example 17
Source File: ConstructorMethod.java    From rembulan with Apache License 2.0 4 votes vote down vote up
public Type methodType() {
	Type[] args = new Type[context.fn.upvals().size()];
	Arrays.fill(args, Type.getType(Variable.class));
	return Type.getMethodType(Type.VOID_TYPE, args);
}
 
Example 18
Source File: ObjectCodeGenerator.java    From OpenPeripheral with MIT License 4 votes vote down vote up
private static void createDummyConstructor(ClassWriter writer, String clsName, Type targetType) {
	final Type ctorType = Type.getMethodType(Type.VOID_TYPE);

	MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>", ctorType.getDescriptor(), null, null);
	init.visitCode();

	visitSuperCtor(init);

	init.visitInsn(Opcodes.ACONST_NULL);
	init.visitFieldInsn(Opcodes.PUTFIELD, clsName, CommonMethodsBuilder.TARGET_FIELD_NAME, targetType.getDescriptor());
	init.visitInsn(Opcodes.RETURN);

	init.visitMaxs(0, 0);

	init.visitEnd();
}
 
Example 19
Source File: ObjectCodeGenerator.java    From OpenPeripheral with MIT License 4 votes vote down vote up
private static void createDefaultConstructor(ClassWriter writer, String clsName, Type targetType) {
	final Type ctorType = Type.getMethodType(Type.VOID_TYPE, targetType);

	MethodVisitor init = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, "<init>", ctorType.getDescriptor(), null, null);
	init.visitCode();

	visitSuperCtor(init);

	init.visitVarInsn(Opcodes.ALOAD, 1);
	init.visitFieldInsn(Opcodes.PUTFIELD, clsName, CommonMethodsBuilder.TARGET_FIELD_NAME, targetType.getDescriptor());
	init.visitInsn(Opcodes.RETURN);

	init.visitMaxs(0, 0);

	init.visitEnd();
}
 
Example 20
Source File: NamespaceMapper.java    From AVM with MIT License 2 votes vote down vote up
/**
 * @param type The pre-transform method type.
 * @return The post-transform method type.
 */
public Type mapMethodType(Type type, boolean preserveDebuggability) {
    return Type.getMethodType(mapDescriptor(type.getDescriptor(), preserveDebuggability));
}