Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct type arguments for introduces beans #9752

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ protected BeanDefinitionVisitor createBeanDefinitionVisitor() {
throw new ProcessingException(classElement, "Cannot apply AOP advice to final class. Class must be made non-final to support proxying: " + classElement.getName());
}
aopProxyVisitor = createIntroductionAopProxyWriter(classElement, visitorContext);
aopProxyVisitor.visitTypeArguments(classElement.getAllTypeArguments());
visitAnnotationMetadata(aopProxyVisitor, classElement.getAnnotationMetadata());
beanDefinitionWriters.add(aopProxyVisitor);
MethodElement constructorElement = classElement.getPrimaryConstructor().orElse(null);
if (constructorElement != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,22 @@ class Test {
return (BeanDefinition)classLoader.loadClass(beanFullName).newInstance()
}

/**
* Builds the bean definition for an AOP proxy bean.
* @param className The class name
* @param cls The class source
* @return The bean definition
*/
protected BeanDefinition buildSimpleInterceptedBeanDefinition(String className, @Language("java") String cls) {
def classSimpleName = NameUtils.getSimpleName(className)
def beanDefName = (classSimpleName.startsWith('$') ? '' : '$') + classSimpleName + BeanDefinitionVisitor.PROXY_SUFFIX + BeanDefinitionWriter.CLASS_SUFFIX
def packageName = NameUtils.getPackageName(className)
String beanFullName = "${packageName}.${beanDefName}"

ClassLoader classLoader = buildClassLoader(className, cls)
return (BeanDefinition)classLoader.loadClass(beanFullName).newInstance()
}

/**
* Retrieve additional annotation mappers to apply
* @param annotationName The annotation name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.micronaut.annotation.processing.test.AbstractTypeElementSpec
import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.AnnotationUtil
import io.micronaut.core.annotation.Order
import io.micronaut.core.reflect.ClassUtils
import io.micronaut.core.type.Argument
import io.micronaut.core.type.GenericPlaceholder
import io.micronaut.inject.BeanDefinition
Expand Down Expand Up @@ -589,6 +590,84 @@ interface Deserializer<T> {
!(deserializerTypeParam instanceof GenericPlaceholder)
}

void "test intercepted type arguments"() {
given:
BeanDefinition definition = buildSimpleInterceptedBeanDefinition('test.AImplementationLong', '''
package test;

import io.micronaut.aop.Introduction;
import io.micronaut.context.annotation.DefaultScope;
import io.micronaut.context.annotation.Prototype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Secondary;

@MyIntroductionBean
@Secondary
class AImplementationLong implements AInterface<Long, Long> {

private Long l1,l2;

@Override
public void set(Long s, Long s2) {
l1=s;
l2=s2;
}

@Override
public String get(Long s, Long s2) {
return s.getClass().getName() +","+s.getClass().getName();
}
}

@MyIntroductionBean
@Secondary
class AImplementationString implements AInterface<String, String> {

private String s1,s2;

@Override
public void set(String s, String s2) {
s1=s;
this.s2=s2;
}

@Override
public String get(String s, String s2) {
return s.getClass().getName() +","+s.getClass().getName();
}
}

interface AInterface<K, V> {

void set(K k, V v);

String get(K k, V v);
}

@Introduction
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@DefaultScope(Prototype.class)
@interface MyIntroductionBean {
}

''')

when:
def arguments = definition.getTypeArguments(ClassUtils.forName("test.AInterface", definition.getClass().classLoader).orElseThrow())
then:
arguments[0].type == Long
arguments[1].type == Long
}

void "test repeatable inner type annotation 1"() {
when:
def ctx = ApplicationContext.builder().properties(["repeatabletest": "true"]).build().start()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.inject.beans.intro;

import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Secondary;

@Requires(property = "spec", value = "IntroductionInjectionSpec")
@MyIntroductionBean
@Secondary
public class AImplementationLong implements AInterface<Long, Long> {

private Long l1,l2;

@Override
public void set(Long s, Long s2) {
l1=s;
l2=s2;
}

@Override
public String get(Long s, Long s2) {
return s.getClass().getName() +","+s.getClass().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.inject.beans.intro;

import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Secondary;

@Requires(property = "spec", value = "IntroductionInjectionSpec")
@MyIntroductionBean
@Secondary
public class AImplementationString implements AInterface<String, String> {

private String s1,s2;

@Override
public void set(String s, String s2) {
s1=s;
this.s2=s2;
}

@Override
public String get(String s, String s2) {
return s.getClass().getName() +","+s.getClass().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.micronaut.inject.beans.intro;

public interface AInterface<K, V> {

void set(K k, V v);

String get(K k, V v);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.micronaut.inject.beans.intro

import io.micronaut.context.ApplicationContext
import io.micronaut.core.type.Argument
import spock.lang.Specification

class IntroductionInjectionSpec extends Specification {

void "test injection"() {
def ctx = ApplicationContext.run(["spec": "IntroductionInjectionSpec"])
def cacheString = ctx.getBean(Argument.of(AInterface, String, String))
def cacheLong = ctx.getBean(Argument.of(AInterface, Long, Long))

expect:
cacheString.get("hola", "mundo") == "java.lang.String,java.lang.String"
cacheLong.get(1L, 2L) == "java.lang.Long,java.lang.Long"

cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.inject.beans.intro;

import io.micronaut.aop.Introduction;
import io.micronaut.context.annotation.DefaultScope;
import io.micronaut.context.annotation.Prototype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Introduction
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@DefaultScope(Prototype.class)
public @interface MyIntroductionBean {
}