net.bytebuddy.description.type.TypeDescription.Generic Java Examples

The following examples show how to use net.bytebuddy.description.type.TypeDescription.Generic. 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: ByteBuddyProcessFunctionInvoker.java    From da-streamingledger with Apache License 2.0 6 votes vote down vote up
private static <InT, OutT> DynamicType.Unloaded<?> createDynamicTypeFromSpec(StreamingLedgerSpec<InT, OutT> spec)
        throws NoSuchMethodException {
    PackageLocalNamingStrategy generatedTypeName = new PackageLocalNamingStrategy(spec.processFunction.getClass());

    TypeDefinition generatedType = Generic.Builder.parameterizedType(
            ProcessFunctionInvoker.class,
            spec.inputType.getTypeClass(),
            spec.resultType.getTypeClass()
    ).build();

    TypeDefinition processFunctionType = new TypeDescription.ForLoadedType(spec.processFunction.getClass());
    ForLoadedConstructor superTypeConstructor =
            new ForLoadedConstructor(ProcessFunctionInvoker.class.getDeclaredConstructor());
    MethodDescription processMethodType = processMethodTypeFromSpec(spec);

    Builder<?> builder = configureByteBuddyBuilder(
            generatedTypeName,
            generatedType,
            processFunctionType,
            superTypeConstructor,
            processMethodType,
            spec.stateBindings.size());

    return builder.make();
}
 
Example #2
Source File: Controller.java    From Diorite with MIT License 6 votes vote down vote up
static String fixName(TypeDescription.Generic type, String currentName)
{
    if (type.asErasure().equals(PROVIDER))
    {
        String name = currentName.toLowerCase();
        int providerIndex = name.indexOf("provider");
        if (providerIndex != - 1)
        {
            name = currentName.substring(0, providerIndex);
        }
        else
        {
            name = currentName;
        }
        return name;
    }
    else
    {
        return currentName;
    }
}
 
Example #3
Source File: Controller.java    From Diorite with MIT License 6 votes vote down vote up
<T, B extends AnnotatedCodeElement & NamedElement.WithRuntimeName> ControllerInjectValueData<T> createValue
        (int index, TypeDescription classType, TypeDescription.ForLoadedType.Generic type, B member, String name,
         Map<Class<? extends Annotation>, ? extends Annotation> parentRawScopeAnnotations,
         Map<Class<? extends Annotation>, ? extends Annotation> parentRawQualifierAnnotations)
{
    Map<Class<? extends Annotation>, ? extends Annotation> scopeAnnotations;
    {
        Map<Class<? extends Annotation>, ? extends Annotation> memberRawScopeAnnotations = this.extractRawScopeAnnotations(member);
        Map<Class<? extends Annotation>, Annotation> rawScopeAnnotations = new HashMap<>(parentRawScopeAnnotations);
        rawScopeAnnotations.putAll(memberRawScopeAnnotations);
        scopeAnnotations = this.transformAll(classType, name, member, rawScopeAnnotations);
    }
    Map<Class<? extends Annotation>, ? extends Annotation> qualifierAnnotations;
    {
        Map<Class<? extends Annotation>, ? extends Annotation> memberRawQualifierAnnotations = this.extractRawQualifierAnnotations(member);
        Map<Class<? extends Annotation>, Annotation> rawQualifierAnnotations = new HashMap<>(parentRawQualifierAnnotations);
        rawQualifierAnnotations.putAll(memberRawQualifierAnnotations);
        qualifierAnnotations = this.transformAll(classType, name, member, rawQualifierAnnotations);
    }
    return new ControllerInjectValueData<>(index, name, type, scopeAnnotations, qualifierAnnotations);
}
 
Example #4
Source File: TypeDefinitionUtil.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
static TypeDefinition createTypeDefinitionFromJavaType(JavaType javaType) {
    if (!javaType.hasGenericTypes()) {
        //simply use the raw class to construct the corresponding TypeDefinition
        return new TypeDescription.ForLoadedType(javaType.getRawClass());
    }
    //create the appropriate Generic TypeDescription using containedType values
    final List<Type> genericParameters = new ArrayList<Type>();
    for(int i=0; i<javaType.containedTypeCount(); i++) {
        genericParameters.add(javaType.containedType(i).getRawClass());
    }
    return Generic.Builder.parameterizedType(javaType.getRawClass(), genericParameters).build();
}
 
Example #5
Source File: TemplateModelProxyHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private static boolean isAccessor(MethodDescription method) {
    if (method.getDeclaringType().represents(Object.class)) {
        return false;
    }
    String methodName = method.getName();
    Generic returnType = method.getReturnType();
    ParameterList<?> args = method.getParameters();

    boolean isSetter = Generic.VOID.equals(returnType) && args.size() == 1
            && ReflectTools.isSetterName(methodName);
    boolean isGetter = !Generic.VOID.equals(returnType) && args.isEmpty()
            && ReflectTools.isGetterName(methodName,
                    returnType.represents(boolean.class));
    return isSetter || isGetter;
}
 
Example #6
Source File: BinderValueData.java    From Diorite with MIT License 5 votes vote down vote up
public boolean isCompatible(InjectValueData<?, Generic> injectValueData)
{
    if (! this.typePredicate.test(injectValueData.getType()))
    {
        return false;
    }
    for (QualifierPattern qualifier : this.qualifiers)
    {
        if (! qualifier.test(injectValueData))
        {
            return false;
        }
    }
    return true;
}
 
Example #7
Source File: ControllerInjectValueData.java    From Diorite with MIT License 5 votes vote down vote up
ControllerInjectValueData(int index, String name, Generic type, Map<Class<? extends Annotation>, ? extends Annotation> scopes,
                          Map<Class<? extends Annotation>, ? extends Annotation> qualifiers)
{
    this.index = index;
    this.name = name;
    this.type = type;
    this.scopes = scopes;
    this.qualifiers = qualifiers;
    this.isProvider = type.asErasure().equals(Controller.PROVIDER);
}
 
Example #8
Source File: Controller.java    From Diorite with MIT License 5 votes vote down vote up
@Override
public void rebind()
{
    Lock writeLock = this.lock.writeLock();
    try
    {
        writeLock.lock();
        for (org.diorite.inject.impl.data.ClassData<TypeDescription.ForLoadedType.Generic> classData : this.dataList)
        {
            this.rebindSingle(classData);
        }
        for (BinderValueData bindValue : this.bindValues)
        {
            Collection<? extends InjectValueData<?, ?>> injectValues = bindValue.getProvider().getInjectValues();
            if (injectValues.isEmpty())
            {
                continue;
            }
            for (InjectValueData<?, ?> valueData : injectValues)
            {
                this.findBinder((ControllerInjectValueData<?>) valueData);
            }
        }
    }
    finally
    {
        writeLock.unlock();
    }
}
 
Example #9
Source File: Controller.java    From Diorite with MIT License 5 votes vote down vote up
private void rebindSingle(org.diorite.inject.impl.data.ClassData<TypeDescription.ForLoadedType.Generic> classData)
{
    Collection<org.diorite.inject.impl.data.InjectValueData<?, Generic>> allData = new ArrayList<>(100);
    for (org.diorite.inject.impl.data.MemberData<TypeDescription.ForLoadedType.Generic> fieldData : classData.getMembers())
    {
        allData.addAll(fieldData.getInjectValues());
    }
    for (org.diorite.inject.impl.data.InjectValueData<?, TypeDescription.ForLoadedType.Generic> valueData : allData)
    {
        this.findBinder(valueData);
    }
}
 
Example #10
Source File: Controller.java    From Diorite with MIT License 5 votes vote down vote up
private void rebindSingleWithLock(org.diorite.inject.impl.data.ClassData<TypeDescription.ForLoadedType.Generic> classData)
{
    Lock writeLock = this.lock.writeLock();
    try
    {
        writeLock.lock();
        this.rebindSingle(classData);
    }
    finally
    {
        writeLock.unlock();
    }
}
 
Example #11
Source File: Controller.java    From Diorite with MIT License 5 votes vote down vote up
@Override
protected ControllerClassData addClassData(TypeDescription typeDescription,
                                           org.diorite.inject.impl.data.ClassData<TypeDescription.ForLoadedType.Generic> classData)
{
    if (! (classData instanceof ControllerClassData))
    {
        throw new IllegalArgumentException("Unsupported class data for this controller");
    }
    this.map.put(typeDescription, classData);
    Lock lock = this.lock.writeLock();
    try
    {
        lock.lock();
        ((ControllerClassData) classData).setIndex(this.dataList.size());
        this.dataList.add(classData);
    }
    finally
    {
        lock.unlock();
    }
    try
    {
        ByteBuddyAgent.getInstrumentation().retransformClasses(AsmUtils.toClass(typeDescription));
    }
    catch (Exception e)
    {
        throw new TransformerError(e);
    }
    return (ControllerClassData) classData;
}
 
Example #12
Source File: Controller.java    From Diorite with MIT License 5 votes vote down vote up
@Override
public void addClassData(Class<?> clazz)
{
    org.diorite.inject.impl.data.ClassData<Generic> classData = this.addClassData(new ForLoadedType(clazz));
    if (classData != null)
    {
        this.rebindSingleWithLock(classData);
    }
}
 
Example #13
Source File: BinderSimpleInstance.java    From Diorite with MIT License 5 votes vote down vote up
private void bind()
{
    Predicate<Generic> typePredicate =
            type -> type.asErasure().equals(Controller.PROVIDER) ?
                    this.typePredicate.test(type.getTypeArguments().get(0)) :
                    this.typePredicate.test(type);
    BinderValueData valueData = BinderValueData.dynamic(typePredicate, this.patterns, this.provider);
    this.diController.bindValues.add(valueData);
}
 
Example #14
Source File: Controller.java    From Diorite with MIT License 4 votes vote down vote up
public <T> Binder<T> bindToType(Predicate<Generic> typePredicate)
{
    return new BinderSimpleInstance<>(this, typePredicate);
}
 
Example #15
Source File: BinderSimpleValueData.java    From Diorite with MIT License 4 votes vote down vote up
BinderSimpleValueData(Predicate<Generic> typePredicate, Collection<QualifierPattern> qualifiers, Provider<?> provider)
{
    super(typePredicate, qualifiers);
    this.provider = provider;
}
 
Example #16
Source File: TransformerInvokerGenerator.java    From Diorite with MIT License 4 votes vote down vote up
public static void generateMethodInjection(ControllerClassData classData, ControllerMethodData methodData, MethodNode mv, boolean printMethods,
                                           int lineNumber)
{
    MethodDescription.InDefinedShape member = methodData.getMember();
    boolean isStatic = member.isStatic();

    if (printMethods)
    {
        lineNumber = printMethods(mv, classData.getType().getInternalName(), methodData.getBefore(), isStatic, lineNumber);
    }
    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (! isStatic)
    {
        mv.visitVarInsn(ALOAD, 0);
    }

    for (InjectValueData<?, Generic> valueData : methodData.getInjectValues())
    {
        if (isStatic)
        {
            mv.visitInsn(ACONST_NULL);
        }
        else
        {
            mv.visitVarInsn(ALOAD, 0);
        }
        AsmUtils.storeInt(mv, classData.getIndex());
        AsmUtils.storeInt(mv, methodData.getIndex());
        AsmUtils.storeInt(mv, valueData.getIndex());
        mv.visitInsn(ICONST_0); // skip null checks in methods.
        lineNumber = AsmUtils.printLineNumber(mv, lineNumber);
        mv.visitMethodInsn(INVOKESTATIC, INJECTOR_CLASS, INJECTOR_METHOD, INJECTOR_METHOD_DESC, false);
        TypeDescription paramType = valueData.getType().asErasure();
        mv.visitTypeInsn(CHECKCAST, paramType.getInternalName()); // skip cast check?
    }

    lineNumber = AsmUtils.printLineNumber(mv, lineNumber);

    if (isStatic)
    {
        mv.visitMethodInsn(INVOKESTATIC, classData.getType().getInternalName(), member.getName(), member.getDescriptor(), false);
    }
    else
    {
        mv.visitMethodInsn(INVOKESPECIAL, classData.getType().getInternalName(), member.getName(), member.getDescriptor(), false);
    }

    if (printMethods)
    {
        printMethods(mv, classData.getType().getInternalName(), methodData.getAfter(), isStatic, lineNumber);
    }
}
 
Example #17
Source File: ControllerInjectValueData.java    From Diorite with MIT License 4 votes vote down vote up
@Override
public TypeDescription.ForLoadedType.Generic getType()
{
    return this.type;
}
 
Example #18
Source File: BinderValueData.java    From Diorite with MIT License 4 votes vote down vote up
BinderValueData(Predicate<TypeDescription.Generic> typePredicate, Collection<QualifierPattern> qualifiers)
{
    this.typePredicate = typePredicate;
    this.qualifiers = qualifiers;
}
 
Example #19
Source File: BinderSimpleInstance.java    From Diorite with MIT License 4 votes vote down vote up
BinderSimpleInstance(Controller diController, Predicate<Generic> typePredicate)
{
    this.diController = diController;
    this.typePredicate = typePredicate;
}
 
Example #20
Source File: BinderValueData.java    From Diorite with MIT License 4 votes vote down vote up
static BinderValueData simple(Predicate<Generic> typePredicate, Collection<QualifierPattern> qualifiers, Provider<?> provider)
{
    return new BinderSimpleValueData(typePredicate, qualifiers, provider);
}
 
Example #21
Source File: BinderValueData.java    From Diorite with MIT License 4 votes vote down vote up
static BinderValueData dynamic(Predicate<Generic> typePredicate, Collection<QualifierPattern> qualifiers, DynamicProvider<?> provider)
{
    return new BinderDynamicValueData(typePredicate, qualifiers, provider);
}
 
Example #22
Source File: BinderDynamicValueData.java    From Diorite with MIT License 4 votes vote down vote up
BinderDynamicValueData(Predicate<Generic> typePredicate, Collection<QualifierPattern> qualifiers, DynamicProvider<?> provider)
{
    super(typePredicate, qualifiers);
    this.provider = provider;
}