diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/Ts2JavaX.java b/src/main/java/com/caoccao/javet/buddy/ts2java/Ts2JavaX.java deleted file mode 100644 index eb1c1ec..0000000 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/Ts2JavaX.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2024. caoccao.com Sam Cao - * - * Licensed 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 com.caoccao.javet.buddy.ts2java; - -import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaAstClassDecl; -import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaException; -import com.caoccao.javet.swc4j.Swc4j; -import com.caoccao.javet.swc4j.ast.program.Swc4jAstModule; -import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstClassDecl; -import com.caoccao.javet.swc4j.enums.Swc4jMediaType; -import com.caoccao.javet.swc4j.enums.Swc4jParseMode; -import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; -import com.caoccao.javet.swc4j.options.Swc4jParseOptions; -import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput; -import net.bytebuddy.ByteBuddy; -import net.bytebuddy.dynamic.DynamicType; -import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -public class Ts2JavaX { - protected final static Swc4j swc4j = new Swc4j(); - protected final static Swc4jParseOptions swc4jParseOptions = new Swc4jParseOptions() - .setMediaType(Swc4jMediaType.TypeScript) - .setParseMode(Swc4jParseMode.Module) - .setCaptureAst(true); - protected final String packageName; - protected final String tsCode; - protected List> classes; - - public Ts2JavaX(String packageName, String tsCode) { - classes = new ArrayList<>(); - this.packageName = packageName; - this.tsCode = Objects.requireNonNull(tsCode); - } - - public List> getClasses() { - return classes; - } - - public String getPackageName() { - return packageName; - } - - public String getTsCode() { - return tsCode; - } - - public void transpile() throws Swc4jCoreException { - classes.clear(); - Swc4jParseOutput output = swc4j.parse(getTsCode(), swc4jParseOptions); - Swc4jAstModule module = (Swc4jAstModule) output.getProgram(); - if (module == null) { - throw new Ts2JavaException("The TypeScript code must be a module, not a script."); - } - if (module.getBody().isEmpty()) { - throw new Ts2JavaException("The TypeScript code must contain at least one statement."); - } - List classDecls = module.getBody().stream() - .filter(ast -> ast instanceof Swc4jAstClassDecl) - .map(ast -> (Swc4jAstClassDecl) ast) - .collect(Collectors.toList()); - if (classDecls.isEmpty()) { - throw new Ts2JavaException("There must be at least one class declaration in the TypeScript code."); - } - for (Swc4jAstClassDecl classDecl : classDecls) { - DynamicType.Builder builder = new ByteBuddy() - .subclass(Object.class, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR); - builder = new Ts2JavaAstClassDecl(getPackageName()).transpile(builder, classDecl); - try (DynamicType.Unloaded unloadedType = builder.make()) { - classes.add(unloadedType.load(getClass().getClassLoader()).getLoaded()); - } - } - } -} diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstClassFunction.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstClassFunction.java index f03d447..85c4ffb 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstClassFunction.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstClassFunction.java @@ -16,6 +16,7 @@ package com.caoccao.javet.buddy.ts2java.ast; +import com.caoccao.javet.buddy.ts2java.ast.enums.Ts2JavaAstAccessibility; import com.caoccao.javet.buddy.ts2java.compiler.JavaFunctionContext; import com.caoccao.javet.buddy.ts2java.compiler.visitors.JavaLoggingMethodVisitor; import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstFunction; diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/clazz/Ts2JavaAstFunction.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/clazz/Ts2JavaAstFunction.java index 9934461..563d9ec 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/clazz/Ts2JavaAstFunction.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/clazz/Ts2JavaAstFunction.java @@ -17,7 +17,7 @@ package com.caoccao.javet.buddy.ts2java.ast.clazz; import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst; -import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaAstAccessibility; +import com.caoccao.javet.buddy.ts2java.ast.enums.Ts2JavaAstAccessibility; import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaAstParam; import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaAstTsTypeAnn; import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst; diff --git a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstAccessibility.java b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/enums/Ts2JavaAstAccessibility.java similarity index 89% rename from src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstAccessibility.java rename to src/main/java/com/caoccao/javet/buddy/ts2java/ast/enums/Ts2JavaAstAccessibility.java index 80cc3f5..c55bc7d 100644 --- a/src/main/java/com/caoccao/javet/buddy/ts2java/ast/Ts2JavaAstAccessibility.java +++ b/src/main/java/com/caoccao/javet/buddy/ts2java/ast/enums/Ts2JavaAstAccessibility.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024. caoccao.com Sam Cao + * Copyright (c) 2024-2024. caoccao.com Sam Cao * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.caoccao.javet.buddy.ts2java.ast; +package com.caoccao.javet.buddy.ts2java.ast.enums; import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility; import net.bytebuddy.description.modifier.Visibility; diff --git a/src/test/java/com/caoccao/javet/buddy/ts2java/TsClassX.java b/src/test/java/com/caoccao/javet/buddy/ts2java/TsClassX.java deleted file mode 100644 index 8050904..0000000 --- a/src/test/java/com/caoccao/javet/buddy/ts2java/TsClassX.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2024. caoccao.com Sam Cao - * - * Licensed 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 com.caoccao.javet.buddy.ts2java; - -import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; -import com.caoccao.javet.swc4j.utils.SimpleList; -import com.caoccao.javet.utils.SimpleFreeMarkerFormat; -import com.caoccao.javet.utils.SimpleMap; - -import java.lang.reflect.Method; -import java.lang.reflect.Parameter; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static org.junit.jupiter.api.Assertions.*; - -public final class TsClassX { - private final List arguments; - private final String body; - private final Class returnType; - private Object instance; - private Class testClass; - private Method testMethod; - - public TsClassX(String body, Class returnType, TsMethodArgument... arguments) { - assertNotNull(body); - assertNotNull(returnType); - assertNotNull(arguments); - this.arguments = SimpleList.immutableOf(arguments); - this.body = body; - this.returnType = returnType; - instance = null; - testClass = null; - testMethod = null; - init(); - } - - public List getArguments() { - return arguments; - } - - public String getBody() { - return body; - } - - public Object getInstance() throws Exception { - if (instance == null) { - instance = testClass.getConstructor().newInstance(); - } - return instance; - } - - public Class getReturnType() { - return returnType; - } - - public Class getTestClass() { - return testClass; - } - - public Method getTestMethod() { - return testMethod; - } - - private void init() { - String argumentsString = arguments.stream() - .map(TsMethodArgument::toString) - .collect(Collectors.joining(", ")); - String codeString = SimpleFreeMarkerFormat.format( - "class Test {\n" + - " public test(${argumentsString}): ${returnType} {\n" + - " ${body}\n" + - " }\n" + - "}\n", - SimpleMap.of( - "argumentsString", argumentsString, - "returnType", returnType.getName(), - "body", body)); - Ts2JavaX ts2Java = new Ts2JavaX("com.test", codeString); - try { - ts2Java.transpile(); - } catch (Swc4jCoreException e) { - fail(e); - } - List> classes = ts2Java.getClasses(); - assertEquals(1, classes.size()); - testClass = classes.get(0); - assertEquals("Test", testClass.getSimpleName()); - assertEquals("com.test.Test", testClass.getName()); - try { - testMethod = testClass.getMethod( - "test", - arguments.stream().map(TsMethodArgument::getType).toArray(Class[]::new)); - assertEquals(returnType, testMethod.getReturnType()); - final int argumentsLength = arguments.size(); - final Parameter[] parameters = testMethod.getParameters(); - assertEquals(argumentsLength, testMethod.getParameterCount()); - assertEquals(argumentsLength, parameters.length); - IntStream.range(0, argumentsLength) - .forEach(i -> assertEquals( - arguments.get(i).getType(), - parameters[i].getType(), - "Argument[" + i + "] type mismatch")); - } catch (NoSuchMethodException e) { - fail(e); - } - } - - public Object invoke(Object... arguments) throws Exception { - return testMethod.invoke(getInstance(), arguments); - } -}