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

WELD-2712 Write more detailed tests for self-interception and allow p… #2722

Merged
merged 1 commit into from Apr 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -172,11 +172,15 @@ protected void addSerializationSupport(ClassFile proxyClassType) {
protected void createForwardingMethodBody(ClassMethod classMethod, final MethodInformation methodInfo, ClassMethod staticConstructor) {
final Method method = methodInfo.getMethod();
// we can only use bytecode based invocation for some methods
// at the moment we restrict it solely to public methods with public
// at the moment we restrict it solely to public/protected methods with public/protected
// return and parameter types
boolean bytecodeInvocationAllowed = Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getReturnType().getModifiers());
int methodModifiers = method.getModifiers();
int returnTypeModifiers = method.getReturnType().getModifiers();
boolean bytecodeInvocationAllowed = (Modifier.isPublic(methodModifiers) || Modifier.isProtected(methodModifiers))
&& (Modifier.isPublic(returnTypeModifiers) || Modifier.isProtected(returnTypeModifiers));
for (Class<?> paramType : method.getParameterTypes()) {
if (!Modifier.isPublic(paramType.getModifiers())) {
int paramTypeMofidiers = paramType.getModifiers();
if (!(Modifier.isPublic(paramTypeMofidiers) || Modifier.isProtected(paramTypeMofidiers))) {
bytecodeInvocationAllowed = false;
break;
}
Expand Down
@@ -0,0 +1,126 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2022, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.resolution.circular.self.modifiers;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class SelfInjectingImpl implements SomeInterface {

@Inject
SelfInjectingImpl self;

@Override
public Integer selfInvokePublicMethodPublicParamsPublicReturnType(Integer foo) {
return self._selfInvokePublicMethodPublicParamsPublicReturnType(foo);
}

@Override
public Integer selfInvokePublicMethodProtectedParamsPublicReturnType(Integer foo) {
return self._selfInvokePublicMethodProtectedParamsPublicReturnType(new ProtectedType(foo));
}

@Override
public Integer selfInvokePublicMethodPublicParamsProtectedReturnType(Integer foo) {
return self._selfInvokePublicMethodPublicParamsProtectedReturnType(foo).getVal();
}

@Override
public Integer selfInvokePublicMethodProtectedParamsProtectedReturnType(Integer foo) {
return self._selfInvokePublicMethodProtectedParamsProtectedReturnType(new ProtectedType(foo)).getVal();
}

@Override
public Integer selfInvokeProtectedMethodProtectedParamsProtectedReturnType(Integer foo) {
return self._selfInvokeProtectedMethodProtectedParamsProtectedReturnType(new ProtectedType(foo)).getVal();
}

@Override
public Integer selfInvokeProtectedMethodPublicParamsProtectedReturnType(Integer foo) {
return self._selfInvokeProtectedMethodPublicParamsProtectedReturnType(foo).getVal();
}

@Override
public Integer selfInvokeProtectedMethodProtectedParamsPublicReturnType(Integer foo) {
return self._selfInvokeProtectedMethodProtectedParamsPublicReturnType(new ProtectedType(foo));
}

@Override
public Integer selfInvokeProtectedMethodPublicParamsPublicReturnType(Integer foo) {
return self._selfInvokeProtectedMethodPublicParamsPublicReturnType(foo);
}

@SelfIntercept
public Integer _selfInvokePublicMethodPublicParamsPublicReturnType(Integer foo) {
return foo;
}

@SelfIntercept
public Integer _selfInvokePublicMethodProtectedParamsPublicReturnType(ProtectedType foo) {
return foo.getVal();
}

@SelfIntercept
public ProtectedType _selfInvokePublicMethodPublicParamsProtectedReturnType(Integer foo) {
return new ProtectedType(foo);
}

@SelfIntercept
public ProtectedType _selfInvokePublicMethodProtectedParamsProtectedReturnType(ProtectedType foo) {
return foo;
}

@SelfIntercept
protected ProtectedType _selfInvokeProtectedMethodProtectedParamsProtectedReturnType(ProtectedType foo) {
return foo;
}

@SelfIntercept
protected ProtectedType _selfInvokeProtectedMethodPublicParamsProtectedReturnType(Integer foo) {
return new ProtectedType(foo);
}

@SelfIntercept
protected Integer _selfInvokeProtectedMethodProtectedParamsPublicReturnType(ProtectedType foo) {
return foo.getVal();
}

@SelfIntercept
protected Integer _selfInvokeProtectedMethodPublicParamsPublicReturnType(Integer foo) {
return foo;
}


/**
* Used as either a protected return type or a protected param type
*/
protected class ProtectedType {

Integer val;

ProtectedType(Integer value) {
this.val = value;
}

public Integer getVal() {
return val;
}
}

}
@@ -0,0 +1,33 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.resolution.circular.self.modifiers;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import jakarta.interceptor.InterceptorBinding;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@InterceptorBinding
@Target( {METHOD, TYPE} )
@Retention( RUNTIME )
public @interface SelfIntercept {

}
@@ -0,0 +1,110 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2022, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.resolution.circular.self.modifiers;

import jakarta.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.weld.test.util.Utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Tests self-interception when invoking method via client proxy.
* Tests are for public/protected method and public/protected return type and parameter all of which should work.
* No other modifiers are working for self-interception at the time of writing.
*/
@RunWith(Arquillian.class)
public class SelfInterceptionComplexTest {

@Inject
SomeInterface selfInjectingImpl;

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(SelfInterceptionComplexTest.class))
.addPackage(SelfInterceptionComplexTest.class.getPackage());
}

@Test
public void selfInvokePublicMethodPublicParamsPublicReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokePublicMethodPublicParamsPublicReturnType(10);
Assert.assertEquals("_selfInvokePublicMethodPublicParamsPublicReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokePublicMethodProtectedParamsPublicReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokePublicMethodProtectedParamsPublicReturnType(10);
Assert.assertEquals("_selfInvokePublicMethodProtectedParamsPublicReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokePublicMethodPublicParamsProtectedReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokePublicMethodPublicParamsProtectedReturnType(10);
Assert.assertEquals("_selfInvokePublicMethodPublicParamsProtectedReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokePublicMethodProtectedParamsProtectedReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokePublicMethodProtectedParamsProtectedReturnType(10);
Assert.assertEquals("_selfInvokePublicMethodProtectedParamsProtectedReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokeProtectedMethodProtectedParamsProtectedReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokeProtectedMethodProtectedParamsProtectedReturnType(10);
Assert.assertEquals("_selfInvokeProtectedMethodProtectedParamsProtectedReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokeProtectedMethodPublicParamsProtectedReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokeProtectedMethodPublicParamsProtectedReturnType(10);
Assert.assertEquals("_selfInvokeProtectedMethodPublicParamsProtectedReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokeProtectedMethodProtectedParamsPublicReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokeProtectedMethodProtectedParamsPublicReturnType(10);
Assert.assertEquals("_selfInvokeProtectedMethodProtectedParamsPublicReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}

@Test
public void selfInvokeProtectedMethodPublicParamsPublicReturnTypeTest() {
SelfInterceptor.nullifyCache();
Assert.assertTrue(SelfInterceptor.INTERCEPTED_METHOD_NAME == null);
selfInjectingImpl.selfInvokeProtectedMethodPublicParamsPublicReturnType(10);
Assert.assertEquals("_selfInvokeProtectedMethodPublicParamsPublicReturnType", SelfInterceptor.INTERCEPTED_METHOD_NAME);
}
}
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2022, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.resolution.circular.self.modifiers;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@SelfIntercept
@Priority(1)
public class SelfInterceptor {

public static String INTERCEPTED_METHOD_NAME = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a constant so why the uppercase letters? Just nitpicking ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBF, I don't remember :-D


public static void nullifyCache() {
INTERCEPTED_METHOD_NAME = null;
}

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
INTERCEPTED_METHOD_NAME = ctx.getMethod().getName();
return ctx.proceed();
}
}
@@ -0,0 +1,33 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2022, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.weld.tests.resolution.circular.self.modifiers;

public interface SomeInterface {

// public method, all variations
Integer selfInvokePublicMethodPublicParamsPublicReturnType(Integer foo);
Integer selfInvokePublicMethodProtectedParamsPublicReturnType(Integer foo);
Integer selfInvokePublicMethodPublicParamsProtectedReturnType(Integer foo);
Integer selfInvokePublicMethodProtectedParamsProtectedReturnType(Integer foo);

// protected method, all variations
Integer selfInvokeProtectedMethodProtectedParamsProtectedReturnType(Integer foo);
Integer selfInvokeProtectedMethodPublicParamsProtectedReturnType(Integer foo);
Integer selfInvokeProtectedMethodProtectedParamsPublicReturnType(Integer foo);
Integer selfInvokeProtectedMethodPublicParamsPublicReturnType(Integer foo);
}