-
Notifications
You must be signed in to change notification settings - Fork 2
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
5c6df5c
commit 9a28deb
Showing
10 changed files
with
437 additions
and
280 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
...ions/src/main/java/pl/com/labaj/autorecord/extension/compact/AdditionalMethodBuilder.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,91 @@ | ||
package pl.com.labaj.autorecord.extension.compact; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.ParameterSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeVariableName; | ||
import pl.com.labaj.autorecord.context.Logger; | ||
import pl.com.labaj.autorecord.context.StaticImports; | ||
|
||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Types; | ||
import java.util.Map; | ||
|
||
import static javax.lang.model.element.Modifier.PRIVATE; | ||
import static javax.lang.model.element.Modifier.STATIC; | ||
import static pl.com.labaj.autorecord.extension.compact.ImmutableCollectionsExtension.ENUM_SET; | ||
import static pl.com.labaj.autorecord.extension.compact.ImmutableCollectionsExtension.GUAVA_IMMUTABLE_SORTED_SET_CLASS_NAME; | ||
import static pl.com.labaj.autorecord.extension.compact.ImmutableCollectionsExtension.SET; | ||
import static pl.com.labaj.autorecord.extension.compact.ImmutableCollectionsExtension.SORTED_SET; | ||
|
||
class AdditionalMethodBuilder { | ||
private static final Map<String, MethodBuilder> METHOD_BUILDERS = Map.of( | ||
SET, builderForSet() | ||
); | ||
|
||
private final Types typeUtils; | ||
|
||
private final Map<String, TypeMirror> immutableTypes; | ||
private final Map<String, TypeMirror> collectionTypes; | ||
private final StaticImports staticImports; | ||
private final Logger logger; | ||
|
||
AdditionalMethodBuilder(Types typeUtils, | ||
Map<String, TypeMirror> immutableTypes, | ||
Map<String, TypeMirror> collectionTypes, | ||
StaticImports staticImports, | ||
Logger logger) { | ||
this.typeUtils = typeUtils; | ||
this.immutableTypes = immutableTypes; | ||
this.collectionTypes = collectionTypes; | ||
this.staticImports = staticImports; | ||
this.logger = logger; | ||
} | ||
|
||
MethodSpec buildMethodFor(String mark) { | ||
return METHOD_BUILDERS.get(mark).buildMethod(typeUtils, immutableTypes, collectionTypes, staticImports, logger); | ||
} | ||
|
||
private static MethodBuilder builderForSet() { | ||
return (typeUtils, immutableTypes, collectionTypes, staticImports, logger) -> { | ||
var typeVariableName = TypeVariableName.get("E"); | ||
var setType = collectionTypes.get(SET); | ||
var className = ClassName.get((TypeElement) typeUtils.asElement(setType)); | ||
var setTypeName = ParameterizedTypeName.get(className, typeVariableName); | ||
var parameterSpec = ParameterSpec.builder(setTypeName, "set").build(); | ||
var methodBuilder = MethodSpec.methodBuilder("_immutableSet") | ||
.addModifiers(PRIVATE, STATIC) | ||
.addTypeVariable(typeVariableName) | ||
.returns(setTypeName) | ||
.addParameter(parameterSpec); | ||
|
||
var enumSetType = immutableTypes.get(ENUM_SET); | ||
immutableTypes.values().stream() | ||
.filter(immutableType -> typeUtils.isSubtype(immutableType, setType)) | ||
.forEach(immutableType -> { | ||
var isEnumSet = typeUtils.isSameType(immutableType, enumSetType); | ||
methodBuilder.beginControlFlow("if (set instanceof $T<$L>)", immutableType, isEnumSet ? '?' : 'E') | ||
.addStatement("return set") | ||
.endControlFlow(); | ||
}); | ||
|
||
methodBuilder.beginControlFlow("if (set instanceof $T<E> sortedSet)", collectionTypes.get(SORTED_SET)) | ||
.addStatement("return $T.copyOfSorted(sortedSet)", GUAVA_IMMUTABLE_SORTED_SET_CLASS_NAME) | ||
.endControlFlow() | ||
.addStatement("return set"); | ||
|
||
return methodBuilder.build(); | ||
}; | ||
} | ||
|
||
@FunctionalInterface | ||
private static interface MethodBuilder { | ||
MethodSpec buildMethod(Types typeUtils, | ||
Map<String, TypeMirror> immutableTypes, | ||
Map<String, TypeMirror> collectionTypes, | ||
StaticImports staticImports, | ||
Logger logger); | ||
} | ||
} |
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
Oops, something went wrong.