Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lx01.platform.reflect;
- public final class LX01Class {
- private final DataTree dataTree;
- private final String name;
- private final String[] paths;
- LX01Class (byte[] data, String fullName) {
- this(parseDataTree(data), fullName.substring(0, fullName.lastIndexOf(".")).split("."), fullName.substring(fullName.lastIndexOf(".") + 1));
- }
- LX01Class (DataTree tree, String fullName) {
- this(tree, fullName.substring(0, fullName.lastIndexOf(".")).split("."), fullName.substring(fullName.lastIndexOf(".") + 1));
- }
- LX01Class (byte[] data, String[] path, String lastName) {
- this(parseDataTree(data), path, lastName);
- }
- LX01Class (DataTree tree, String[] path, String lastName) {
- this.dataTree = tree;
- this.name = lastName;
- this.paths = path;
- String[] allPaths = new String[this.paths.length + 1];
- System.arraycopy(this.paths, 0, allPaths, 0, this.paths.length);
- allPaths[this.paths.length] = this.name;
- LX01Classes.register(allPaths);
- }
- public static LX01Class get (String name) throws ClassNotFoundException {
- return get(name.split("."));
- }
- public static LX01Class get (String[] path) throws ClassNotFoundException {
- LX01Class lc = Classes.get(path);
- if (lc == null) throw new ClassNotFoundException("Path does not point to a qualified LX01 class");
- return lc;
- }
- private static DataTree parseDataTree (byte[] data) {
- return DataTree.parse(data);
- }
- public List<Function> getFunctions (String name) throws FunctionNotFoundException {
- List<Function> funcs = List.<Function>of();
- for (Function f : this.dataTree.getFunctions()) if (f.getName().contains(name)) funcs.add(f);
- if (funcs.size() == 0) throw new FunctionNotFoundException("Could not get specified function");
- return funcs;
- }
- public Function getFunction (String name) throws FunctionNotFoundException {
- for (Function f : this.dataTree.getFunctions()) if (f.getName().equals(name)) return f;
- throw new FunctionNotFoundException("Could not get specified function");
- }
- public List<Variable> getVariables (String name) throws VariableNotFoundException {
- List<Variable> vars = List.<Variable>of();
- for (Variable v : this.dataTree.getVariables()) if (v.getName().contains(name)) vars.add(v);
- if (vars.size() == 0) throw new VariableNotFoundException("Could not get specified variable");
- return vars;
- }
- public Variable getVariable (String name) throws VariableNotFoundException {
- for (Variable v : this.dataTree.getVariables()) if (v.getName().equals(name)) return v;
- throw new VariableNotFoundException("Could not get specified variable");
- }
- public List<LX01Class> getClasses (String name) throws ClassNotFoundException {
- List<LX01Class> cls = List.<LX01Class>of();
- for (LX01Class c : this.dataTree.getClasses()) if (c.getName().contains(name)) cls.add(c);
- if (cls.size() == 0) throw new ClassNotFoundException("Could not get specified class");
- return cls;
- }
- public LX01Class getClass (String name) throws ClassNotFoundException {
- for (LX01Class c : this.dataTree.getClasses()) if (c.getName().equals(name)) return c;
- throw new ClassNotFoundException("Could not get specified class");
- }
- }
- class DataTree {
- private boolean initialized = false;
- private Map<Class<?>, ?> types = Map.<Class<?>, ?>of();
- private List<String> properties = List.<String>of();
- private DataTree () {}
- public void add (Object obj) {
- if (!validType(obj) || this.initialized) return;
- if (obj instanceof Function f)
- this.add(Function.class, f);
- else if (obj instanceof Variable v)
- this.add(Variable.class, v);
- else if (obj instanceof DataTree c)
- this.add(LX01Class.class, c);
- }
- private <T> void add (Class<T> clazz, T t) {
- this.types.put(clazz, t);
- }
- private boolean validType (Object obj) {
- return obj instanceof Function || obj instanceof Variable || obj instanceof LX01Class;
- }
- static DataTree parse (byte[] data) {
- byte[] actualData = new byte[data.length - 8];
- System.arraycopy(data, 8, actualData, 0, actualData.length);
- DataTree tree = new DataTree();
- boolean loop = true;
- while (loop) {
- int type = actualData[0];
- switch (type) {
- case 0:
- short length = ((short)actualData[1]) << 8 | actualData[2];
- byte[] classData = new byte[length];
- System.arraycopy(actualData, 3, classData, 0, length);
- tree.add(LX01Class.class, parseClass(classData));
- byte[] data2 = new byte[actualData.length - length - 3];
- System.arraycopy(actualData, 3, data2, 0, data2.length);
- System.arraycopy(data2, 0, actualData, 0, data2.length);
- break;
- case 1:
- short length = ((short)actualData[1]) << 8 | actualData[2];
- byte[] functionData = new byte[length];
- System.arraycopy(actualData, 3, functionData, 0, length);
- tree.add(Function.class, parseFunction(functionData));
- byte[] data2 = new byte[actualData.length - length - 3];
- System.arraycopy(actualData, 3, data2, 0, data2.length);
- System.arraycopy(data2, 0, actualData, 0, data2.length);
- break;
- case 2:
- short length = ((short)actualData[1]) << 8 | actualData[2];
- byte[] variableData = new byte[length];
- System.arraycopy(actualData, 3, variableData, 0, length);
- tree.add(Variable.class, parseVariable(variableData));
- byte[] data2 = new byte[actualData.length - length - 3];
- System.arraycopy(actualData, 3, data2, 0, data2.length);
- System.arraycopy(data2, 0, actualData, 0, data2.length);
- break;
- case 3:
- short length = ((short)actualData[1]) << 8 | actualData[2];
- byte[] stringData = new byte[length];
- System.arraycopy(actualData, 3, stringData, 0, length);
- tree.properties.add(new String(stringData));
- byte[] data2 = new byte[actualData.length - length - 3];
- System.arraycopy(actualData, 3, data2, 0, data2.length);
- System.arraycopy(data2, 0, actualData, 0, data2.length);
- break;
- default:
- throw new IllegalTypeException("Unknown byte: 0x" + Integer.toHexString(type).substring(6));
- }
- }
- }
- private static Function parseFunction (byte[] data) {
- return Function.parse(data);
- }
- private static Variable parseVariable (byte[] data) {
- return Variable.parse(data);
- }
- private LX01Class parseClass (byte[] data) {
- short length = ((short)data[1]) << 8 | data[2];
- byte[] name = new byte[length];
- System.arraycopy(data, 2, name, 0, length);
- String nameString = new String(name);
- byte[] split = new byte[data.length - 2 - length];
- System.arraycopy(data, 2 + length, split, 0, split.length);
- return new LX01Class(split, nameString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement