forked from apache/fury
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(java): Streamline ClassResolver responsibilities
To reduce the complexity of the `ClassResolver` class, we mainly do the following two things: 1. Move ClassId related responsibilities into ClassIdAllocator class. 2. Move `registeredId2ClassInfo` into `ExtRegistry` static inner class since they are content related. Signed-off-by: LiangliangSui <[email protected]>
- Loading branch information
1 parent
c294885
commit f0e34b4
Showing
15 changed files
with
419 additions
and
264 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ on: | |
push: | ||
branches: | ||
- main | ||
- sll3 | ||
- 'releases/**' | ||
- 'deploy/**' | ||
- 'test*' | ||
|
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
115 changes: 115 additions & 0 deletions
115
java/fury-core/src/main/java/org/apache/fury/resolver/ClassIdAllocator.java
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.fury.resolver; | ||
|
||
import java.util.function.Function; | ||
import org.apache.fury.util.Preconditions; | ||
|
||
/** Responsible for allocating ClassId and maintaining built-in ClassId. */ | ||
public class ClassIdAllocator { | ||
public static class BuiltinClassId { | ||
// preserve 0 as flag for class id not set in ClassInfo` | ||
public static final short NO_CLASS_ID = (short) 0; | ||
public static final short LAMBDA_STUB_ID = 1; | ||
public static final short JDK_PROXY_STUB_ID = 2; | ||
public static final short REPLACE_STUB_ID = 3; | ||
// Note: following pre-defined class id should be continuous, since they may be used based | ||
// range. | ||
public static final short PRIMITIVE_VOID_CLASS_ID = (short) (REPLACE_STUB_ID + 1); | ||
public static final short PRIMITIVE_BOOLEAN_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 1); | ||
public static final short PRIMITIVE_BYTE_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 2); | ||
public static final short PRIMITIVE_CHAR_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 3); | ||
public static final short PRIMITIVE_SHORT_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 4); | ||
public static final short PRIMITIVE_INT_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 5); | ||
public static final short PRIMITIVE_FLOAT_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 6); | ||
public static final short PRIMITIVE_LONG_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 7); | ||
public static final short PRIMITIVE_DOUBLE_CLASS_ID = (short) (PRIMITIVE_VOID_CLASS_ID + 8); | ||
public static final short VOID_CLASS_ID = (short) (PRIMITIVE_DOUBLE_CLASS_ID + 1); | ||
public static final short BOOLEAN_CLASS_ID = (short) (VOID_CLASS_ID + 1); | ||
public static final short BYTE_CLASS_ID = (short) (VOID_CLASS_ID + 2); | ||
public static final short CHAR_CLASS_ID = (short) (VOID_CLASS_ID + 3); | ||
public static final short SHORT_CLASS_ID = (short) (VOID_CLASS_ID + 4); | ||
public static final short INTEGER_CLASS_ID = (short) (VOID_CLASS_ID + 5); | ||
public static final short FLOAT_CLASS_ID = (short) (VOID_CLASS_ID + 6); | ||
public static final short LONG_CLASS_ID = (short) (VOID_CLASS_ID + 7); | ||
public static final short DOUBLE_CLASS_ID = (short) (VOID_CLASS_ID + 8); | ||
public static final short STRING_CLASS_ID = (short) (VOID_CLASS_ID + 9); | ||
public static final short PRIMITIVE_BOOLEAN_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 1); | ||
public static final short PRIMITIVE_BYTE_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 2); | ||
public static final short PRIMITIVE_CHAR_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 3); | ||
public static final short PRIMITIVE_SHORT_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 4); | ||
public static final short PRIMITIVE_INT_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 5); | ||
public static final short PRIMITIVE_FLOAT_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 6); | ||
public static final short PRIMITIVE_LONG_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 7); | ||
public static final short PRIMITIVE_DOUBLE_ARRAY_CLASS_ID = (short) (STRING_CLASS_ID + 8); | ||
public static final short STRING_ARRAY_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 1); | ||
public static final short OBJECT_ARRAY_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 2); | ||
public static final short ARRAYLIST_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 3); | ||
public static final short HASHMAP_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 4); | ||
public static final short HASHSET_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 5); | ||
public static final short CLASS_CLASS_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 6); | ||
public static final short EMPTY_OBJECT_ID = (short) (PRIMITIVE_DOUBLE_ARRAY_CLASS_ID + 7); | ||
} | ||
|
||
// class id of last default registered class. | ||
private short innerEndClassId; | ||
// Here we set it to 1 because `NO_CLASS_ID` is 0 to avoid calculating it again in | ||
// `register(Class<?> cls)`. | ||
private short classIdGenerator = 1; | ||
|
||
private final Function<Class<?>, Boolean> classRegisteredFactory; | ||
|
||
private final Function<Short, Boolean> classIdRegisteredFactory; | ||
|
||
public ClassIdAllocator( | ||
Function<Class<?>, Boolean> classRegisteredFactory, | ||
Function<Short, Boolean> classIdRegisteredFactory) { | ||
Preconditions.checkNotNull(classRegisteredFactory); | ||
Preconditions.checkNotNull(classIdRegisteredFactory); | ||
this.classRegisteredFactory = classRegisteredFactory; | ||
this.classIdRegisteredFactory = classIdRegisteredFactory; | ||
} | ||
|
||
public short allocateClassId(Class<?> cls) { | ||
if (!classRegisteredFactory.apply(cls)) { | ||
while (classIdRegisteredFactory.apply(classIdGenerator)) { | ||
classIdGenerator++; | ||
} | ||
} | ||
return classIdGenerator; | ||
} | ||
|
||
public void notifyRegistrationEnd() { | ||
classIdGenerator++; | ||
} | ||
|
||
public void markInternalRegistrationEnd() { | ||
innerEndClassId = classIdGenerator; | ||
} | ||
|
||
public boolean isInnerClass(Short classId) { | ||
return classId != null && classId != BuiltinClassId.NO_CLASS_ID && classId < innerEndClassId; | ||
} | ||
|
||
public boolean isPrimitive(short classId) { | ||
return classId >= BuiltinClassId.PRIMITIVE_VOID_CLASS_ID | ||
&& classId <= BuiltinClassId.PRIMITIVE_DOUBLE_CLASS_ID; | ||
} | ||
} |
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.