Skip to content

Commit

Permalink
Merge pull request #17 from zeoflow/extends-type
Browse files Browse the repository at this point in the history
Fixed bug where classes can not have parameters
  • Loading branch information
teogor authored Apr 15, 2021
2 parents dcfba52 + b2f182d commit 5e94854
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 24 deletions.
40 changes: 40 additions & 0 deletions app/src/main/java/com/zeoflow/test/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<T> name")
.build();

MethodSpec main2 = MethodSpec.methodBuilder("getObs")
.addModifiers(Modifier.PUBLIC)
.returns(void.class)
.addParameter(TypeName.get("com.Observable<T>"), "args")
.addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
.addStatement("String<T> name")
.build();

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld<Text, View>")
.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();
}
}

}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SIGNING KEYS
*.gpg

# Key Credentials
gradle.properties
7 changes: 2 additions & 5 deletions jx/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.1.1
VERSION_NAME=1.2.1
GROUP=com.zeoflow

POM_DESCRIPTION=JavaX improved library - JDK-8
Expand All @@ -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=
POM_ARTIFACT_ID=jx
11 changes: 9 additions & 2 deletions jx/src/main/java/com/zeoflow/jx/file/ClassBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions jx/src/main/java/com/zeoflow/jx/file/JavaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -120,8 +119,13 @@ public void writeTo(Appendable out) throws IOException
Map<String, ClassName> 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);
}
/**
Expand Down
16 changes: 16 additions & 0 deletions jx/src/main/java/com/zeoflow/jx/file/MethodSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.emit(" ");
}

// TODO indexes of <>
// codeWriter.emit("<");
// for (Iterator<ParameterSpec> 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);
Expand Down
11 changes: 8 additions & 3 deletions jx/src/main/java/com/zeoflow/jx/file/TypeName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> packages = new ArrayList<>();
while(raw.contains("<"))
Expand Down
21 changes: 18 additions & 3 deletions jx/src/main/java/com/zeoflow/jx/file/TypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class TypeSpec
public final List<Element> originatingElements;
public final Set<String> alwaysQualifiedNames;
final Set<String> nestedTypesSimpleNames;
public final String typeArguments;

private TypeSpec(Builder builder)
{
Expand All @@ -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<Element> originatingElementsMutable = new ArrayList<>();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -234,10 +237,10 @@ void emit(CodeWriter codeWriter, String enumName, Set<Modifier> 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);

Expand Down Expand Up @@ -469,6 +472,7 @@ public static final class Builder
public final Set<String> 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();
Expand All @@ -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;
Expand Down

0 comments on commit 5e94854

Please sign in to comment.