Skip to content

Commit

Permalink
WELD-2273 Decorating bean with private constructor is not allowed.
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn authored and mkouba committed Jun 7, 2017
1 parent 20c44f0 commit 9a3fa7d
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 6 deletions.
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2017, 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.environment.se.test.decorator.invalid;

import org.jboss.arquillian.container.se.api.ClassPath;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.exceptions.DeploymentException;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* See WELD-2273 for more information
* Relaxed mode should have no effect on this particular problem, therefore we test same assumption with and without it
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
*/
@RunWith(Arquillian.class)
public class DecoratorWithPrivateConstructorTest {

@Deployment
public static Archive<?> createTestArchive() {
return ClassPath.builder()
.add(ShrinkWrap.create(BeanArchive.class)
.addPackage(DecoratorWithPrivateConstructorTest.class.getPackage())).build();
}

@Test(expected = DeploymentException.class)
public void testExceptionIsThrownWithoutRelaxedMode() {
Weld weld = new Weld();
try (WeldContainer container = weld.disableDiscovery().addPackages(DecoratorWithPrivateConstructorTest.class.getPackage())
// NOTE: in SE this is by default true
.property("org.jboss.weld.construction.relaxed", false)
.initialize()) {
container.select(ImplementingBean.class).get().ping();
}
}

@Test(expected = DeploymentException.class)
public void testExceptionIsThrownWithRelaxedMode() {
Weld weld = new Weld();
try (WeldContainer container = weld.disableDiscovery().addPackages(DecoratorWithPrivateConstructorTest.class.getPackage())
.property("org.jboss.weld.construction.relaxed", true)
.initialize()) {
container.select(ImplementingBean.class).get().ping();
}
}
}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2017, 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.environment.se.test.decorator.invalid;

import javax.enterprise.context.ApplicationScoped;

/**
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
*/
@ApplicationScoped
public class ImplementingBean implements SomeInterface {

private ImplementingBean() {
// private constructor is invalid
}

@Override
public void ping() {
}

}
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2017, 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.environment.se.test.decorator.invalid;

import javax.annotation.Priority;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;

/**
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
*/
@Decorator
@Priority(1)
public class SomeDecorator implements SomeInterface {

@Inject
@Delegate
SomeInterface impl;

@Override
public void ping() {
impl.ping();
}
}
@@ -0,0 +1,26 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2017, 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.environment.se.test.decorator.invalid;

/**
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
*/
public interface SomeInterface {

public void ping();
}
Expand Up @@ -139,14 +139,20 @@ protected void setupConstructorInterceptionInstantiator(InterceptionModel interc
}

protected void checkNoArgsConstructor(EnhancedAnnotatedType<T> type) {
if (!beanManager.getServices().get(ProxyInstantiator.class).isUsingConstructor()) {
return;
}
EnhancedAnnotatedConstructor<T> constructor = type.getNoArgsEnhancedConstructor();
if (constructor == null) {
throw BeanLogger.LOG.decoratedHasNoNoargsConstructor(this);
} else if (constructor.isPrivate()) {
throw BeanLogger.LOG.decoratedNoargsConstructorIsPrivate(this, Formats.formatAsStackTraceElement(constructor.getJavaMember()));
if (!beanManager.getServices().get(ProxyInstantiator.class).isUsingConstructor()) {
// Relaxed construction allows us to bypass non-existent no-arg constructor
return;
} else {
// Without relaxed contruction, CDI enforces existence of no-arg constructor
throw BeanLogger.LOG.decoratedHasNoNoargsConstructor(this);
}
} else {
if (constructor.isPrivate()) {
// Private no-arg constructor is a problem while creating the decorator and we have to blow up
throw BeanLogger.LOG.decoratedNoargsConstructorIsPrivate(this, Formats.formatAsStackTraceElement(constructor.getJavaMember()));
}
}
}

Expand Down

0 comments on commit 9a3fa7d

Please sign in to comment.