diff --git a/app/src/main/java/com/zeoflow/test/MainActivity.java b/app/src/main/java/com/zeoflow/test/MainActivity.java index 9d58173..3099073 100644 --- a/app/src/main/java/com/zeoflow/test/MainActivity.java +++ b/app/src/main/java/com/zeoflow/test/MainActivity.java @@ -19,7 +19,14 @@ import android.os.Bundle; import com.zeoflow.app.Activity; +import com.zeoflow.jx.file.JavaFile; +import com.zeoflow.jx.file.MethodSpec; import com.zeoflow.jx.file.TypeName; +import com.zeoflow.jx.file.TypeSpec; + +import java.io.IOException; + +import javax.lang.model.element.Modifier; public class MainActivity extends Activity { @@ -39,6 +46,39 @@ protected void onCreate(Bundle savedInstanceState) log("" + TypeName.get(packageEx).assemble("java.lang.String").assemble(String.class).toString()); log("" + TypeName.get(String.class).assemble(Activity.class, true).toString()); log("" + TypeName.get(String.class).assemble(Activity.class, false).toString()); + + MethodSpec main = MethodSpec.methodBuilder("main") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(void.class) + .addParameter(String[].class, "args") + .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") + .addStatement("String name") + .build(); + + MethodSpec main2 = MethodSpec.methodBuilder("getObs") + .addModifiers(Modifier.PUBLIC) + .returns(void.class) + .addParameter(TypeName.get("com.Observable"), "args") + .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") + .addStatement("String name") + .build(); + + TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld") + .addModifiers(Modifier.PUBLIC, Modifier.FINAL) + .addMethod(main) + .addMethod(main2) + .build(); + + JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld) + .build(); + + try + { + javaFile.writeTo(System.out); + } catch (IOException e) + { + e.printStackTrace(); + } } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 3dd66ad..f060a6d 100644 --- a/build.gradle +++ b/build.gradle @@ -36,8 +36,8 @@ ext { buildToolsVersion = '30.0.1' minSdkVersion = 21 targetSdkVersion = 30 - versionCode = 4 - versionName = "1.1.2" + versionCode = 5 + versionName = "1.2.0" androidx = [ appcompat: 'androidx.appcompat:appcompat:1.0.0', diff --git a/buildSrc/.gitignore b/buildSrc/.gitignore new file mode 100644 index 0000000..84b30df --- /dev/null +++ b/buildSrc/.gitignore @@ -0,0 +1,5 @@ +# SIGNING KEYS +*.gpg + +# Key Credentials +gradle.properties \ No newline at end of file diff --git a/jx/gradle.properties b/jx/gradle.properties index 398f16c..a4365ee 100644 --- a/jx/gradle.properties +++ b/jx/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.1.1 +VERSION_NAME=1.2.1 GROUP=com.zeoflow POM_DESCRIPTION=JavaX improved library - JDK-8 @@ -11,7 +11,4 @@ POM_DEVELOPER_NAME=ZeoFlow POM_DEVELOPER_EMAIL=open-source@zeoflow.com POM_NAME=JX POM_PACKAGING=jar -POM_ARTIFACT_ID=jx - -NEXUS_USERNAME= -NEXUS_PASSWORD= \ No newline at end of file +POM_ARTIFACT_ID=jx \ No newline at end of file diff --git a/jx/src/main/java/com/zeoflow/jx/file/ClassBean.java b/jx/src/main/java/com/zeoflow/jx/file/ClassBean.java index 9cc7708..225d459 100644 --- a/jx/src/main/java/com/zeoflow/jx/file/ClassBean.java +++ b/jx/src/main/java/com/zeoflow/jx/file/ClassBean.java @@ -8,8 +8,15 @@ public class ClassBean public ClassBean(String classData) { - this.classPackage = classData.substring(0, classData.lastIndexOf(".")); - this.className = classData.substring(classData.lastIndexOf(".") + 1); + int lastIndex = classData.lastIndexOf("."); + if (lastIndex != -1) + { + this.classPackage = classData.substring(0, lastIndex); + this.className = classData.substring(lastIndex + 1); + } else { + this.classPackage = classData; + this.className = classData; + } } public ClassBean(String classPackage, String className) diff --git a/jx/src/main/java/com/zeoflow/jx/file/JavaFile.java b/jx/src/main/java/com/zeoflow/jx/file/JavaFile.java index b4fb4dc..b18ae56 100644 --- a/jx/src/main/java/com/zeoflow/jx/file/JavaFile.java +++ b/jx/src/main/java/com/zeoflow/jx/file/JavaFile.java @@ -16,12 +16,6 @@ package com.zeoflow.jx.file; -import javax.annotation.processing.Filer; -import javax.lang.model.element.Element; -import javax.tools.JavaFileObject; -import javax.tools.JavaFileObject.Kind; -import javax.tools.SimpleJavaFileObject; - import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -34,13 +28,18 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import javax.annotation.processing.Filer; +import javax.lang.model.element.Element; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.SimpleJavaFileObject; + import static com.zeoflow.jx.file.Util.checkArgument; import static com.zeoflow.jx.file.Util.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; @@ -120,8 +119,13 @@ public void writeTo(Appendable out) throws IOException Map suggestedImports = importsCollector.suggestedImports(); // Second pass: write the code, taking advantage of the imports. - CodeWriter codeWriter - = new CodeWriter(out, indent, suggestedImports, staticImports, alwaysQualify); + CodeWriter codeWriter = new CodeWriter( + out, + indent, + suggestedImports, + staticImports, + alwaysQualify + ); emit(codeWriter); } /** diff --git a/jx/src/main/java/com/zeoflow/jx/file/MethodSpec.java b/jx/src/main/java/com/zeoflow/jx/file/MethodSpec.java index c410d4f..d4a67b8 100644 --- a/jx/src/main/java/com/zeoflow/jx/file/MethodSpec.java +++ b/jx/src/main/java/com/zeoflow/jx/file/MethodSpec.java @@ -197,6 +197,22 @@ void emit(CodeWriter codeWriter, String enclosingName, Set implicitMod codeWriter.emit(" "); } +// TODO indexes of <> +// codeWriter.emit("<"); +// for (Iterator i = parameters.iterator(); i.hasNext(); ) +// { +// ParameterSpec parameter = i.next(); +// codeWriter.emit(parameter.name); +// } +// codeWriter.emit("> "); +// int start = name.indexOf("<"); +// int end = name.lastIndexOf(">") + 1; +// if (start != -1) +// { +// this.typeArguments = name.substring(start, end); +// name = name.substring(0, start) + name.substring(end); +// } + if (isConstructor()) { codeWriter.emit("$L($Z", enclosingName); diff --git a/jx/src/main/java/com/zeoflow/jx/file/TypeName.java b/jx/src/main/java/com/zeoflow/jx/file/TypeName.java index c587459..37c6801 100644 --- a/jx/src/main/java/com/zeoflow/jx/file/TypeName.java +++ b/jx/src/main/java/com/zeoflow/jx/file/TypeName.java @@ -119,9 +119,14 @@ public static TypeName get(String raw) { if (!raw.contains("<")) { - String classPackage = raw.substring(0, raw.lastIndexOf(".")); - String className = raw.substring(raw.lastIndexOf(".") + 1); - return ClassName.get(classPackage, className); + int lastIndex = raw.lastIndexOf("."); + if(lastIndex != -1) + { + String classPackage = raw.substring(0, raw.lastIndexOf(".")); + String className = raw.substring(raw.lastIndexOf(".") + 1); + return ClassName.get(classPackage, className); + } + return null; } List packages = new ArrayList<>(); while(raw.contains("<")) diff --git a/jx/src/main/java/com/zeoflow/jx/file/TypeSpec.java b/jx/src/main/java/com/zeoflow/jx/file/TypeSpec.java index b26cff7..5bdd12c 100644 --- a/jx/src/main/java/com/zeoflow/jx/file/TypeSpec.java +++ b/jx/src/main/java/com/zeoflow/jx/file/TypeSpec.java @@ -70,6 +70,7 @@ public final class TypeSpec public final List originatingElements; public final Set alwaysQualifiedNames; final Set nestedTypesSimpleNames; + public final String typeArguments; private TypeSpec(Builder builder) { @@ -89,6 +90,7 @@ private TypeSpec(Builder builder) this.methodSpecs = Util.immutableList(builder.methodSpecs); this.typeSpecs = Util.immutableList(builder.typeSpecs); this.alwaysQualifiedNames = Util.immutableSet(builder.alwaysQualifiedNames); + this.typeArguments = builder.typeArguments; nestedTypesSimpleNames = new HashSet<>(builder.typeSpecs.size()); List originatingElementsMutable = new ArrayList<>(); @@ -127,6 +129,7 @@ private TypeSpec(TypeSpec type) this.originatingElements = Collections.emptyList(); this.nestedTypesSimpleNames = Collections.emptySet(); this.alwaysQualifiedNames = Collections.emptySet(); + this.typeArguments = ""; } public static Builder classBuilder(String name) { @@ -234,10 +237,10 @@ void emit(CodeWriter codeWriter, String enumName, Set implicitModifier codeWriter.emitModifiers(modifiers, Util.union(implicitModifiers, kind.asMemberModifiers)); if (kind == Kind.ANNOTATION) { - codeWriter.emit("$L $L", "@interface", name); + codeWriter.emit("$L $L", "@interface", name + typeArguments); } else { - codeWriter.emit("$L $L", kind.name().toLowerCase(Locale.US), name); + codeWriter.emit("$L $L", kind.name().toLowerCase(Locale.US), name + typeArguments); } codeWriter.emitTypeVariables(typeVariables); @@ -469,6 +472,7 @@ public static final class Builder public final Set alwaysQualifiedNames = new LinkedHashSet<>(); private final Kind kind; private final String name; + private String typeArguments = ""; private final CodeBlock anonymousTypeArguments; private final CodeBlock.Builder javadoc = CodeBlock.builder(); private final CodeBlock.Builder staticBlock = CodeBlock.builder(); @@ -478,7 +482,18 @@ public static final class Builder private Builder(Kind kind, String name, CodeBlock anonymousTypeArguments) { - checkArgument(name == null || SourceVersion.isName(name), "not a valid name: %s", name); + if (name == null) + { + throw new IllegalArgumentException(String.format("not a valid name: %s", name)); + } + int start = name.indexOf("<"); + int end = name.lastIndexOf(">") + 1; + if (start != -1) + { + this.typeArguments = name.substring(start, end); + name = name.substring(0, start) + name.substring(end); + } + checkArgument(SourceVersion.isName(name), "not a valid name: %s", name); this.kind = kind; this.name = name; this.anonymousTypeArguments = anonymousTypeArguments;