Skip to content

Commit

Permalink
Refactor the SignatureBuilder API for better encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Jan 9, 2023
1 parent 8787f16 commit 634a4b6
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 277 deletions.
15 changes: 6 additions & 9 deletions src/main/java/io/quarkus/gizmo/ClassSignatureBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import java.util.ArrayList;
import java.util.List;

import org.jboss.jandex.DotName;

import io.quarkus.gizmo.SignatureBuilder.ClassSignatureBuilder;
import io.quarkus.gizmo.Type.ClassType;
import io.quarkus.gizmo.Type.ParameterizedType;
import io.quarkus.gizmo.Type.TypeVariable;

class ClassSignatureBuilderImpl implements ClassSignatureBuilder {

private Type superClass = Type.classType(DotName.OBJECT_NAME);
private List<TypeVariable> typeParameters = new ArrayList<>();
private Type superClass = ClassType.OBJECT;
private List<Type> superInterfaces = new ArrayList<>();

@Override
Expand All @@ -30,12 +27,12 @@ public String build() {
}

// superclass
signature.append(superClass.toSignature());
superClass.appendToSignature(signature);

// interfaces
if (!superInterfaces.isEmpty()) {
for (Type superInterface : superInterfaces) {
signature.append(superInterface.toSignature());
superInterface.appendToSignature(signature);
}
}
return signature.toString();
Expand All @@ -58,6 +55,7 @@ public ClassSignatureBuilder setSuperClass(ParameterizedType superClass) {
if (containsWildcard(superClass)) {
throw new IllegalArgumentException("A super type may not specify a wilcard");
}

this.superClass = superClass;
return this;
}
Expand All @@ -81,13 +79,12 @@ private boolean containsWildcard(Type type) {
if (type.isWildcard()) {
return true;
} else if (type.isParameterizedType()) {
for (Type typeArgument : type.asParameterizedType().typeArguments) {
for (Type typeArgument : type.asParameterizedType().getTypeArguments()) {
if (containsWildcard(typeArgument)) {
return true;
}
}
}
return false;
}

}
}
8 changes: 4 additions & 4 deletions src/main/java/io/quarkus/gizmo/FieldSignatureBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import io.quarkus.gizmo.SignatureBuilder.FieldSignatureBuilder;

class FieldSignatureBuilderImpl implements FieldSignatureBuilder {

private Type type;

@Override
public String build() {
return type.toSignature();
StringBuilder signature = new StringBuilder();
type.appendToSignature(signature);
return signature.toString();
}

@Override
public FieldSignatureBuilder setType(Type type) {
this.type = Objects.requireNonNull(type);
return this;
}

}
}
23 changes: 12 additions & 11 deletions src/main/java/io/quarkus/gizmo/MethodSignatureBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import io.quarkus.gizmo.SignatureBuilder.MethodSignatureBuilder;
import io.quarkus.gizmo.Type.ClassType;
import io.quarkus.gizmo.Type.TypeVariable;
import io.quarkus.gizmo.Type.VoidType;

class MethodSignatureBuilderImpl implements MethodSignatureBuilder {

private Type returnType = Type.voidType();
private List<Type> parameterTypes = new ArrayList<>();
private List<TypeVariable> typeParameters = new ArrayList<>();
private Type returnType = VoidType.INSTANCE;
private List<Type> parameterTypes = new ArrayList<>();
private List<Type> exceptions = new ArrayList<>();

@Override
Expand All @@ -28,22 +28,24 @@ public String build() {
signature.append('>');
}

// params
// param types
signature.append('(');
for (Type parameterType : parameterTypes) {
signature.append(parameterType.toSignature());
parameterType.appendToSignature(signature);
}
signature.append(')');

// return type
signature.append(returnType.toSignature());
returnType.appendToSignature(signature);

// exceptions
// exception types
if (!exceptions.isEmpty()) {
for (Type exceptionType : exceptions) {
signature.append('^').append(exceptionType.toSignature());
signature.append('^');
exceptionType.appendToSignature(signature);
}
}

return signature.toString();
}

Expand All @@ -60,7 +62,7 @@ public MethodSignatureBuilder setReturnType(Type returnType) {
}

@Override
public MethodSignatureBuilder addParameter(Type parameterType) {
public MethodSignatureBuilder addParameterType(Type parameterType) {
this.parameterTypes.add(Objects.requireNonNull(parameterType));
return this;
}
Expand All @@ -76,5 +78,4 @@ public MethodSignatureBuilder addException(TypeVariable exceptionType) {
this.exceptions.add(Objects.requireNonNull(exceptionType));
return this;
}

}
}
20 changes: 12 additions & 8 deletions src/main/java/io/quarkus/gizmo/SignatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import io.quarkus.gizmo.Type.TypeVariable;

/**
* Builds a signature as defined in JVMS 17, chapter "4.7.9.1. Signatures".
* Builds a generic signature as defined in JVMS 17, chapter "4.7.9.1. Signatures".
*
* @see SignatureElement#setSignature(String)
*/
public interface SignatureBuilder {

static ClassSignatureBuilder forClass() {
return new ClassSignatureBuilderImpl();
}
Expand All @@ -24,12 +23,14 @@ static FieldSignatureBuilder forField() {
}

/**
* @return the signature
* @return the generic signature
*/
String build();

/**
* Builds a generic signature of a class (including interfaces).
*/
interface ClassSignatureBuilder extends SignatureBuilder {

ClassSignatureBuilder addTypeParameter(TypeVariable typeParameter);

ClassSignatureBuilder setSuperClass(ClassType superClass);
Expand All @@ -41,22 +42,25 @@ interface ClassSignatureBuilder extends SignatureBuilder {
ClassSignatureBuilder addSuperInterface(ParameterizedType interfaceType);
}

/**
* Builds a generic signature of a method (including constructors).
*/
interface MethodSignatureBuilder extends SignatureBuilder {

MethodSignatureBuilder addTypeParameter(TypeVariable typeParameter);

MethodSignatureBuilder setReturnType(Type returnType);

MethodSignatureBuilder addParameter(Type parameter);
MethodSignatureBuilder addParameterType(Type parameterType);

MethodSignatureBuilder addException(ClassType exceptionType);

MethodSignatureBuilder addException(TypeVariable exceptionType);
}

/**
* Builds a generic signature of a field. Also usable for building generic signatures of record components.
*/
interface FieldSignatureBuilder extends SignatureBuilder {

FieldSignatureBuilder setType(Type type);
}

}
2 changes: 1 addition & 1 deletion src/main/java/io/quarkus/gizmo/SignatureElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface SignatureElement<S> {
/**
* Use the convenient {@link SignatureBuilder} to build signatures for classes, methods and fields.
*
* @param signature The signature as defined in JVMS 17, chapter "4.7.9.1. Signatures"
* @param signature The generic signature as defined in JVMS 17, chapter "4.7.9.1. Signatures"
* @return the element
* @see SignatureBuilder
*/
Expand Down
Loading

0 comments on commit 634a4b6

Please sign in to comment.