-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Cmyna/development-alpha
FIX issues
- Loading branch information
Showing
5 changed files
with
78 additions
and
22 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
41 changes: 41 additions & 0 deletions
41
api/src/main/kotlin/net/myna/mnbt/reflect/InstanceAsTool.kt
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,41 @@ | ||
package net.myna.mnbt.reflect | ||
|
||
import net.myna.mnbt.annotations.InstanceAs | ||
import net.myna.mnbt.codec.binary.RArray | ||
import net.myna.mnbt.exceptions.InvalidInstanceAsClassException | ||
import net.myna.mnbt.reflect.TypeCheckTool.isInterfaceOrAbstract | ||
import java.lang.reflect.Field | ||
import java.lang.reflect.Modifier | ||
|
||
object InstanceAsTool { | ||
|
||
fun resolveAnnotation(field: Field):MTypeToken<out Any> { | ||
val instanceAsAnnotation = field.getAnnotation(InstanceAs::class.java) | ||
if (instanceAsAnnotation!=null) { | ||
val wrappedClass = instanceAsAnnotation.instanceClass.java | ||
val token = MTypeToken.of(wrappedClass) | ||
//val declaredType = if (field.type.isArray) field.type.componentType else field.type | ||
val declaredType = field.type | ||
|
||
if (!TypeCheckTool.isCastable(token, MTypeToken.of(declaredType))) { | ||
throw InvalidInstanceAsClassException( | ||
wrappedClass, | ||
declaredType, | ||
InvalidInstanceAsClassException.ExceptionType.NOT_SUBTYPE | ||
) | ||
} | ||
if (isInterfaceOrAbstract(wrappedClass)) { | ||
throw InvalidInstanceAsClassException( | ||
wrappedClass, declaredType, | ||
InvalidInstanceAsClassException.ExceptionType.IS_ABSTRACT | ||
) | ||
} | ||
|
||
// if (field.type.isArray) { | ||
// // return array type declaration | ||
// return RArray.newInstance(token.rawType, 0).javaClass.let { MTypeToken.of(it) } | ||
// } | ||
return token | ||
} else return MTypeToken.of(field.genericType) | ||
} | ||
} |
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