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

The following examples show how to use org.objectweb.asm.Type#getArgumentTypes() . 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: ConstantTracker.java    From zelixkiller with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Inline method arguments
 */
public ConstantTracker(boolean isStatic, String descr, Object[] args) {
	super(ASM5);
	this.desc = Type.getArgumentTypes(descr);
	ArrayList<Object> reformatted = new ArrayList<>();
	int local = isStatic ? 0 : 1;
	if (isStatic) {
		reformatted.set(0, null); // this reference
	}
	for (int i = 0; i < desc.length; ++i) {
		reformatted.set(local++, i >= args.length ? null : args[i]);
		if (desc[i].getSize() == 2) {
			reformatted.set(local++, null);
		}
	}
	this.args = reformatted.toArray(new Object[0]);
}
 
Example 2
Source File: PrefixReferenceScanner.java    From bazel with Apache License 2.0 6 votes vote down vote up
void typeReference(Type type) {
  switch (type.getSort()) {
    case Type.ARRAY:
      typeReference(type.getElementType());
      break;
    case Type.OBJECT:
      classReference(type.getInternalName());
      break;

    case Type.METHOD:
      for (Type param : type.getArgumentTypes()) {
        typeReference(param);
      }
      typeReference(type.getReturnType());
      break;

    default:
      break;
  }
}
 
Example 3
Source File: Instrumentator.java    From instrumentation with Apache License 2.0 6 votes vote down vote up
private boolean modifyMethod(MethodNode mn) {

        if (!this.interceptor.interceptMethod(this.cn, mn) || Helper.isAbstract(mn)) {
            return false;
        }
        this.mn = mn;
        this.methodArguments = Type.getArgumentTypes(this.mn.desc);
        this.methodReturnType = Type.getReturnType(this.mn.desc);
        this.methodOffset = isStatic() ? 0 : 1;

        addTraceStart();
        addTraceReturn();
        addTraceThrow();
        addTraceThrowablePassed();

        return true;
    }
 
Example 4
Source File: ClassAnalyzer.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private List<ThinMethod> getPossibleTargetMethods(ThinMethod bridgeMethod) {
    List<ThinMethod> possibleTargetMethods = Lists.newArrayList();
    for (ThinMethod possibleTargetMethod : thinClass.nonBridgeMethods()) {
        if (!possibleTargetMethod.name().equals(bridgeMethod.name())) {
            continue;
        }
        Type[] bridgeMethodParamTypes = Type.getArgumentTypes(bridgeMethod.descriptor());
        Type[] possibleTargetMethodParamTypes =
                Type.getArgumentTypes(possibleTargetMethod.descriptor());
        if (possibleTargetMethodParamTypes.length != bridgeMethodParamTypes.length) {
            continue;
        }
        possibleTargetMethods.add(possibleTargetMethod);
    }
    return possibleTargetMethods;
}
 
Example 5
Source File: Virtualizer.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
private static String getVMMethodDesc(String desc) {
    Type[] types = Type.getArgumentTypes(desc);
    StringBuilder sb = new StringBuilder();

    if (types.length == 0)
        sb.append("\u0000\u0000\u0000");
    else
        Arrays.stream(types).forEach(type -> {
            if (type.getSort() == Type.ARRAY)
                sb.append(type.getInternalName().replace('/', '.'));
            else
                sb.append(type.getClassName());

            sb.append("\u0001\u0001");
        });

    return sb.toString();
}
 
Example 6
Source File: ClassPrinter.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MethodVisitor visitMethod(int access, String name,
								 String desc, String signature,
								 String[] exceptions)
{
	String ret = Type.getReturnType(desc).getClassName();
	Type[] args = Type.getArgumentTypes(desc);
	System.out.println(" " + ret + " "+ name+"("+
					   Arrays.toString(args)+"): "+desc);
	return null; // don't visit method
}
 
Example 7
Source File: BcUtils.java    From Concurnas with MIT License 5 votes vote down vote up
public static int incArgPointsFromDesc(String desc, int access) {
	Type[] args = Type.getArgumentTypes(desc);
	int incon = 0;
	for (int n = 0; n < args.length; n++) {
		incon += args[n].getSize();
	}

	if (!Modifier.isStatic(access)) {// not static thus first var is self reference
		incon++;
	}
	return incon;
}
 
Example 8
Source File: LocalVariableTableParameterNameDiscoverer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public LocalVariableTableVisitor(Class<?> clazz, Map<Member, String[]> map, String name, String desc,
                                 boolean isStatic) {
    super(Opcodes.ASM7);
    this.clazz = clazz;
    this.memberMap = map;
    this.name = name;
    this.args = Type.getArgumentTypes(desc);
    this.parameterNames = new String[this.args.length];
    this.isStatic = isStatic;
    this.lvtSlotIndex = computeLvtSlotIndices(isStatic, this.args);
}
 
Example 9
Source File: CheckParserUsagesDT.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private Collection<String> getParameterAndReturnTypes(String descriptor) {
    Set<String> types = new HashSet<>();
    Type type = Type.getType(descriptor);
    types.add(NodeClass.typeToString(type.getReturnType()));
    if (type.getSort() == Type.METHOD) {
        for (Type argumentType : type.getArgumentTypes()) {
            types.add(NodeClass.typeToString(argumentType));
        }
    }
    return types;
}
 
Example 10
Source File: BasicVerifier.java    From Concurnas with MIT License 5 votes vote down vote up
@Override
public BasicValue naryOperation(
    final AbstractInsnNode insn, final List<? extends BasicValue> values)
    throws AnalyzerException {
  int opcode = insn.getOpcode();
  if (opcode == MULTIANEWARRAY) {
    for (BasicValue value : values) {
      if (!BasicValue.INT_VALUE.equals(value)) {
        throw new AnalyzerException(insn, null, BasicValue.INT_VALUE, value);
      }
    }
  } else {
    int i = 0;
    int j = 0;
    if (opcode != INVOKESTATIC && opcode != INVOKEDYNAMIC) {
      Type owner = Type.getObjectType(((MethodInsnNode) insn).owner);
      if (!isSubTypeOf(values.get(i++), newValue(owner))) {
        throw new AnalyzerException(insn, "Method owner", newValue(owner), values.get(0));
      }
    }
    String methodDescriptor =
        (opcode == INVOKEDYNAMIC)
            ? ((InvokeDynamicInsnNode) insn).desc
            : ((MethodInsnNode) insn).desc;
    Type[] args = Type.getArgumentTypes(methodDescriptor);
    while (i < values.size()) {
      BasicValue expected = newValue(args[j++]);
      BasicValue actual = values.get(i++);
      if (!isSubTypeOf(actual, expected)) {
        throw new AnalyzerException(insn, "Argument " + j, expected, actual);
      }
    }
  }
  return super.naryOperation(insn, values);
}
 
Example 11
Source File: AnalyzerAdapter.java    From JReFrameworker with MIT License 5 votes vote down vote up
private void pop(final String descriptor) {
  char firstDescriptorChar = descriptor.charAt(0);
  if (firstDescriptorChar == '(') {
    int numSlots = 0;
    Type[] types = Type.getArgumentTypes(descriptor);
    for (Type type : types) {
      numSlots += type.getSize();
    }
    pop(numSlots);
  } else if (firstDescriptorChar == 'J' || firstDescriptorChar == 'D') {
    pop(2);
  } else {
    pop(1);
  }
}
 
Example 12
Source File: DescDeal.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
public static String getSimplifyMethodDesc(String desc) {
    StringBuilder sb = new StringBuilder();
    Type methodType = Type.getMethodType(desc);
    Type[] types = methodType.getArgumentTypes();
    for (Type type : types) {
        sb.append(getName(type.getClassName()));
        sb.append(",");
    }
    String result = sb.toString();
    if (result.endsWith(",")) {
        return result.substring(0, result.length() - 1);
    }
    return result;
}
 
Example 13
Source File: NameTranslatorClassVisitor.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void visitMethodInsn(final int opcode, final String owner,
		final String name, final String desc) {

	// Special case method invocations for name translation.
	// Specifically to deal with methods mirrors.
	String newOwner = owner;
	int newOpcode = opcode;
	String newDesc = translator.translateMethodDescriptor(desc);
	
	String lookupOwner = owner;
	while (lookupOwner.startsWith("[")) {
		lookupOwner = lookupOwner.substring(1);
	}
	final Mirror mirror = translator.getMirror(lookupOwner);

	if (mirror.isClassMirror()) {
		newOwner = translator.translate(owner);
	} else if ("<init>".equals(name)&&(opcode == Opcodes.INVOKESPECIAL)) {
		/* Look for an equivalent constructor. For instance,
		 * INVOKESPECIAL, "java/math/BigDecimal", "<init>", "(I)V")
		 * will be transformed as
		 * INVOKESTATIC, "../BigDecimal_", "BigDecimal", "(I)Ljava/math/BigDecimal;"
		 * 
		 * the previously constructed object was on top of the stack and the mirror
		 * function has put its result on top, so a SWAP and POP are issued to
		 * discard the previously constructed object and store the new one instead.
		 */
		String constructorDesc = newDesc.substring(0, newDesc.length()-1) + 'L' + owner + ';';
		String constructorName;
		int i = owner.lastIndexOf('/');
		if (i == -1) {
			constructorName = owner;
		} else {
			constructorName = owner.substring(i+1);
		}
		if (mirror.hasMethod(owner, constructorName, constructorDesc, Opcodes.INVOKESPECIAL)) {
			newOwner = translator.translate(owner);

			super.visitMethodInsn(Opcodes.INVOKESTATIC, newOwner, constructorName,
					constructorDesc);

			super.visitInsn(Opcodes.SWAP);
			super.visitInsn(Opcodes.POP);
			super.visitInsn(Opcodes.SWAP);
			super.visitInsn(Opcodes.POP);
			return;
		}
	} else if (mirror.hasMethod(owner, name, newDesc, opcode)) {
		newOwner = translator.translate(owner);
		newOpcode = Opcodes.INVOKESTATIC;

		// We have to insert the owner into the arguments of the
		// descriptor
		if (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKEINTERFACE) {
			final Type[] argTypes = Type.getArgumentTypes(newDesc);
			final Type[] newArgTypes = new Type[argTypes.length + 1];
			newArgTypes[0] = Type.getType("L" + owner + ";");
			System.arraycopy(argTypes, 0, newArgTypes, 1, argTypes.length);
			newDesc = Type.getMethodDescriptor(
					Type.getReturnType(newDesc), newArgTypes);
			newDesc = translator.translateMethodDescriptor(newDesc);
		}
	}

	super.visitMethodInsn(newOpcode, newOwner, name, newDesc);
}
 
Example 14
Source File: MethodKey.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** The formal parameter types of a method. */
public Type[] getArgumentTypeArray() {
  return Type.getArgumentTypes(descriptor());
}
 
Example 15
Source File: CachingGenerator.java    From grappa with Apache License 2.0 4 votes vote down vote up
private void generateGetFromCache(final CodeBlock block)
{
    final Type[] paramTypes = Type.getArgumentTypes(method.desc);
    cacheFieldName = findUnusedCacheFieldName();

    // if we have no parameters we use a simple Rule field as cache,
    // otherwise a HashMap
    final String cacheFieldDesc = paramTypes.length == 0
        ? CodegenUtils.ci(Rule.class)
        : CodegenUtils.ci(HashMap.class);
    final FieldNode field = new FieldNode(ACC_PRIVATE, cacheFieldName,
        cacheFieldDesc, null, null);

    classNode.fields.add(field);

    block.aload(0).getfield(classNode.name, cacheFieldName, cacheFieldDesc);

    if (paramTypes.length == 0)
        return; // if we have no parameters we are done

    // generate: if (<cache> == null) <cache> = new HashMap<Object, Rule>();

    final LabelNode alreadyInitialized = new LabelNode();

    block.dup()
        .ifnonnull(alreadyInitialized)
        .pop()
        .aload(0)
        .newobj(CodegenUtils.p(HashMap.class)).dup_x1().dup()
        .invokespecial(CodegenUtils.p(HashMap.class), "<init>",
            CodegenUtils.sig(void.class))
        .putfield(classNode.name, cacheFieldName, cacheFieldDesc)
        .label(alreadyInitialized);

    // if we have more than one parameter or the parameter is an array we
    // have to wrap with our Arguments class since we need to unroll all
    // inner arrays and apply custom hashCode(...) and equals(...)
    // implementations
    if (paramTypes.length > 1 || paramTypes[0].getSort() == Type.ARRAY) {
        // generate: push new Arguments(new Object[] {<params>})

        block.newobj(CodegenUtils.p(CacheArguments.class)).dup();

        generatePushNewParameterObjectArray(block, paramTypes);

        block.invokespecial(CodegenUtils.p(CacheArguments.class), "<init>",
            CodegenUtils.sig(void.class, Object[].class));
    } else {
        generatePushParameterAsObject(block, paramTypes, 0);
    }

    // generate: <hashMap>.get(...)

    block.dup().astore(method.maxLocals)
        .invokevirtual(CodegenUtils.p(HashMap.class), "get",
            CodegenUtils.sig(Object.class, Object.class));
}
 
Example 16
Source File: SignaturePrinter.java    From Mixin with MIT License 4 votes vote down vote up
public SignaturePrinter(MethodNode method, String[] argNames) {
    this(method.name, Type.VOID_TYPE, Type.getArgumentTypes(method.desc), argNames);
    this.setModifiers(method);
}
 
Example 17
Source File: HierarchyMethods.java    From maple-ir with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Finds methods in cn matching name and desc.
 * @param cn ClassNode
 * @param name name of method
 * @param desc method descriptor
 * @param returnTypes One of ANY_TYPE, CONGRUENT_TYPES, EXACT_TYPES, or LOOSELY_RELATED_TYPES
 * @param allowedMask Mask of allowed attributes for modifiers; bit 1 = allowed, 0 = not allowed
 * @param requiredMask Mask of required attributes for modifiers; bit 1 = allowed, 0 = not allowed
 * @return Set of methods matching specifications
 */
private Set<MethodNode> findMethods(ClassNode cn, String name, String desc, int returnTypes, int allowedMask, int requiredMask) {
	allowedMask |= requiredMask;
	Set<MethodNode> findM = new HashSet<>();
	
	Type[] expectedParams = Type.getArgumentTypes(desc);
	
	for(MethodNode m : cn.getMethods()) {
		// no bits set in m.access that aren't in allowedMask
		// no bits unset in m.access that are in requiredMask
		if(((m.node.access ^ allowedMask) & m.node.access) == 0 && ((m.node.access ^ requiredMask) & requiredMask) == 0) {
			if (!Modifier.isStatic(allowedMask) && Modifier.isStatic(m.node.access))
				throw new IllegalStateException("B0i");
			if (!Modifier.isAbstract(allowedMask) && Modifier.isAbstract(m.node.access))
				throw new IllegalStateException("B0i");
			if (!isBridge(allowedMask) && isBridge(m.node.access))
				throw new IllegalStateException("B0i");
			if (Modifier.isStatic(requiredMask) && !Modifier.isStatic(m.node.access))
				throw new IllegalStateException("B0i");

			if (!m.getName().equals(name) || !Arrays.equals(expectedParams, Type.getArgumentTypes(m.getDesc()))) {
				continue;
			}

			switch(returnTypes) {
				case ANY_TYPES:
					break;
				case CONGRUENT_TYPES:
					if (!areTypesCongruent(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
						continue;
					}
					break;
				case EXACT_TYPES:
					if (!desc.equals(m.getDesc())) {
						continue;
					}
					break;
				case LOOSELY_RELATED_TYPES:
					if(!areTypesLooselyRelated(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
						continue;
					}
					break;
			}
			
			// sanity check
			if (returnTypes == EXACT_TYPES && !isBridge(allowedMask) && !findM.isEmpty()) {
				System.err.println("==findM==");
				debugCong(desc, findM.iterator().next().getDesc());
				System.err.println("==m==");
				debugCong(desc, m.getDesc());
				
				{
					byte[] bs = ClassHelper.toByteArray(cn);
					
					try {
						FileOutputStream fos = new FileOutputStream(new File("out/broken.class"));
						fos.write(bs);
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
				
				throw new IllegalStateException(String.format("%s contains %s(br=%b) and %s(br=%b)", cn.getName(), findM, isBridge(findM.iterator().next().node.access), m, isBridge(m.node.access)));
			}
			findM.add(m);
		}
	}
	
	return findM;
}
 
Example 18
Source File: Signature.java    From cglib with Apache License 2.0 4 votes vote down vote up
public Type[] getArgumentTypes() {
    return Type.getArgumentTypes(desc);
}
 
Example 19
Source File: SerianalyzerConfig.java    From serianalyzer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param ref
 * @return
 */
private static boolean isSetter ( MethodReference ref ) {
    Type[] args = Type.getArgumentTypes(ref.getSignature());
    return !ref.isStatic() && ref.getMethod().startsWith("set") && ref.getMethod().length() > 3
            && Character.isUpperCase(ref.getMethod().charAt(3)) && ref.getSignature().endsWith(")V") && args.length == 1;
}
 
Example 20
Source File: Method.java    From JReFrameworker with MIT License 2 votes vote down vote up
/**
 * Returns the argument types of the method described by this object.
 *
 * @return the argument types of the method described by this object.
 */
public Type[] getArgumentTypes() {
  return Type.getArgumentTypes(descriptor);
}