javassist.bytecode.Descriptor Java Examples

The following examples show how to use javassist.bytecode.Descriptor. 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: ReparentClassTransformer.java    From DroidAssist with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean execute(CtClass inputClass, String inputClassName, MethodCall methodCall)
        throws CannotCompileException, NotFoundException {
    if (methodCall.isSuper()) {
        String signature = methodCall.getSignature();
        boolean voidType =
                Descriptor.getReturnType(signature, classPool) == CtClass.voidType;
        String methodName = methodCall.getMethodName();

        Logger.warning(getCategoryName() + " reset parent for class " + inputClassName
                + " adapt super method call" +
                " at " + inputClassName + ".java" + ":" + methodCall.getLineNumber());

        methodCall.replace((!voidType ? "$_=" : "") + "super." + methodName + "($$);");
        return true;
    }
    return false;
}
 
Example #2
Source File: CodeBuilder.java    From anno4j with Apache License 2.0 6 votes vote down vote up
public CodeBuilder insertMethod(String name, Class<?>[] params) {
	CtClass cc = klass.getCtClass();
	String className = Descriptor.toJavaName(Descriptor.toJvmName(cc));
	List<Class<?>> list = Arrays.asList(params);
	CodeBuilder cb = klass.getCodeBuilder();
	Map<List<Class<?>>, String> map = cb.methodTemplateVars.get(name);
	if (map == null) {
		cb.methodTemplateVars.put(name, map = new HashMap());
	} else {
		if (map.containsKey(list)) {
			body.append(map.get(list));
			return this;
		}
	}
	String parameterTypes = declareVar(params, cb);
	String var = cb.getVarName("Method");
	CodeBuilder field = klass.assignStaticField(Method.class, var);
	field.insertObjectClass(className);
	field.code(".getDeclaredMethod(").insert(name);
	field.code(", ").code(parameterTypes).code(")").end();
	map.put(list, var);
	body.append(var);
	return this;
}
 
Example #3
Source File: ClassFactory.java    From anno4j with Apache License 2.0 6 votes vote down vote up
CtClass get(Class<?> type) throws ObjectCompositionException {
	ClassPool cp = getClassPool();
	if (type.isPrimitive()) {
		return getPrimitive(type);
	}
	try {
		if (type.isArray())
			return Descriptor.toCtClass(type.getName(), cp);
		return cp.get(type.getName());
	} catch (NotFoundException e) {
		try {
			ClassLoader cl = type.getClassLoader();
			if (cl == null)
				throw new ObjectCompositionException(e);
			appendClassLoader(cl);
			if (type.isArray())
				return Descriptor.toCtClass(type.getName(), cp);
			return cp.get(type.getName());
		} catch (NotFoundException e1) {
			throw new ObjectCompositionException(e);
		}
	}
}
 
Example #4
Source File: ClassFactory.java    From anno4j with Apache License 2.0 6 votes vote down vote up
Class<?> getJavaClass(CtClass cc) throws ClassNotFoundException {
	if (cc.isPrimitive()) {
		if (cc.equals(CtClass.booleanType))
			return Boolean.TYPE;
		if (cc.equals(CtClass.byteType))
			return Byte.TYPE;
		if (cc.equals(CtClass.charType))
			return Character.TYPE;
		if (cc.equals(CtClass.doubleType))
			return Double.TYPE;
		if (cc.equals(CtClass.floatType))
			return Float.TYPE;
		if (cc.equals(CtClass.intType))
			return Integer.TYPE;
		if (cc.equals(CtClass.longType))
			return Long.TYPE;
		if (cc.equals(CtClass.shortType))
			return Short.TYPE;
		throw new AssertionError();
	}
	String name = Descriptor.toJavaName(Descriptor.toJvmName(cc));
	return classForName(name);
}
 
Example #5
Source File: JavassistAdapter.java    From panda with Apache License 2.0 6 votes vote down vote up
private List<String> splitDescriptorToTypeNames(String descriptors) {
    List<String> result = new ArrayList<>();

    if (descriptors != null && descriptors.length() != 0) {
        List<Integer> indices = new ArrayList<>();
        Descriptor.Iterator iterator = new Descriptor.Iterator(descriptors);

        while (iterator.hasNext()) {
            indices.add(iterator.next());
        }

        indices.add(descriptors.length());

        for (int i = 0; i < indices.size() - 1; i++) {
            String s1 = Descriptor.toString(descriptors.substring(indices.get(i), indices.get(i + 1)));
            result.add(s1);
        }
    }

    return result;
}
 
Example #6
Source File: MemberValue.java    From ModTheSpire with MIT License 5 votes vote down vote up
private static String convertFromArray(String classname)
{
    int index = classname.indexOf("[]");
    if (index != -1) {
        String rawType = classname.substring(0, index);
        StringBuffer sb = new StringBuffer(Descriptor.of(rawType));
        while (index != -1) {
            sb.insert(0, "[");
            index = classname.indexOf("[]", index + 1);
        }
        return sb.toString().replace('/', '.');
    }
    return classname;
}
 
Example #7
Source File: CodeBuilder.java    From anno4j with Apache License 2.0 5 votes vote down vote up
private CodeBuilder insert(CtClass cc) {
	if (cc.isPrimitive()) {
		body.append(getPrimitiveJavaClassWrapper(cc).getName())
				.append(".TYPE");
		return this;
	}
	return insertObjectClass(Descriptor.toJavaName(Descriptor.toJvmName(cc)));
}
 
Example #8
Source File: JavassistUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
/**
 * This class will find the method in the CtClass, and return it as a CtMethod.
 *
 * @throws NotFoundException
 */
public static CtMethod findMethod(final CtClass clz, final Method m) throws NotFoundException {
  final ClassPool pool = ClassPool.getDefault();
  final Class<?>[] paramTypes = m.getParameterTypes();
  final List<CtClass> paramTypesCtClass = new ArrayList<>();
  for (final Class<?> claz : paramTypes) {
    paramTypesCtClass.add(pool.get(claz.getName()));
  }
  final String desc =
      Descriptor.ofMethod(
          pool.get(m.getReturnType().getName()),
          paramTypesCtClass.toArray(new CtClass[] {}));
  final CtMethod method = clz.getMethod(m.getName(), desc);
  return method;
}
 
Example #9
Source File: SourceTargetTransformer.java    From DroidAssist with Apache License 2.0 4 votes vote down vote up
protected CtClass getSourceReturnType() throws NotFoundException {
    if (sourceReturnType == null) {
        sourceReturnType = Descriptor.getReturnType(sourceSpec.getSignature(), classPool);
    }
    return sourceReturnType;
}
 
Example #10
Source File: SourceTargetTransformer.java    From DroidAssist with Apache License 2.0 4 votes vote down vote up
protected boolean isVoidSourceReturnType() throws NotFoundException {
    return Descriptor.getReturnType(sourceSpec.getSignature(), classPool) == CtClass.voidType;
}
 
Example #11
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void addWriteMethod(ClassFile classfile, FieldInfo finfo)
		throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int this_class_index = cp.getThisClassInfo();
	String desc = "(" + finfo.getDescriptor() + ")V";
	MethodInfo minfo = new MethodInfo(cp, EACH_WRITE_METHOD_PREFIX
	                                      + finfo.getName(), desc);
	/* local variables | target obj | each oldvalue | */
	Bytecode code = new Bytecode(cp, 6, 3);
	// aload_0
	code.addAload(0);
	// invokeinterface // enabled.getInterceptFieldCallback()
	int enabled_class_index = cp.addClassInfo(FIELD_HANDLED_TYPE_NAME);
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// ifnonnull (label1)
	code.addOpcode(Opcode.IFNONNULL);
	code.addIndex(9);
	// aload_0
	code.addAload(0);
	// *load_1
	addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
	// putfield
	code.addOpcode(Opcode.PUTFIELD);
	int base_field_index = cp.addFieldrefInfo(this_class_index, finfo
			.getName(), finfo.getDescriptor());
	code.addIndex(base_field_index);
	code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
	// return ;
	code.addOpcode(Opcode.RETURN);
	// aload_0
	code.addAload(0);
	// dup
	code.addOpcode(Opcode.DUP);
	// invokeinterface // enabled.getInterceptFieldCallback()
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// aload_0
	code.addAload(0);
	// ldc // field name
	code.addLdc(finfo.getName());
	// aload_0
	code.addAload(0);
	// getfield // old value of the field
	code.addOpcode(Opcode.GETFIELD);
	code.addIndex(base_field_index);
	code.growStack(Descriptor.dataSize(finfo.getDescriptor()) - 1);
	// *load_1
	addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
	// invokeinterface // callback.write*(..)
	addInvokeFieldHandlerMethod(classfile, code, finfo.getDescriptor(),
	                            false);
	// putfield // new value of the field
	code.addOpcode(Opcode.PUTFIELD);
	code.addIndex(base_field_index);
	code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
	// return
	code.addOpcode(Opcode.RETURN);

	minfo.setCodeAttribute(code.toCodeAttribute());
	minfo.setAccessFlags(AccessFlag.PUBLIC);
	classfile.addMethod(minfo);
}