Skip to content

Commit

Permalink
Merge pull request google#942 from tavianator/skip-static-interface-m…
Browse files Browse the repository at this point in the history
…ethods

assistedinject: Skip static methods on interfaces.
  • Loading branch information
sameb committed Jul 6, 2015
2 parents 6a0ae37 + 87cd5ad commit abc78c3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public TypeLiteral<?> getImplementationType() {
ImmutableMap.Builder<Method, AssistData> assistDataBuilder = ImmutableMap.builder();
// TODO: also grab methods from superinterfaces
for (Method method : factoryRawType.getMethods()) {
// Skip static methods
if (Modifier.isStatic(method.getModifiers())) {
continue;
}

// Skip default methods that java8 may have created.
if (isDefault(method) && (method.isBridge() || method.isSynthetic())) {
// Even synthetic default methods need the return type validation...
Expand Down
6 changes: 6 additions & 0 deletions jdk8-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ See the Apache License Version 2.0 for the specific language governing permissio
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (C) 2015 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.jdk8;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.FactoryModuleBuilder;

import junit.framework.TestCase;

/**
* Test static methods in interfaces.
*
* @author [email protected] (Tavian Barnes)
*/
public class StaticInterfaceMethodsTest extends TestCase {

private static class Thing {
final int i;

@Inject
Thing(@Assisted int i) {
this.i = i;
}
}

private interface Factory {
Thing create(int i);

static Factory getDefault() {
return Thing::new;
}
}

public void testAssistedInjection() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().build(Factory.class));
}
});
Factory factory = injector.getInstance(Factory.class);
assertEquals(1, factory.create(1).i);
}

}

0 comments on commit abc78c3

Please sign in to comment.