-
Notifications
You must be signed in to change notification settings - Fork 124
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
#806 Support custom ClassLookup in WireTypeConverter #807
Draft
maxim-ponomarev
wants to merge
3
commits into
develop
Choose a base branch
from
#806-Support-custom-ClassLookup-in-WireTypeConverter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 14 additions & 4 deletions
18
src/main/java/net/openhft/chronicle/wire/WireTypeConverter.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 |
---|---|---|
@@ -1,27 +1,37 @@ | ||
package net.openhft.chronicle.wire; | ||
|
||
import net.openhft.chronicle.core.pool.ClassLookup; | ||
import net.openhft.chronicle.wire.internal.WireTypeConverterInternal; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class WireTypeConverter { | ||
private final WireTypeConverterInternal delegate; | ||
|
||
public WireTypeConverter(Validate validate) { | ||
public WireTypeConverter(@NotNull Validate validate, @NotNull ClassLookup classLookup) { | ||
delegate = new WireTypeConverterInternal(validate, classLookup); | ||
} | ||
|
||
public WireTypeConverter(@NotNull ClassLookup classLookup) { | ||
delegate = new WireTypeConverterInternal(classLookup); | ||
} | ||
|
||
public WireTypeConverter(@NotNull Validate validate) { | ||
delegate = new WireTypeConverterInternal(validate); | ||
} | ||
|
||
public WireTypeConverter() { | ||
delegate = new WireTypeConverterInternal(); | ||
} | ||
|
||
public CharSequence jsonToYaml(CharSequence json) throws Exception { | ||
public CharSequence jsonToYaml(CharSequence json) { | ||
return delegate.jsonToYaml(json); | ||
} | ||
|
||
public CharSequence yamlToJson(CharSequence yaml) throws Exception { | ||
public CharSequence yamlToJson(CharSequence yaml) { | ||
return delegate.yamlToJson(yaml); | ||
} | ||
|
||
public void addAlias(Class newClass, String oldTypeName) { | ||
public void addAlias(Class<?> newClass, String oldTypeName) { | ||
delegate.addAlias(newClass, oldTypeName); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/net/openhft/chronicle/wire/internal/UnknownClassBase.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,25 @@ | ||
package net.openhft.chronicle.wire.internal; | ||
|
||
import net.openhft.chronicle.core.io.IORuntimeException; | ||
import net.openhft.chronicle.core.io.InvalidMarshallableException; | ||
import net.openhft.chronicle.wire.SelfDescribingMarshallable; | ||
import net.openhft.chronicle.wire.WireIn; | ||
import net.openhft.chronicle.wire.WireOut; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public abstract class UnknownClassBase extends SelfDescribingMarshallable { | ||
private Map<Object, Object> map = new LinkedHashMap<>(); | ||
|
||
@Override | ||
public void readMarshallable(@NotNull WireIn wire) throws IORuntimeException, InvalidMarshallableException { | ||
map = wire.readAllAsMap(Object.class, Object.class, new LinkedHashMap<>()); | ||
} | ||
|
||
@Override | ||
public void writeMarshallable(@NotNull WireOut wire) throws InvalidMarshallableException { | ||
wire.writeAllAsMap(Object.class, Object.class, map); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/net/openhft/chronicle/wire/internal/UnknownClassLookup.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,55 @@ | ||
package net.openhft.chronicle.wire.internal; | ||
|
||
import net.openhft.chronicle.core.Jvm; | ||
import net.openhft.chronicle.core.OS; | ||
import net.openhft.chronicle.core.pool.ClassLookup; | ||
import net.openhft.chronicle.core.util.ClassNotFoundRuntimeException; | ||
import net.openhft.compiler.CachedCompiler; | ||
|
||
import java.io.File; | ||
|
||
public class UnknownClassLookup implements ClassLookup { | ||
public static final CachedCompiler CACHED_COMPILER = | ||
new CachedCompiler(Jvm.isDebug() ? new File(OS.getTarget(), "generated-test-sources") : null, null); | ||
|
||
private final ClassLookup delegate; | ||
|
||
public UnknownClassLookup(ClassLookup delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public Class<?> forName(CharSequence name) throws ClassNotFoundRuntimeException { | ||
try { | ||
return delegate.forName(name); | ||
} catch (Exception e) { | ||
String[] parts = name.toString().split("\\."); | ||
String className = parts[parts.length - 1]; | ||
Class<?> unknownClass; | ||
try { | ||
unknownClass = CACHED_COMPILER.loadFromJava(className, | ||
"public class " + className + " extends " + UnknownClassBase.class.getName() + "{}"); | ||
} catch (ClassNotFoundException ex) { | ||
throw new ClassNotFoundRuntimeException(ex); | ||
} | ||
|
||
addAlias(unknownClass, className); | ||
return delegate.forName(name); | ||
} | ||
} | ||
|
||
@Override | ||
public String nameFor(Class<?> clazz) throws IllegalArgumentException { | ||
return delegate.nameFor(clazz); | ||
} | ||
|
||
@Override | ||
public void addAlias(Class<?>... classes) { | ||
delegate.addAlias(classes); | ||
} | ||
|
||
@Override | ||
public void addAlias(Class<?> clazz, String names) { | ||
delegate.addAlias(clazz, names); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class should be moved to test sources or target has to be adjusted respectively.