Skip to content

Commit b4fcd92

Browse files
ClassNotFoundException from KryoHelper.readObject when running Contes… (#1669)
* ClassNotFoundException from KryoHelper.readObject when running ContestEstimator for seata-core-0.5.0 #1649 Avoid Java 9 code style Add fallback to super (namely to ObjectInputStream)
1 parent aaaa4e0 commit b4fcd92

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

buildSrc/src/main/java/SettingsTemplateHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void proceed(Project project) {
4747
if (s.startsWith("enum class ")) {//Enum class declaration
4848
enums.add(new EnumInfo(s.substring(11, s.length() - 2)));
4949
} else if (s.matches("[A-Z_]+,?") && !enums.isEmpty()) {//Enum value
50-
var enumValue = s.substring(0, s.length());
50+
String enumValue = s.substring(0, s.length());
5151
if (enumValue.endsWith(",")) enumValue = enumValue.substring(0, enumValue.length() - 1);
5252
enums.get(enums.size() - 1).docMap.put(enumValue, new ArrayList<>(docAcc));
5353
} else if (s.startsWith("const val ")) {//Constand value to be substitute later if need
@@ -66,9 +66,9 @@ public static void proceed(Project project) {
6666
s = acc.toString();
6767
acc.delete(0, acc.length());
6868
if (s.startsWith("var") || s.startsWith("val")) {
69-
var i = s.indexOf(" by ", 3);
69+
int i = s.indexOf(" by ", 3);
7070
if (i > 0) {
71-
var key = s.substring(3, i).trim();
71+
String key = s.substring(3, i).trim();
7272
if (key.contains(":")) {
7373
key = key.substring(0, key.lastIndexOf(':'));
7474
}
@@ -91,7 +91,7 @@ public static void proceed(Project project) {
9191
i = s.indexOf('(', i);
9292
if (i > 0) {
9393
s = s.substring(i + 1);
94-
var defaultValue = s.substring(0, s.indexOf(')')).trim();
94+
String defaultValue = s.substring(0, s.indexOf(')')).trim();
9595
if (defaultValue.contains(",")) defaultValue = defaultValue.substring(0, defaultValue.indexOf(','));
9696
defaultValue = dictionary.getOrDefault(defaultValue, defaultValue);
9797
if (defaultValue.matches("[\\d_]+L")) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.utbot.instrumentation.util
2+
3+
import com.esotericsoftware.kryo.kryo5.Kryo
4+
import com.esotericsoftware.kryo.kryo5.KryoException
5+
import com.esotericsoftware.kryo.kryo5.io.Input
6+
import com.esotericsoftware.kryo.kryo5.serializers.JavaSerializer
7+
import java.io.InputStream
8+
import java.io.ObjectInputStream
9+
import java.io.ObjectStreamClass
10+
11+
/**
12+
* This ad-hoc solution for ClassNotFoundException
13+
*/
14+
class JavaSerializerWrapper : JavaSerializer() {
15+
override fun read(kryo: Kryo, input: Input?, type: Class<*>?): Any? {
16+
return try {
17+
val graphContext = kryo.graphContext
18+
var objectStream = graphContext.get<Any>(this) as ObjectInputStream?
19+
if (objectStream == null) {
20+
objectStream = WrappingObjectInputStream(input, kryo)
21+
graphContext.put(this, objectStream)
22+
}
23+
objectStream.readObject()
24+
} catch (ex: java.lang.Exception) {
25+
throw KryoException("Error during Java deserialization.", ex)
26+
}
27+
}
28+
}
29+
30+
class WrappingObjectInputStream(iss : InputStream?, private val kryo: Kryo) : ObjectInputStream(iss) {
31+
override fun resolveClass(type: ObjectStreamClass): Class<*>? {
32+
return try {
33+
Class.forName(type.name, false, kryo.classLoader)
34+
} catch (ex: ClassNotFoundException) {
35+
try {
36+
return Kryo::class.java.classLoader.loadClass(type.name)
37+
} catch (e: ClassNotFoundException) {
38+
try {
39+
return super.resolveClass(type);
40+
} catch (e: ClassNotFoundException) {
41+
throw KryoException("Class not found: " + type.name, e)
42+
}
43+
}
44+
}
45+
}
46+
}

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/util/KryoHelper.kt

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import com.esotericsoftware.kryo.kryo5.io.Output
88
import com.esotericsoftware.kryo.kryo5.objenesis.instantiator.ObjectInstantiator
99
import com.esotericsoftware.kryo.kryo5.objenesis.strategy.InstantiatorStrategy
1010
import com.esotericsoftware.kryo.kryo5.objenesis.strategy.StdInstantiatorStrategy
11-
import com.esotericsoftware.kryo.kryo5.serializers.JavaSerializer
1211
import com.esotericsoftware.kryo.kryo5.util.DefaultInstantiatorStrategy
1312
import com.jetbrains.rd.util.lifetime.Lifetime
1413
import com.jetbrains.rd.util.lifetime.throwIfNotAlive
15-
import org.utbot.api.exception.UtMockAssumptionViolatedException
16-
import org.utbot.framework.plugin.api.TimeoutException
1714
import java.io.ByteArrayOutputStream
1815

1916
/**
@@ -123,47 +120,14 @@ internal class TunedKryo : Kryo() {
123120
}
124121

125122
this.setOptimizedGenerics(false)
126-
register(TimeoutException::class.java, TimeoutExceptionSerializer())
127-
register(UtMockAssumptionViolatedException::class.java, UtMockAssumptionViolatedExceptionSerializer())
128123

129124
// TODO: JIRA:1492
130-
addDefaultSerializer(java.lang.Throwable::class.java, JavaSerializer())
125+
addDefaultSerializer(java.lang.Throwable::class.java, JavaSerializerWrapper())
131126

132127
val factory = object : SerializerFactory.FieldSerializerFactory() {}
133128
factory.config.ignoreSyntheticFields = true
134129
factory.config.serializeTransient = false
135130
factory.config.fieldsCanBeNull = true
136131
this.setDefaultSerializer(factory)
137132
}
138-
139-
/**
140-
* Specific serializer for [TimeoutException] - [JavaSerializer] is not applicable
141-
* because [TimeoutException] is not in class loader.
142-
*
143-
* This serializer is very simple - it just writes [TimeoutException.message]
144-
* because we do not need other components.
145-
*/
146-
private class TimeoutExceptionSerializer : Serializer<TimeoutException>() {
147-
override fun write(kryo: Kryo, output: Output, value: TimeoutException) {
148-
output.writeString(value.message)
149-
}
150-
151-
override fun read(kryo: Kryo?, input: Input, type: Class<out TimeoutException>?): TimeoutException =
152-
TimeoutException(input.readString())
153-
}
154-
155-
/**
156-
* Specific serializer for [UtMockAssumptionViolatedException] - [JavaSerializer] is not applicable
157-
* because [UtMockAssumptionViolatedException] is not in class loader.
158-
*/
159-
private class UtMockAssumptionViolatedExceptionSerializer : Serializer<UtMockAssumptionViolatedException>() {
160-
override fun write(kryo: Kryo, output: Output, value: UtMockAssumptionViolatedException) {
161-
output.writeString(value.message)
162-
}
163-
164-
override fun read(kryo: Kryo?, input: Input, type: Class<out UtMockAssumptionViolatedException>?): UtMockAssumptionViolatedException {
165-
input.readString() // shift the reading position
166-
return UtMockAssumptionViolatedException()
167-
}
168-
}
169133
}

0 commit comments

Comments
 (0)