-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@PassthroughDefaultMethods
annotation to allow using default me…
…thods on assisted factories (but only when explicitly requested by the user, see #1347 (comment))
- Loading branch information
Showing
4 changed files
with
133 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...nsions/assistedinject/src/com/google/inject/assistedinject/PassthroughDefaultMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2023 Google Inc. | ||
* | ||
* 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 com.google.inject.assistedinject; | ||
|
||
import com.google.inject.BindingAnnotation; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.lang.invoke.MethodHandles; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Annotates a factory interface to indicate its default methods | ||
* should be pass-through on the generated factory implementation, | ||
* instead of treated as standard factory methods. | ||
* | ||
* <p>This annotation may also be used on individual default methods | ||
* of factory interfaces, but it is named with the assumption that | ||
* the general use case wants default methods treated in a uniform | ||
* fashion for an entire factory.</p> | ||
* | ||
* @see FactoryModuleBuilder#withLookups(MethodHandles.Lookup) | ||
*/ | ||
@BindingAnnotation | ||
@Target({METHOD, TYPE}) | ||
@Retention(RUNTIME) | ||
public @interface PassthroughDefaultMethods { | ||
} |
64 changes: 64 additions & 0 deletions
64
...s/assistedinject/test/com/google/inject/assistedinject/PassthroughDefaultMethodsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2023 Google Inc. | ||
* | ||
* 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 com.google.inject.assistedinject; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
import java.lang.invoke.MethodHandles; | ||
import junit.framework.TestCase; | ||
|
||
public class PassthroughDefaultMethodsTest extends TestCase { | ||
private static class Thing { | ||
final int i; | ||
|
||
@Inject | ||
Thing(@Assisted int i) { | ||
this.i = i; | ||
} | ||
} | ||
|
||
@PassthroughDefaultMethods | ||
private interface Factory { | ||
Thing create(int i); | ||
|
||
default Thing one() { | ||
return this.create(1); | ||
} | ||
|
||
default Thing createPow(int i, int pow) { | ||
return this.create((int) Math.pow(i, pow)); | ||
} | ||
} | ||
|
||
public void testAssistedInjection() throws IllegalAccessException { | ||
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(Factory.class, MethodHandles.lookup()); | ||
Injector injector = | ||
Guice.createInjector( | ||
new AbstractModule() { | ||
@Override | ||
protected void configure() { | ||
install(new FactoryModuleBuilder().withLookups(lookup).build(Factory.class)); | ||
} | ||
}); | ||
Factory factory = injector.getInstance(Factory.class); | ||
assertEquals(1, factory.create(1).i); | ||
assertEquals(1, factory.one().i); | ||
assertEquals(256, factory.createPow(2, 8).i); | ||
} | ||
} |