Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6755c51
commit a75fd9d
Showing
3 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-solver-core/src/main/java/me/tomassetti/symbolsolver/logic/FunctionalInterfaceLogic.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,25 @@ | |||
package me.tomassetti.symbolsolver.logic; | |||
|
|||
import me.tomassetti.symbolsolver.model.invokations.MethodUsage; | |||
import me.tomassetti.symbolsolver.model.typesystem.TypeUsage; | |||
|
|||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
|
|||
public class FunctionalInterfaceLogic { | |||
|
|||
public static Optional<MethodUsage> getFunctionalMethod(TypeUsage type) { | |||
if (type.isReferenceType() && type.asReferenceTypeUsage().getTypeDeclaration().isInterface()) { | |||
//We need to find all abstract methods | |||
Set<MethodUsage> methods = type.asReferenceTypeUsage().getTypeDeclaration().getAllMethods().stream().filter(m -> m.getDeclaration().isAbstract()).collect(Collectors.toSet()); | |||
if (methods.size() == 1) { | |||
return Optional.of(methods.iterator().next()); | |||
} else { | |||
return Optional.empty(); | |||
} | |||
} else { | |||
return Optional.empty(); | |||
} | |||
} | |||
} |
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
35 changes: 35 additions & 0 deletions
35
...olver-core/src/test/java/me/tomassetti/symbolsolver/logic/FunctionInterfaceLogicTest.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
package me.tomassetti.symbolsolver.logic; | |||
|
|||
import me.tomassetti.symbolsolver.model.resolution.TypeSolver; | |||
import me.tomassetti.symbolsolver.model.typesystem.ReferenceTypeUsageImpl; | |||
import me.tomassetti.symbolsolver.model.typesystem.TypeUsage; | |||
import me.tomassetti.symbolsolver.reflectionmodel.ReflectionClassDeclaration; | |||
import me.tomassetti.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration; | |||
import me.tomassetti.symbolsolver.resolution.typesolvers.JreTypeSolver; | |||
import org.junit.Test; | |||
|
|||
import java.util.function.Consumer; | |||
import java.util.function.Function; | |||
|
|||
import static org.junit.Assert.*; | |||
|
|||
public class FunctionInterfaceLogicTest { | |||
|
|||
@Test | |||
public void testGetFunctionalMethodNegativeCaseOnClass() { | |||
TypeSolver typeSolver = new JreTypeSolver(); | |||
TypeUsage string = new ReferenceTypeUsageImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver); | |||
assertEquals(false, FunctionalInterfaceLogic.getFunctionalMethod(string).isPresent()); | |||
} | |||
|
|||
@Test | |||
public void testGetFunctionalMethodPositiveCasesOnInterfaces() { | |||
TypeSolver typeSolver = new JreTypeSolver(); | |||
TypeUsage function = new ReferenceTypeUsageImpl(new ReflectionInterfaceDeclaration(Function.class, typeSolver), typeSolver); | |||
assertEquals(true, FunctionalInterfaceLogic.getFunctionalMethod(function).isPresent()); | |||
assertEquals("apply", FunctionalInterfaceLogic.getFunctionalMethod(function).get().getName()); | |||
TypeUsage consumer = new ReferenceTypeUsageImpl(new ReflectionInterfaceDeclaration(Consumer.class, typeSolver), typeSolver); | |||
assertEquals(true, FunctionalInterfaceLogic.getFunctionalMethod(consumer).isPresent()); | |||
assertEquals("accept", FunctionalInterfaceLogic.getFunctionalMethod(consumer).get().getName()); | |||
} | |||
} |