-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interceptor should intercept non-abstract methods for abstract classes (
- Loading branch information
Showing
10 changed files
with
322 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
inject-java/src/test/groovy/io/micronaut/aop/introduction/MyAbstractRepoSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2017-2019 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 | ||
* | ||
* http://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.aop.introduction | ||
|
||
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec | ||
|
||
class MyAbstractRepoSpec extends AbstractTypeElementSpec { | ||
|
||
void "test abstract interceptor method"() { | ||
given: | ||
def context = buildContext(""" | ||
package test; | ||
import io.micronaut.aop.introduction.Tx; | ||
import io.micronaut.aop.introduction.RepoDef; | ||
import io.micronaut.aop.introduction.DeleteByIdCrudRepo; | ||
import io.micronaut.context.annotation.Executable; | ||
@Tx | ||
@RepoDef | ||
abstract class MyAbstractRepo4 implements DeleteByIdCrudRepo<Integer> { | ||
public String findById(Integer id) { | ||
return "ABC"; | ||
} | ||
} | ||
""") | ||
|
||
when: | ||
def beanDef1 = context.getBeanDefinition(context.classLoader.loadClass("test.MyAbstractRepo4")) | ||
def findById = beanDef1.getRequiredMethod("findById", Integer) | ||
then: | ||
findById | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
|
||
void "test default interceptor method"() { | ||
given: | ||
def context = buildContext(""" | ||
package test; | ||
import io.micronaut.aop.introduction.Tx; | ||
import io.micronaut.aop.introduction.RepoDef; | ||
import io.micronaut.aop.introduction.DeleteByIdCrudRepo; | ||
@Tx | ||
@RepoDef | ||
interface MyDefaultRepo extends DeleteByIdCrudRepo<Integer> { | ||
default String findById(Integer id) { | ||
return "ABC"; | ||
} | ||
} | ||
""") | ||
|
||
when: | ||
def beanDef1 = context.getBeanDefinition(context.classLoader.loadClass("test.MyDefaultRepo")) | ||
def findById = beanDef1.getRequiredMethod("findById", Integer) | ||
then: | ||
findById | ||
|
||
cleanup: | ||
context.close() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
inject-java/src/test/groovy/io/micronaut/aop/introduction/MyRepo3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.aop.introduction; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
@Tx | ||
@RepoDef | ||
public interface MyRepo3 extends DeleteByIdCrudRepo<Integer> { | ||
|
||
@Override | ||
void deleteById(@NotNull Integer id); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
inject-java/src/test/groovy/io/micronaut/aop/introduction/MyRepo4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.aop.introduction; | ||
|
||
@Tx | ||
@RepoDef | ||
public abstract class MyRepo4 implements DeleteByIdCrudRepo<Integer> { | ||
|
||
public String findById(Integer id) { | ||
return "ABC"; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
inject-java/src/test/groovy/io/micronaut/aop/introduction/MyRepo5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.aop.introduction; | ||
|
||
@Tx | ||
@RepoDef | ||
public interface MyRepo5 extends DeleteByIdCrudRepo<Integer> { | ||
|
||
default String findById(Integer id) { | ||
return "ABC"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
inject-java/src/test/groovy/io/micronaut/aop/introduction/Tx.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.aop.introduction; | ||
|
||
// tag::imports[] | ||
|
||
import io.micronaut.aop.Around; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
// end::imports[] | ||
|
||
// tag::annotation[] | ||
@Documented | ||
@Retention(RUNTIME) // <1> | ||
@Target({TYPE, METHOD}) // <2> | ||
@Around // <3> | ||
public @interface Tx { | ||
} | ||
// end::annotation[] |
63 changes: 63 additions & 0 deletions
63
inject-java/src/test/groovy/io/micronaut/aop/introduction/TxInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.aop.introduction; | ||
|
||
|
||
import io.micronaut.aop.InterceptedMethod; | ||
import io.micronaut.aop.InterceptorBean; | ||
import io.micronaut.aop.MethodInterceptor; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.convert.ConversionService; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Singleton | ||
@InterceptorBean(Tx.class) | ||
public class TxInterceptor implements MethodInterceptor<Object, Object> { | ||
|
||
public static final List<Method> EXECUTED_METHODS = new ArrayList<>(); | ||
|
||
private final ConversionService conversionService; | ||
|
||
public TxInterceptor(ConversionService conversionService) { | ||
this.conversionService = conversionService; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 0; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Object intercept(MethodInvocationContext<Object, Object> context) { | ||
EXECUTED_METHODS.add(context.getExecutableMethod().getTargetMethod()); | ||
InterceptedMethod interceptedMethod = InterceptedMethod.of(context, conversionService); | ||
try { | ||
return interceptedMethod.handleResult( | ||
interceptedMethod.interceptResult() | ||
); | ||
} catch (Exception e) { | ||
return interceptedMethod.handleException(e); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters