javassist.bytecode.SignatureAttribute Java Examples

The following examples show how to use javassist.bytecode.SignatureAttribute. 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: PersistentAttributesHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static String inferFieldTypeName(CtField field) {
	try {
		if ( field.getFieldInfo2().getAttribute( SignatureAttribute.tag ) == null ) {
			return field.getType().getName();
		}
		return inferGenericTypeName(
				field.getType(),
				SignatureAttribute.toTypeSignature( field.getGenericSignature() )
		);
	}
	catch (BadBytecode ignore) {
		return null;
	}
	catch (NotFoundException e) {
		return null;
	}
}
 
Example #2
Source File: PersistentAttributesHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static String inferMethodTypeName(CtMethod method) {
	try {
		if ( method.getMethodInfo2().getAttribute( SignatureAttribute.tag ) == null ) {
			return method.getReturnType().getName();
		}
		return inferGenericTypeName(
				method.getReturnType(),
				SignatureAttribute.toMethodSignature( method.getGenericSignature() ).getReturnType()
		);
	}
	catch (BadBytecode ignore) {
		return null;
	}
	catch (NotFoundException e) {
		return null;
	}
}
 
Example #3
Source File: PersistentAttributesHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static String inferGenericTypeName(CtClass ctClass, SignatureAttribute.Type genericSignature) {
	// infer targetEntity from generic type signature
	if ( isAssignable( ctClass, Collection.class.getName() ) ) {
		return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[0].getType().jvmTypeName();
	}
	if ( isAssignable( ctClass, Map.class.getName() ) ) {
		return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[1].getType().jvmTypeName();
	}
	return ctClass.getName();
}
 
Example #4
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getMethodGenericSignatureType(Signature methodSignature) throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    final SignatureAttribute sa
        = (SignatureAttribute) m.getAttribute(SignatureAttribute.tag);
    return sa == null ? null : sa.getSignature();
}
 
Example #5
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getFieldGenericSignatureType(Signature fieldSignature) 
throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    SignatureAttribute sa = (SignatureAttribute) fld.getAttribute(SignatureAttribute.tag);
    return (sa == null ? null : sa.getSignature());
}
 
Example #6
Source File: AttributeBytecodeGenerator.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for generating SimpleAttribute AND SimpleNullableAttribute.
 *
 * @param target Snippet of code which reads the value, such as: <code>object.fieldName</code>, <code>object.getFoo()</code>, or <code>object.getFoo("bar")</code>
 */
private static <O, A, C extends Attribute<O, A>, R extends Class<? extends C>> R generateSimpleAttribute(Class<C> attributeSuperClass, Class<O> pojoClass, Class<A> attributeValueType, String attributeName, String target) {
    try {
        ClassPool pool = new ClassPool(false);
        pool.appendClassPath(new ClassClassPath(pojoClass));

        CtClass attributeClass = pool.makeClass(pojoClass.getName() + "$$CQEngine_" + attributeSuperClass.getSimpleName() + "_" + attributeName);
        attributeClass.setSuperclass(pool.get(attributeSuperClass.getName()));

        SignatureAttribute.ClassType genericTypeOfAttribute = new SignatureAttribute.ClassType(
                attributeSuperClass.getName(),
                new SignatureAttribute.TypeArgument[]{
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(pojoClass.getName())),
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(attributeValueType.getName()))
                }
        );
        attributeClass.setGenericSignature(genericTypeOfAttribute.encode());

        // Add a no-arg constructor which pass the attribute name to the superclass...
        CtConstructor constructor = CtNewConstructor.make(
                "public " + attributeClass.getSimpleName() + "() { "
                        + "super(\"" + attributeName + "\");"
                        + " }", attributeClass);
        attributeClass.addConstructor(constructor);

        // Add the getter method...
        CtMethod getterMethod = CtMethod.make(
                "public " + attributeValueType.getName() + " getValue(" + pojoClass.getName() + " object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return (" + attributeValueType.getName() + ") " + GeneratedAttributeSupport.class.getName() + ".valueOf(" + target + ");"
                        + " }", attributeClass);
        attributeClass.addMethod(getterMethod);

        // Add a bridge method for the getter method to account for type erasure (see https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html)...
        CtMethod getterBridgeMethod = CtMethod.make(
                "public java.lang.Object getValue(java.lang.Object object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return getValue((" + pojoClass.getName() + ")object, queryOptions);"
                        + " }", attributeClass);
        getterBridgeMethod.setModifiers(getterBridgeMethod.getModifiers() | AccessFlag.BRIDGE);
        attributeClass.addMethod(getterBridgeMethod);

        @SuppressWarnings("unchecked")
        R result = (R) attributeClass.toClass(pojoClass.getClassLoader(), pojoClass.getProtectionDomain());
        attributeClass.detach();
        return result;
    } catch (Exception e) {
        throw new IllegalStateException(getExceptionMessage(pojoClass, attributeValueType, attributeName), e);
    }
}
 
Example #7
Source File: AttributeBytecodeGenerator.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for generating MultiValueAttribute.
 *
 * @param target Snippet of code which reads the value, such as: <code>object.fieldName</code>, <code>object.getFoo()</code>, or <code>object.getFoo("bar")</code>
 */
private static <O, A, C extends MultiValueAttribute<O, A>, R extends Class<? extends C>> R generateMultiValueAttribute(Class<C> attributeSuperClass, Class<O> pojoClass, Class<A> attributeValueType, String attributeName, String target) {
    try {
        ClassPool pool = new ClassPool(false);
        pool.appendClassPath(new ClassClassPath(pojoClass));

        CtClass attributeClass = pool.makeClass(pojoClass.getName() + "$$CQEngine_" + attributeSuperClass.getSimpleName() + "_" + attributeName);
        attributeClass.setSuperclass(pool.get(attributeSuperClass.getName()));

        SignatureAttribute.ClassType genericTypeOfAttribute = new SignatureAttribute.ClassType(
                attributeSuperClass.getName(),
                new SignatureAttribute.TypeArgument[]{
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(pojoClass.getName())),
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(attributeValueType.getName()))
                }
        );
        attributeClass.setGenericSignature(genericTypeOfAttribute.encode());

        // Add a no-arg constructor which pass the attribute name to the superclass...
        CtConstructor constructor = CtNewConstructor.make(
                "public " + attributeClass.getSimpleName() + "() { "
                        + "super(\"" + attributeName + "\");"
                        + " }", attributeClass);
        attributeClass.addConstructor(constructor);

        // Add the getter method...
        CtMethod getterMethod = CtMethod.make(
                "public java.lang.Iterable getValues(" + pojoClass.getName() + " object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return " + GeneratedAttributeSupport.class.getName() + ".valueOf(" + target + ");"
                        + " }", attributeClass);

        getterMethod.setGenericSignature(new SignatureAttribute.MethodSignature(
                new SignatureAttribute.TypeParameter[0],
                new SignatureAttribute.Type[]{new SignatureAttribute.ClassType(pojoClass.getName())},
                new SignatureAttribute.ClassType(
                        java.lang.Iterable.class.getName(),
                        new SignatureAttribute.TypeArgument[]{
                                new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(attributeValueType.getName()))
                        }
                ),
                new SignatureAttribute.ObjectType[0]
        ).encode());
        attributeClass.addMethod(getterMethod);

        // Add a bridge method for the getter method to account for type erasure (see https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html)...
        CtMethod getterBridgeMethod = CtMethod.make(
                "public java.lang.Iterable getValues(java.lang.Object object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return getValues((" + pojoClass.getName() + ")object, queryOptions);"
                        + " }", attributeClass);
        getterBridgeMethod.setModifiers(getterBridgeMethod.getModifiers() | AccessFlag.BRIDGE);
        attributeClass.addMethod(getterBridgeMethod);

        @SuppressWarnings("unchecked")
        R result = (R) attributeClass.toClass(pojoClass.getClassLoader(), pojoClass.getProtectionDomain());
        attributeClass.detach();
        return result;
    } catch (Exception e) {
        throw new IllegalStateException(getExceptionMessage(pojoClass, attributeValueType, attributeName), e);
    }
}
 
Example #8
Source File: AttributeBytecodeGenerator.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for generating MultiValueNullableAttribute.
 *
 * @param target Snippet of code which reads the value, such as: <code>object.fieldName</code>, <code>object.getFoo()</code>, or <code>object.getFoo("bar")</code>
 */
private static <O, A, C extends MultiValueNullableAttribute<O, A>, R extends Class<? extends C>> R generateMultiValueNullableAttribute(Class<C> attributeSuperClass, Class<O> pojoClass, Class<A> attributeValueType, String attributeName, boolean componentValuesNullable, String target) {
    try {
        ClassPool pool = new ClassPool(false);
        pool.appendClassPath(new ClassClassPath(pojoClass));

        CtClass attributeClass = pool.makeClass(pojoClass.getName() + "$$CQEngine_" + attributeSuperClass.getSimpleName() + "_" + attributeName);
        attributeClass.setSuperclass(pool.get(attributeSuperClass.getName()));

        SignatureAttribute.ClassType genericTypeOfAttribute = new SignatureAttribute.ClassType(
                attributeSuperClass.getName(),
                new SignatureAttribute.TypeArgument[]{
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(pojoClass.getName())),
                        new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(attributeValueType.getName()))
                }
        );
        attributeClass.setGenericSignature(genericTypeOfAttribute.encode());

        // Add a no-arg constructor which pass the attribute name to the superclass...
        CtConstructor constructor = CtNewConstructor.make(
                "public " + attributeClass.getSimpleName() + "() { "
                        + "super(\"" + attributeName + "\", " + componentValuesNullable + ");"
                        + " }", attributeClass);
        attributeClass.addConstructor(constructor);

        // Add the getter method...
        CtMethod getterMethod = CtMethod.make(
                "public java.lang.Iterable getNullableValues(" + pojoClass.getName() + " object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return " + GeneratedAttributeSupport.class.getName() + ".valueOf(" + target + ");"
                        + " }", attributeClass);

        getterMethod.setGenericSignature(new SignatureAttribute.MethodSignature(
                new SignatureAttribute.TypeParameter[0],
                new SignatureAttribute.Type[]{new SignatureAttribute.ClassType(pojoClass.getName())},
                new SignatureAttribute.ClassType(
                        java.lang.Iterable.class.getName(),
                        new SignatureAttribute.TypeArgument[]{
                                new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(attributeValueType.getName()))
                        }
                ),
                new SignatureAttribute.ObjectType[0]
        ).encode());
        attributeClass.addMethod(getterMethod);

        // Add a bridge method for the getter method to account for type erasure (see https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html)...
        CtMethod getterBridgeMethod = CtMethod.make(
                "public java.lang.Iterable getNullableValues(java.lang.Object object, " + QueryOptions.class.getName() + " queryOptions) { "
                        + "return getNullableValues((" + pojoClass.getName() + ")object, queryOptions);"
                        + " }", attributeClass);
        getterBridgeMethod.setModifiers(getterBridgeMethod.getModifiers() | AccessFlag.BRIDGE);
        attributeClass.addMethod(getterBridgeMethod);

        @SuppressWarnings("unchecked")
        R result = (R) attributeClass.toClass(pojoClass.getClassLoader(), pojoClass.getProtectionDomain());
        attributeClass.detach();
        return result;
    } catch (Exception e) {
        throw new IllegalStateException(getExceptionMessage(pojoClass, attributeValueType, attributeName), e);
    }
}
 
Example #9
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String getGenericSignatureType() {
	final SignatureAttribute sa = (SignatureAttribute) this.cf.getAttribute(SignatureAttribute.tag);
	return sa == null ? null : sa.getSignature();    
}
 
Example #10
Source File: ModelInstrumentation.java    From javalite with Apache License 2.0 4 votes vote down vote up
private void doInstrument(CtClass target) throws NotFoundException, CannotCompileException {
    CtMethod[] modelMethods = modelClass.getDeclaredMethods();
    CtMethod[] targetMethods = target.getDeclaredMethods();

    CtMethod modelGetClass = modelClass.getDeclaredMethod("modelClass");
    CtMethod newGetClass = CtNewMethod.copy(modelGetClass, target, null);
    newGetClass.setBody("{ return " + target.getName() + ".class; }");

    // do not convert Model class to Target class in methods
    ClassMap classMap = new ClassMap();
    classMap.fix(modelClass);

    // convert Model.getDaClass() calls to Target.getDaClass() calls
    CodeConverter conv = new CodeConverter();
    conv.redirectMethodCall(modelGetClass, newGetClass);

    for (CtMethod method : modelMethods) {
        int modifiers = method.getModifiers();
        if (Modifier.isStatic(modifiers)) {
            if (targetHasMethod(targetMethods, method)) {
                Logger.debug("Detected method: " + method.getName() + ", skipping delegate.");
            } else {
                CtMethod newMethod;
                if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) {
                    newMethod = CtNewMethod.copy(method, target, classMap);
                    newMethod.instrument(conv);
                } else if ("modelClass".equals(method.getName())) {
                    newMethod = newGetClass;
                } else {
                    newMethod = CtNewMethod.delegator(method, target);
                }

                // Include the generic signature
                for (Object attr : method.getMethodInfo().getAttributes()) {
                    if (attr instanceof SignatureAttribute) {
                        newMethod.getMethodInfo().addAttribute((SignatureAttribute) attr);
                    }
                }
                addGeneratedAnnotation(newMethod, target);
                target.addMethod(newMethod);
            }
        }
    }
}