Skip to content

Commit

Permalink
feat(InjectionHelper): inject() returns the object for chaining (#5028)
Browse files Browse the repository at this point in the history
* chore: Context.getValue error includes which context it is

* feat(InjectionHelper): inject() returns a value for chaining

* doc(InjectionHelper): add javadoc, mark CoreRegistry use as deprecated
  • Loading branch information
keturn authored Jun 4, 2022
1 parent a0c6606 commit 0a9b89f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.registry;

Expand Down Expand Up @@ -44,6 +44,7 @@ public void testShareRequiresShareAnnotation() {
}

@Test
@SuppressWarnings("removal")
public void testDefaultFieldInjection() {
InjectionHelper.share(serviceA);
InjectionHelper.share(serviceB);
Expand All @@ -57,6 +58,7 @@ public void testDefaultFieldInjection() {
}

@Test
@SuppressWarnings("removal")
public void testInjectUnavailableObject() {
InjectionHelper.share(serviceA);
// InjectionHelper.share(serviceB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public interface Context {
default <T> T getValue(Class<T> type) {
T value = get(type);
if (value == null) {
throw new NoSuchElementException(type.toString());
throw new NoSuchElementException(
String.format("%s has no %s", this, type.getName()));
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.context.internal;

import com.google.common.base.MoreObjects;
import com.google.common.collect.Maps;
import org.terasology.engine.context.Context;

Expand Down Expand Up @@ -49,4 +50,11 @@ public <T, U extends T> void put(Class<T> type, U object) {
map.put(type, object);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("#", Integer.toHexString(hashCode()))
.add("parent", parent)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Terasology Foundation
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.registry;

Expand All @@ -23,8 +23,19 @@ public final class InjectionHelper {
private InjectionHelper() {
}

public static void inject(final Object object, Context context) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
/**
* Inject values from this context to annotated fields.
* <p>
* The injector looks for fields annotated with {@link In @In} and tries to set them by
* getting a value by looking up the field's type in the {@code context}.
* <p>
* If there is no matching value in the context, the field is silently skipped and left
* with its previous value.
*
* @return the input {@code object}, now with fields set
*/
public static <T> T inject(final T object, Context context) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
for (Field field : ReflectionUtils.getAllFields(object.getClass(), ReflectionUtils.withAnnotation(In.class))) {
Object value = context.get(field.getType());
if (value != null) {
Expand All @@ -39,8 +50,17 @@ public static void inject(final Object object, Context context) {

return null;
});
return object;
}

/**
* Inject values from CoreRegistry to annotated fields.
*
* @deprecated CoreRegistry-based methods are deprecated in favor of the more thread-safe Context.
*
* @see #inject(Object, Context)
*/
@Deprecated(since = "5.3.0", forRemoval = true)
public static void inject(final Object object) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
for (Field field : ReflectionUtils.getAllFields(object.getClass(), ReflectionUtils.withAnnotation(In.class))) {
Expand Down

0 comments on commit 0a9b89f

Please sign in to comment.