-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect application classes with missing or final supertypes and acces…
…s-check protected members (#13) Resolves the type hierarchy for all application types, so that we can detect missing and final supertypes as well as do access checks for protected members. Resolves the following issues: * #9 * #10 * #12
- Loading branch information
Showing
18 changed files
with
309 additions
and
159 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
19 changes: 12 additions & 7 deletions
19
core/src/main/kotlin/com/toasttab/expediter/AccessCheck.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 |
---|---|---|
@@ -1,21 +1,26 @@ | ||
package com.toasttab.expediter | ||
|
||
import com.toasttab.expediter.types.AccessProtection | ||
import com.toasttab.expediter.types.ApplicationTypeWithResolvedHierarchy | ||
import com.toasttab.expediter.types.MemberDescriptor | ||
import com.toasttab.expediter.types.MemberType | ||
import com.toasttab.expediter.types.ResolvedTypeHierarchy | ||
import com.toasttab.expediter.types.TypeDescriptor | ||
|
||
object AccessCheck { | ||
fun <M : MemberType> allowedAccess(caller: TypeDescriptor, target: TypeDescriptor, member: MemberDescriptor<M>): Boolean { | ||
fun <M : MemberType> allowedAccess(caller: ApplicationTypeWithResolvedHierarchy, target: TypeDescriptor, member: MemberDescriptor<M>): Boolean { | ||
return if (member.protection == AccessProtection.PRIVATE) { | ||
return caller.sameClassAs(target) | ||
caller.name == target.name | ||
} else if (member.protection == AccessProtection.PACKAGE_PRIVATE || target.protection == AccessProtection.PACKAGE_PRIVATE) { | ||
return caller.samePackageAs(target) | ||
samePackage(caller.name, target.name) | ||
} else if (member.protection == AccessProtection.PROTECTED) { | ||
samePackage(caller.name, target.name) || | ||
// if we can't resolve all supertypes, we can't say for sure that access is not allowed | ||
caller.hierarchy !is ResolvedTypeHierarchy.CompleteTypeHierarchy || | ||
caller.hierarchy.allTypes.any { it.name == target.name } | ||
} else { | ||
true | ||
} // TODO: add other checks | ||
} | ||
} | ||
|
||
private fun TypeDescriptor.sameClassAs(other: TypeDescriptor) = name == other.name | ||
private fun TypeDescriptor.samePackageAs(other: TypeDescriptor) = name.substringBeforeLast('/') == other.name.substringBeforeLast('/') | ||
private fun samePackage(a: String, b: String) = a.substringBeforeLast('/') == b.substringBeforeLast('/') | ||
} |
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
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.