You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to the way testing works [1] many CompileResult object are kept in memory leading to OutOfMemoryError when running the test suite. See more details in original issue (closed) [2]
Based on a suggestion to clean them automatically seems the right approach but with some difficulties. See the below draft code. We cannot set any private object fields to null. Therefore finally we may have to search CompileResult object specifically and warn to issue exception if they are private. This may look like an unnecessary restriction to programmers. To reduce the hassle we can skip private check for primitive fields fieldType.isPrimitive().
Also JaCoCo introduce synthetic fields [3].
My code uses reflection. Why does it fail when I execute it with JaCoCo?
To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private ? > static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.
Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates > synthetic members in certain situation.
packageorg.ballerinalang.test;
importorg.testng.annotations.AfterClass;
importjava.lang.reflect.Field;
publicclassBaseTest {
protectedbooleanskipPrivateFields = true;
protectedbooleanskipFinalFields = true;
@AfterClass(alwaysRun = true)
publicvoiddestroy() throwsException {
Class<?> compileResultClass = CompileResult.class;
Class<?> compileResultUtilClass = org.ballerinalang.test.util.CompileResult.class;
for (Fieldfield : getClass().getDeclaredFields()) {
/* To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic. */if (field.isSynthetic()) {
continue;
}
varfieldType = field.getType();
if (fieldType.isPrimitive()) {
continue;
}
varfieldModifiers = field.getModifiers();
booleanisPrivate = java.lang.reflect.Modifier.isPrivate(fieldModifiers);
booleanisFinal = java.lang.reflect.Modifier.isFinal(fieldModifiers);
booleanisCompileResult = fieldType.isAssignableFrom(compileResultClass) ||
fieldType.isAssignableFrom(compileResultUtilClass);
if (isCompileResult) {
/* make sure to forcefully ask users to change `CompileResult` field modifiers to accessible ones */if (!field.canAccess(this)) {
field.setAccessible(true);
}
if (isPrivate) {
thrownewPrivateFieldException(field);
}
if (isFinal) {
thrownewFinalFieldException(field);
}
}
if (isPrivate) {
if (skipPrivateFields) {
continue;
}
thrownewPrivateFieldException(field);
}
if (isFinal) {
if (skipFinalFields) {
continue;
}
thrownewFinalFieldException(field);
}
if (field.get(this) != null) {
field.set(this, null);
}
}
}
}
...
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Due to the way testing works [1] many
CompileResult
object are kept in memory leading to OutOfMemoryError when running the test suite. See more details in original issue (closed) [2]Based on a suggestion to clean them automatically seems the right approach but with some difficulties. See the below draft code. We cannot set any private object fields to null. Therefore finally we may have to search
CompileResult
object specifically and warn to issue exception if they are private. This may look like an unnecessary restriction to programmers. To reduce the hassle we can skip private check for primitive fieldsfieldType.isPrimitive()
.Also JaCoCo introduce synthetic fields [3].
[1] #27856 (comment)
[2] #27856
[3] https://www.jacoco.org/jacoco/trunk/doc/faq.html
Beta Was this translation helpful? Give feedback.
All reactions