Skip to content

Changes to pass the DI TCK #550

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

Merged
merged 3 commits into from
Apr 6, 2021
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
68 changes: 64 additions & 4 deletions hk2-locator/src/main/java/org/jvnet/hk2/internal/ClazzCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
public class ClazzCreator<T> implements Creator<T> {
private final ServiceLocatorImpl locator;
private final Class<?> implClass;
private final Set<ResolutionInfo> myInitializers = new LinkedHashSet<ResolutionInfo>();
private final Set<ResolutionInfo> myFields = new LinkedHashSet<ResolutionInfo>();
private final Set<ResolutionInfo> myInitializers = new LinkedHashSet<>();
private final Set<ResolutionInfo> superInitializers = new LinkedHashSet<>();
private final Set<ResolutionInfo> myFields = new LinkedHashSet<>();
private final Set<ResolutionInfo> superFields = new LinkedHashSet<>();
private ActiveDescriptor<?> selfDescriptor;

private ResolutionInfo myConstructor;
Expand Down Expand Up @@ -127,7 +129,11 @@ public class ClazzCreator<T> implements Creator<T> {

baseAllInjectees.addAll(injectees);

myInitializers.add(new ResolutionInfo(element, injectees));
if (initMethod.getDeclaringClass().equals(implClass)) {
myInitializers.add(new ResolutionInfo(element, injectees));
} else {
superInitializers.add(new ResolutionInfo(element, injectees));
}
}

Set<Field> fields = Utilities.getInitFields(implClass, analyzer, collector);
Expand All @@ -139,7 +145,11 @@ public class ClazzCreator<T> implements Creator<T> {

baseAllInjectees.addAll(injectees);

myFields.add(new ResolutionInfo(element, injectees));
if (field.getDeclaringClass().equals(implClass)) {
myFields.add(new ResolutionInfo(element, injectees));
} else {
superFields.add(new ResolutionInfo(element, injectees));
}
}

postConstructMethod = Utilities.getPostConstruct(implClass, analyzer, collector);
Expand Down Expand Up @@ -204,6 +214,20 @@ private Map<SystemInjecteeImpl, Object> resolveAllDependencies(final ServiceHand
InjectionResolver<?> resolver = locator.getInjectionResolverForInjectee(injectee);
resolve(retVal, resolver, injectee, root, errorCollector);
}

for (ResolutionInfo fieldRI : superFields) {
for (SystemInjecteeImpl injectee : fieldRI.injectees) {
InjectionResolver<?> resolver = locator.getInjectionResolverForInjectee(injectee);
resolve(retVal, resolver, injectee, root, errorCollector);
}
}

for (ResolutionInfo methodRI : superInitializers) {
for (SystemInjecteeImpl injectee : methodRI.injectees) {
InjectionResolver<?> resolver = locator.getInjectionResolverForInjectee(injectee);
resolve(retVal, resolver, injectee, root, errorCollector);
}
}

for (ResolutionInfo fieldRI : myFields) {
for (SystemInjecteeImpl injectee : fieldRI.injectees) {
Expand Down Expand Up @@ -281,6 +305,22 @@ private void fieldMe(Map<SystemInjecteeImpl, Object> resolved, T t) throws Throw
ReflectionHelper.setField(field, t, putMeIn);
}
}

private void fieldParents(Map<SystemInjecteeImpl, Object> resolved, T t) throws Throwable {
for (ResolutionInfo ri : superFields) {
Field field = (Field) ri.baseElement;
List<SystemInjecteeImpl> injectees = ri.injectees; // Should be only one injectee, itself!

Injectee fieldInjectee = null;
for (Injectee candidate : injectees) {
fieldInjectee = candidate;
}

Object putMeIn = resolved.get(fieldInjectee);

ReflectionHelper.setField(field, t, putMeIn);
}
}

private void methodMe(Map<SystemInjecteeImpl, Object> resolved, T t) throws Throwable {
for (ResolutionInfo ri : myInitializers) {
Expand All @@ -295,6 +335,20 @@ private void methodMe(Map<SystemInjecteeImpl, Object> resolved, T t) throws Thro
ReflectionHelper.invoke(t, m, args, locator.getNeutralContextClassLoader());
}
}

private void methodParents(Map<SystemInjecteeImpl, Object> resolved, T t) throws Throwable {
for (ResolutionInfo ri : superInitializers) {
Method m = (Method) ri.baseElement;
List<SystemInjecteeImpl> injectees = ri.injectees;

Object args[] = new Object[injectees.size()];
for (Injectee injectee : injectees) {
args[injectee.getPosition()] = resolved.get(injectee);
}

ReflectionHelper.invoke(t, m, args, locator.getNeutralContextClassLoader());
}
}

private void postConstructMe(T t) throws Throwable {
if (t == null) return;
Expand Down Expand Up @@ -340,6 +394,12 @@ public T create(ServiceHandle<?> root, SystemDescriptor<?> eventThrower) {

failureLocation = "create";
T retVal = (T) createMe(allResolved);

failureLocation="parent field inject";
fieldParents(allResolved, retVal);

failureLocation = "parent method inject";
methodParents(allResolved, retVal);

failureLocation = "field inject";
fieldMe(allResolved, retVal);
Expand Down
12 changes: 9 additions & 3 deletions hk2-locator/src/main/java/org/jvnet/hk2/internal/Utilities.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Payara Services Ltd.
* Copyright (c) 2020, 2021 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -1419,6 +1419,9 @@ public static Set<Method> findInitializerMethods(
" is static, abstract or has a parameter that is an annotation"));
continue;
}
if (Modifier.isStatic(method.getModifiers())) {
continue;
}

retVal.add(method);
}
Expand Down Expand Up @@ -1485,6 +1488,9 @@ public static Set<Field> findInitializerFields(Class<?> annotatedType,
Pretty.field(field) + " may not be static, final or have an Annotation type"));
continue;
}
if (Modifier.isStatic(field.getModifiers())) {
continue;
}

retVal.add(field);
}
Expand Down Expand Up @@ -1518,7 +1524,7 @@ static AnnotatedElementAnnotationInfo computeAEAI(AnnotatedElement annotatedElem
}

private static boolean isProperMethod(Method member) {
if (ReflectionHelper.isStatic(member)) return false;
//if (ReflectionHelper.isStatic(member)) return false;
if (isAbstract(member)) return false;
for (Class<?> paramClazz : member.getParameterTypes()) {
if (paramClazz.isAnnotation()) {
Expand All @@ -1530,7 +1536,7 @@ private static boolean isProperMethod(Method member) {
}

private static boolean isProperField(Field field) {
if (ReflectionHelper.isStatic(field)) return false;
//if (ReflectionHelper.isStatic(field)) return false;
if (isFinal(field)) return false;
Class<?> type = field.getType();
return !type.isAnnotation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -36,14 +37,10 @@ public class NegativeFieldTest {
*/
@Test
public void testStaticField() {
try {
locator.reifyDescriptor(locator.getBestDescriptor(BuilderHelper.createContractFilter(
StaticFieldService.class.getName())));
Assert.fail("static field should cause failure");
}
catch (MultiException me) {
Assert.assertTrue(me.getMessage().contains(" may not be static, final or have an Annotation type"));
}
StaticFieldService fieldService = locator.getService(StaticFieldService.class);
Assert.assertNull(fieldService.locator);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
*/
public class StaticFieldService {
@Inject
static ServiceLocator locator;
public static ServiceLocator locator;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +17,7 @@

package org.glassfish.hk2.tests.locator.negative.method;

import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.tests.locator.utilities.LocatorHelper;
Expand All @@ -37,12 +39,14 @@ public class NegativeMethodTest {
@Test
public void testStaticMethod() {
try {
locator.reifyDescriptor(locator.getBestDescriptor(BuilderHelper.createContractFilter(
ActiveDescriptor descriptor = locator.reifyDescriptor(locator.getBestDescriptor(BuilderHelper.createContractFilter(
StaticMethodService.class.getName())));
Assert.fail("static method should cause failure");
StaticMethodService methodService = locator.getService(StaticMethodService.class);
}
catch (MultiException me) {
Assert.assertTrue(me.getMessage().contains("is static, abstract or has a parameter that is an annotation"));
me.printStackTrace();
Assert.fail(me.getMessage());
// Assert.assertTrue(me.getMessage(), me.getMessage().contains("is static, abstract or has a parameter that is an annotation"));
}
}

Expand Down
127 changes: 127 additions & 0 deletions hk2-testing/di-tck/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2021 Payara Services Ltd. and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-testing</artifactId>
<version>3.0.2-SNAPSHOT</version>
</parent>

<artifactId>hk2-di-tck-runner</artifactId>
<name>DI TCK Runner</name>
<description>Runner for the Jakarta DI TCK</description>

<properties>
<ditck.version>2.0.1</ditck.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.0</version>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-tck</artifactId>
<version>${ditck.version}</version>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/test-classes</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<includes>
<include>${runSuite}</include>
</includes>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>default-install</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-tck</artifactId>
<version>${ditck.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Loading