Skip to content

Commit 637c0b4

Browse files
authored
[Java] Upgrade janino version to fix package name conflict with classname (#1006)
* upgrade janino version * fix non-public class literal * add test * fix private class load * add missing header * refine test
1 parent b9e1b19 commit 637c0b4

File tree

9 files changed

+87
-17
lines changed

9 files changed

+87
-17
lines changed

integration_tests/latest_jdk_tests/src/test/java/io/fury/integration_tests/RecordSerializersTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import static io.fury.collection.Collections.ofHashMap;
2121

2222
import io.fury.Fury;
23-
import io.fury.resolver.MetaContext;
2423
import io.fury.config.CompatibleMode;
24+
import io.fury.resolver.MetaContext;
2525
import io.fury.test.bean.Struct;
2626
import io.fury.util.record.RecordComponent;
2727
import io.fury.util.record.RecordUtils;
@@ -229,6 +229,5 @@ public void testPrivateRecord(boolean codegen) {
229229
System.out.println(deserialized);
230230
}
231231

232-
private record PrivateRecord(String foo) {
233-
}
232+
private record PrivateRecord(String foo) {}
234233
}

java/fury-core/src/main/java/io/fury/builder/BaseObjectCodecBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ protected Tuple2<Reference, Boolean> addClassInfoField(Class<?> cls) {
560560
return classInfoRef;
561561
}
562562
if (!needUpdate) {
563-
Expression clsExpr = new Literal(cls, CLASS_TYPE);
563+
Expression clsExpr = getClassExpr(cls);
564564
classInfoExpr = inlineInvoke(classResolverRef, "getClassInfo", classInfoTypeToken, clsExpr);
565565
// Use `ctx.freshName(cls)` to avoid wrong name for arr type.
566566
String name = ctx.newName(ctx.newName(cls) + "ClassInfo");

java/fury-core/src/main/java/io/fury/builder/CompatibleCodecBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public CompatibleCodecBuilder(
116116
classResolverRef,
117117
"getFieldResolver",
118118
fieldResolverTypeToken,
119-
Literal.ofClass(getRawType(beanType)));
119+
getClassExpr(getRawType(beanType)));
120120
ctx.addField(ctx.type(fieldResolverTypeToken), FIELD_RESOLVER_NAME, fieldResolverExpr);
121121
if (isRecord) {
122122
buildRecordComponentDefaultValues();

java/fury-core/src/main/java/io/fury/codegen/JaninoUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
import java.util.Map;
3737
import java.util.Objects;
3838
import java.util.stream.Collectors;
39-
import org.codehaus.janino.ByteArrayClassLoader;
39+
import org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader;
40+
import org.codehaus.commons.compiler.util.resource.MapResourceCreator;
41+
import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
42+
import org.codehaus.commons.compiler.util.resource.Resource;
4043
import org.codehaus.janino.ClassLoaderIClassLoader;
4144
import org.codehaus.janino.Compiler;
4245
import org.codehaus.janino.util.ClassFile;
43-
import org.codehaus.janino.util.resource.MapResourceCreator;
44-
import org.codehaus.janino.util.resource.MapResourceFinder;
45-
import org.codehaus.janino.util.resource.Resource;
4646
import org.slf4j.Logger;
4747

4848
/**

java/fury-core/src/main/java/io/fury/util/ReflectionUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.base.Preconditions;
2323
import com.google.common.reflect.TypeToken;
24+
import io.fury.annotation.CodegenInvoke;
2425
import io.fury.annotation.Internal;
2526
import io.fury.collection.Tuple3;
2627
import io.fury.util.function.Functions;
@@ -412,10 +413,18 @@ public static String getPackage(Class<?> cls) {
412413
return pkg;
413414
}
414415

415-
// Invoked by JIT.
416+
@CodegenInvoke
416417
public static Class<?> loadClass(Class<?> neighbor, String className) {
417418
try {
418-
return neighbor.getClassLoader().loadClass(className);
419+
if (className.equals(neighbor.getName())) {
420+
return neighbor;
421+
}
422+
ClassLoader classLoader = neighbor.getClassLoader();
423+
if (classLoader == null) {
424+
// jdk class are loaded by bootstrap class loader, which will return null.
425+
classLoader = Thread.currentThread().getContextClassLoader();
426+
}
427+
return classLoader.loadClass(className);
419428
} catch (ClassNotFoundException e) {
420429
throw new RuntimeException(e);
421430
}

java/fury-core/src/test/java/io/fury/builder/ObjectCodecBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import java.util.Map;
4343
import java.util.Set;
4444
import lombok.Data;
45-
import org.codehaus.janino.ByteArrayClassLoader;
45+
import org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader;
4646
import org.testng.Assert;
4747
import org.testng.annotations.DataProvider;
4848
import org.testng.annotations.Test;

java/fury-core/src/test/java/io/fury/codegen/JaninoUtilsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
import java.util.Map;
2828
import java.util.function.Function;
2929
import org.codehaus.commons.compiler.CompileException;
30-
import org.codehaus.janino.ByteArrayClassLoader;
30+
import org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader;
31+
import org.codehaus.commons.compiler.util.resource.MapResourceCreator;
32+
import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
33+
import org.codehaus.commons.compiler.util.resource.Resource;
3134
import org.codehaus.janino.ClassBodyEvaluator;
3235
import org.codehaus.janino.ClassLoaderIClassLoader;
3336
import org.codehaus.janino.Compiler;
3437
import org.codehaus.janino.ScriptEvaluator;
3538
import org.codehaus.janino.SimpleCompiler;
36-
import org.codehaus.janino.util.resource.MapResourceCreator;
37-
import org.codehaus.janino.util.resource.MapResourceFinder;
38-
import org.codehaus.janino.util.resource.Resource;
3939
import org.testng.Assert;
4040
import org.testng.annotations.Test;
4141

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2023 The Fury Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.test;
18+
19+
import io.fury.Fury;
20+
import io.fury.config.CompatibleMode;
21+
import io.fury.config.Language;
22+
import java.io.Serializable;
23+
import java.util.List;
24+
import org.testng.Assert;
25+
import org.testng.annotations.Test;
26+
27+
public class Org implements Serializable {
28+
private static final long serialVersionUID = 1L;
29+
// constructor
30+
public Org() {}
31+
32+
List<Org> children;
33+
34+
public List<Org> getChildren() {
35+
return children;
36+
}
37+
38+
public void setChildren(List<Org> children) {
39+
this.children = children;
40+
}
41+
42+
// test for class name same with package name:
43+
// https://github.com/janino-compiler/janino/issues/165
44+
@Test
45+
public void testOrgPackage() {
46+
Fury fury =
47+
Fury.builder()
48+
.withLanguage(Language.JAVA)
49+
.withRefTracking(true)
50+
// Allow to deserialize objects unknown types,more flexible but less secure.
51+
.requireClassRegistration(false)
52+
.withDeserializeUnexistedClass(true)
53+
.withCompatibleMode(CompatibleMode.COMPATIBLE)
54+
.withRefTracking(true)
55+
.build();
56+
57+
// If the class name is not Org, it can be serialized normally
58+
byte[] bytes = fury.serialize(new Org());
59+
Object o = fury.deserialize(bytes);
60+
Assert.assertEquals(o.getClass(), Org.class);
61+
}
62+
}

java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<maven.compiler.source>1.8</maven.compiler.source>
7373
<maven.compiler.target>1.8</maven.compiler.target>
7474
<guava.version>32.1.2-jre</guava.version>
75-
<janino.version>3.0.11</janino.version>
75+
<janino.version>3.1.10</janino.version>
7676
<arrow.version>5.0.0</arrow.version>
7777
<jackson-bom.version>2.13.2.20220328</jackson-bom.version>
7878
<commons_codec.version>1.13</commons_codec.version>

0 commit comments

Comments
 (0)