Skip to content

Commit

Permalink
Merge pull request #5385 from stuartwdouglas/5326
Browse files Browse the repository at this point in the history
Use Class.forName to prevent IllegalAccessError
  • Loading branch information
mkouba authored Nov 12, 2019
2 parents 52ee9d0 + 2c6df73 commit eccdeb4
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.resteasy.test.cdi;

import static io.restassured.RestAssured.when;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.resteasy.test.cdi.internal.PublicHello;
import io.quarkus.test.QuarkusUnitTest;

/**
*
*/
public class BeanGenerationTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addPackage(PublicHello.class.getPackage())
.addClasses(Greeting.class, MorningGreeting.class, GreetingEndpoint.class));

@Test
public void testInvocation() throws Exception {
when().get("/cdi-greeting/greet")
.then()
.statusCode(200)
.body(Matchers.is("Good Morning"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.resteasy.test.cdi;

import io.quarkus.resteasy.test.cdi.internal.PublicHello;

/**
*
*/
public class Greeting extends PublicHello {

@Override
public String greet() {
return "Hello user";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.resteasy.test.cdi;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
*
*/
@Path("/cdi-greeting")
public class GreetingEndpoint {

@Inject
Greeting greeting;

@GET
@Path("/greet")
public String greet() {
return greeting.greet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.resteasy.test.cdi;

import javax.enterprise.context.Dependent;

/**
*
*/
@Dependent
public class MorningGreeting extends Greeting {

@Override
public String greet() {
return "Good Morning";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.resteasy.test.cdi.internal;

/**
*
*/
class Hello {

void noop() {
System.out.println("Hello");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.resteasy.test.cdi.internal;

/**
*
*/
public class PublicHello extends Hello {

public String greet() {
return "Hello";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.arc.processor;

import static io.quarkus.arc.processor.IndexClassLookupUtils.getClassByName;
import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import io.quarkus.arc.impl.GenericArrayTypeImpl;
import io.quarkus.arc.impl.ParameterizedTypeImpl;
Expand Down Expand Up @@ -49,7 +50,8 @@ private Types() {

static ResultHandle getTypeHandle(BytecodeCreator creator, Type type) {
if (Kind.CLASS.equals(type.kind())) {
return creator.loadClass(type.asClassType().name().toString());
String className = type.asClassType().name().toString();
return doLoadClass(creator, className);
} else if (Kind.TYPE_VARIABLE.equals(type.kind())) {
// E.g. T -> new TypeVariableImpl("T")
TypeVariable typeVariable = type.asTypeVariable();
Expand Down Expand Up @@ -79,7 +81,7 @@ static ResultHandle getTypeHandle(BytecodeCreator creator, Type type) {
return creator.newInstance(
MethodDescriptor.ofConstructor(ParameterizedTypeImpl.class, java.lang.reflect.Type.class,
java.lang.reflect.Type[].class),
creator.loadClass(parameterizedType.name().toString()), typeArgsHandle);
doLoadClass(creator, parameterizedType.name().toString()), typeArgsHandle);

} else if (Kind.ARRAY.equals(type.kind())) {
Type componentType = type.asArrayType().component();
Expand Down Expand Up @@ -128,6 +130,18 @@ static ResultHandle getTypeHandle(BytecodeCreator creator, Type type) {
}
}

private static ResultHandle doLoadClass(BytecodeCreator creator, String className) {
//we need to use Class.forName as the class may be package private
ResultHandle currentThread = creator
.invokeStaticMethod(ofMethod(Thread.class, "currentThread", Thread.class));
ResultHandle tccl = creator.invokeVirtualMethod(
ofMethod(Thread.class, "getContextClassLoader", ClassLoader.class),
currentThread);
return creator.invokeStaticMethod(
ofMethod(Class.class, "forName", Class.class, String.class, boolean.class, ClassLoader.class),
creator.load(className), creator.load(true), tccl);
}

static Type getProviderType(ClassInfo classInfo) {
List<TypeVariable> typeParameters = classInfo.typeParameters();
if (!typeParameters.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public final class Arc {

public static ArcContainer initialize() {
if (INITIALIZED.compareAndSet(false, true)) {
ArcContainerImpl container = new ArcContainerImpl();
INSTANCE.set(container);
container.init();
return container;
try {
ArcContainerImpl container = new ArcContainerImpl();
INSTANCE.set(container);
container.init();
return container;
} catch (Throwable t) {
INITIALIZED.set(false);
throw new RuntimeException("Failed to initialize Arc", t);
}
}
return container();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void execute(BuildContext context) {

Object actualTest = factory.get();
extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).put(testClass.getName(), actualTest);
} catch (Exception e) {
} catch (Throwable e) {
started = false;
if (assertException != null) {
if (e instanceof RuntimeException) {
Expand Down

0 comments on commit eccdeb4

Please sign in to comment.