Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum value support #28

Merged
merged 4 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions parser/src/main/kotlin/com/linkedin/dex/parser/DecodedValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ sealed class DecodedValue {
data class DecodedType(val value: String) : DecodedValue()
object DecodedNull : DecodedValue()
data class DecodedBoolean(val value: Boolean) : DecodedValue()
data class DecodedEnum(val value: String) : DecodedValue()
// TODO: DecodedType
// TODO: DecodedField
// TODO: DecodedMethod
// TODO: DecodedEnum
// TODO: DecodedArrayValue
// TODO: DecodedAnnotationValue

companion object {
private fun readStringInPosition(dexFile: DexFile, position: Int): String {
dexFile.byteBuffer.position(position)
// read past unused size item
Leb128.readUnsignedLeb128(dexFile.byteBuffer)
return ParseUtils.parseStringBytes(dexFile.byteBuffer)
}

/**
* Resolve an encoded value against the given dexfile
*/
Expand All @@ -45,20 +52,20 @@ sealed class DecodedValue {
is EncodedValue.EncodedFloat -> return DecodedFloat(encodedValue.value)
is EncodedValue.EncodedDouble -> return DecodedDouble(encodedValue.value)
is EncodedValue.EncodedString -> {
dexFile.byteBuffer.position(dexFile.stringIds[encodedValue.value].stringDataOff)
// read past unused size item
Leb128.readUnsignedLeb128(dexFile.byteBuffer)
return DecodedString(ParseUtils.parseStringBytes(dexFile.byteBuffer))
val position = dexFile.stringIds[encodedValue.value].stringDataOff
return DecodedString(readStringInPosition(dexFile, position))
}
is EncodedValue.EncodedType -> {
dexFile.byteBuffer.position(dexFile.typeIds[encodedValue.value].descriptorIdx)
// read past unused size item
Leb128.readUnsignedLeb128(dexFile.byteBuffer)
return DecodedType(ParseUtils.parseStringBytes(dexFile.byteBuffer))
val position = dexFile.typeIds[encodedValue.value].descriptorIdx
return DecodedType(readStringInPosition(dexFile, position))
}
is EncodedValue.EncodedBoolean -> return DecodedBoolean(encodedValue.value)
is EncodedValue.EncodedNull -> return DecodedNull

is EncodedValue.EncodedEnum -> {
val index = dexFile.fieldIds[encodedValue.value].nameIdx
val position = dexFile.stringIds[index].stringDataOff
return DecodedEnum(readStringInPosition(dexFile, position))
}
else -> return DecodedNull
}
}
Expand Down
11 changes: 11 additions & 0 deletions parser/src/test/kotlin/com/linkedin/dex/DexParserShould.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ class DexParserShould {
assertMatches(methodAnnotation.values["longValue"], 56789L)
}

@Test
fun parseEnumAnnotation() {
val method = getSecondBasicJunit4TestMethod()
val valueAnnotations = method.annotations.filter { it.name.contains("TestValueAnnotation") }

val methodAnnotation = valueAnnotations[1]
assertMatches(methodAnnotation.values["enumValue"], "FAIL")
}

private fun getBasicJunit4TestMethod(): TestMethod {
val testMethods = DexParser.findTestMethods(APK_PATH).filter { it.annotations.filter { it.name.contains("TestValueAnnotation") }.isNotEmpty() }.filter { it.testName.equals("com.linkedin.parser.test.junit4.java.BasicJUnit4#basicJUnit4") }

Expand All @@ -176,6 +185,8 @@ class DexParserShould {
private fun assertMatches(value: DecodedValue?, string: String) {
if (value is DecodedValue.DecodedString) {
assertEquals(string, value.value)
} else if (value is DecodedValue.DecodedEnum) {
assertEquals(string, value.value)
} else {
throw Exception("Value was not a string type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
public class BasicJUnit4 extends ConcreteTest {

@Test
@TestValueAnnotation(stringValue = "On a method", intValue = 12345, boolValue = true, longValue = 56789L)
@TestValueAnnotation(stringValue = "On a method", intValue = 12345, boolValue = true, longValue = 56789L, enumValue = TestEnum.SUCCESS)
public void basicJUnit4() {
assertTrue(true);
}

@Test
@TestValueAnnotation(floatValue = 0.25f, doubleValue = 0.5, byteValue = 0x0f, charValue = '?', shortValue = 3)
@TestValueAnnotation(floatValue = 0.25f, doubleValue = 0.5, byteValue = 0x0f, charValue = '?', shortValue = 3, enumValue = TestEnum.FAIL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I'd keep current pattern that one type only shows up once, either in basicJUnit4() or basicJUnit4Second()

@FloatRange(from = 0f, to = Float.MAX_VALUE)
public void basicJUnit4Second() {
assertTrue(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.linkedin.parser.test.junit4.java;

public enum TestEnum {
SUCCESS,
FAIL
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@
char charValue() default 0;

short shortValue() default 0;

TestEnum enumValue() default TestEnum.SUCCESS;
}