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
Showing
7 changed files
with
135 additions
and
80 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
39 changes: 39 additions & 0 deletions
39
...aker_annotation_processors/processors/document/conversion/element/ClassTypeConverter.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,39 @@ | ||
package com.blamejared.crafttweaker_annotation_processors.processors.document.conversion.element; | ||
|
||
import javax.lang.model.type.MirroredTypeException; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import java.lang.annotation.Annotation; | ||
import java.util.function.Function; | ||
|
||
public class ClassTypeConverter { | ||
|
||
private final Elements elementUtils; | ||
|
||
public ClassTypeConverter(Elements elementUtils) { | ||
this.elementUtils = elementUtils; | ||
} | ||
|
||
public <T extends Annotation> TypeMirror getTypeMirror(T annotation, Function<T, Class<?>> classGetter) { | ||
try { | ||
return getNativeTypeFromClass(classGetter.apply(annotation)); | ||
} catch(MirroredTypeException exception) { | ||
return getFromMirroredTypeException(exception); | ||
} catch(TypeNotPresentException exception) { | ||
return getFromTypeNotPresentException(exception); | ||
} | ||
} | ||
|
||
private TypeMirror getFromTypeNotPresentException(TypeNotPresentException exception) { | ||
final String typeName = exception.typeName(); | ||
return elementUtils.getTypeElement(typeName).asType(); | ||
} | ||
|
||
private TypeMirror getFromMirroredTypeException(MirroredTypeException exception) { | ||
return exception.getTypeMirror(); | ||
} | ||
|
||
private TypeMirror getNativeTypeFromClass(Class<?> cls) { | ||
return elementUtils.getTypeElement(cls.getCanonicalName()).asType(); | ||
} | ||
} |
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
67 changes: 43 additions & 24 deletions
67
...processors/processors/document/native_types/dependency_rule/NativeTypeConversionRule.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 |
---|---|---|
@@ -1,75 +1,94 @@ | ||
package com.blamejared.crafttweaker_annotation_processors.processors.document.native_types.dependency_rule; | ||
|
||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.info.*; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.*; | ||
import com.blamejared.crafttweaker_annotations.annotations.*; | ||
import org.jetbrains.annotations.*; | ||
import org.reflections.*; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.conversion.element.ClassTypeConverter; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.info.TypeName; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.info.TypePageInfo; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.AbstractTypeInfo; | ||
import com.blamejared.crafttweaker_annotation_processors.processors.document.page.type.TypePageTypeInfo; | ||
import com.blamejared.crafttweaker_annotations.annotations.Document; | ||
import com.blamejared.crafttweaker_annotations.annotations.NativeTypeRegistration; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.reflections.Reflections; | ||
|
||
import javax.lang.model.element.*; | ||
import javax.lang.model.util.*; | ||
import java.util.*; | ||
import java.util.function.*; | ||
import java.util.stream.*; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
public class NativeTypeConversionRule implements ModDependencyConversionRule { | ||
|
||
private final Reflections reflections; | ||
private final Elements elementUtils; | ||
private final Types typeUtils; | ||
private final ClassTypeConverter classTypeConverter; | ||
|
||
public NativeTypeConversionRule(Reflections reflections, Elements elementUtils) { | ||
public NativeTypeConversionRule(Reflections reflections, Elements elementUtils, Types typeUtils, ClassTypeConverter classTypeConverter) { | ||
this.reflections = reflections; | ||
this.elementUtils = elementUtils; | ||
this.typeUtils = typeUtils; | ||
this.classTypeConverter = classTypeConverter; | ||
} | ||
|
||
@Override | ||
public Map<TypeElement, AbstractTypeInfo> getAll() { | ||
return getNativeExpansionClasses().stream().filter(this::isDocumented).collect(createTypeInfoMap()); | ||
return getNativeExpansionClasses().stream() | ||
.map(this::getTypeElementFromClass) | ||
.filter(this::isDocumented) | ||
.collect(createTypeInfoMap()); | ||
} | ||
|
||
private Collector<Class<?>, ?, Map<TypeElement, AbstractTypeInfo>> createTypeInfoMap() { | ||
final Function<Class<?>, TypeElement> keyMapper = this::getTypeElementFromClass; | ||
final Function<Class<?>, AbstractTypeInfo> valueMapper = this::getTypeInfoFromClass; | ||
private Collector<TypeElement, ?, Map<TypeElement, AbstractTypeInfo>> createTypeInfoMap() { | ||
final Function<TypeElement, TypeElement> keyMapper = Function.identity(); | ||
final Function<TypeElement, AbstractTypeInfo> valueMapper = this::getTypeInfoFromClass; | ||
|
||
return Collectors.toMap(keyMapper, valueMapper); | ||
} | ||
|
||
private TypeElement getTypeElementFromClass(Class<?> documentedClass) { | ||
final NativeTypeRegistration nativeAnnotation = getNativeAnnotation(documentedClass); | ||
return elementUtils.getTypeElement(nativeAnnotation.value().getCanonicalName()); | ||
final TypeElement expansionTypeElement = elementUtils.getTypeElement(documentedClass.getCanonicalName()); | ||
|
||
final NativeTypeRegistration nativeAnnotation = getNativeAnnotation(expansionTypeElement); | ||
final TypeMirror nativeType = classTypeConverter.getTypeMirror(nativeAnnotation, NativeTypeRegistration::value); | ||
|
||
return (TypeElement) typeUtils.asElement(nativeType); | ||
} | ||
|
||
private AbstractTypeInfo getTypeInfoFromClass(Class<?> documentedClass) { | ||
private AbstractTypeInfo getTypeInfoFromClass(TypeElement documentedClass) { | ||
final TypePageInfo pageInfo = getPageInfoFromClass(documentedClass); | ||
return new TypePageTypeInfo(pageInfo); | ||
} | ||
|
||
@NotNull | ||
private TypePageInfo getPageInfoFromClass(Class<?> documentedClass) { | ||
private TypePageInfo getPageInfoFromClass(TypeElement documentedClass) { | ||
final TypeName typeName = getTypeNameFromClass(documentedClass); | ||
final String path = getDocPathFromClass(documentedClass); | ||
|
||
return new TypePageInfo("unknown", path, typeName); | ||
} | ||
|
||
private TypeName getTypeNameFromClass(Class<?> documentedClass) { | ||
private TypeName getTypeNameFromClass(TypeElement documentedClass) { | ||
final NativeTypeRegistration annotation = getNativeAnnotation(documentedClass); | ||
return new TypeName(annotation.zenCodeName()); | ||
} | ||
|
||
private NativeTypeRegistration getNativeAnnotation(Class<?> documentedClass) { | ||
private NativeTypeRegistration getNativeAnnotation(TypeElement documentedClass) { | ||
return documentedClass.getAnnotation(NativeTypeRegistration.class); | ||
} | ||
|
||
private String getDocPathFromClass(Class<?> documentedClass) { | ||
private String getDocPathFromClass(TypeElement documentedClass) { | ||
return documentedClass.getAnnotation(Document.class).value(); | ||
} | ||
|
||
private Set<Class<?>> getNativeExpansionClasses() { | ||
return reflections.getTypesAnnotatedWith(NativeTypeRegistration.class); | ||
} | ||
|
||
private boolean isDocumented(Class<?> nativeRegistrationClass) { | ||
return nativeRegistrationClass.isAnnotationPresent(Document.class); | ||
private boolean isDocumented(TypeElement nativeRegistrationClass) { | ||
return getNativeAnnotation(nativeRegistrationClass) != null; | ||
} | ||
} |
Oops, something went wrong.