-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from mkouba/signature-from
Introduce SignatureBuilder API
- Loading branch information
Showing
8 changed files
with
1,107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/io/quarkus/gizmo/ClassSignatureBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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 { | ||
|
||
List<TypeVariable> typeParameters = new ArrayList<>(); | ||
Type superClass = ClassType.OBJECT; | ||
List<Type> superInterfaces = new ArrayList<>(); | ||
|
||
@Override | ||
public String build() { | ||
StringBuilder signature = new StringBuilder(); | ||
|
||
// type params | ||
if (!typeParameters.isEmpty()) { | ||
signature.append('<'); | ||
for (TypeVariable typeParameter : typeParameters) { | ||
typeParameter.appendTypeParameterToSignature(signature); | ||
} | ||
signature.append('>'); | ||
} | ||
|
||
// superclass | ||
superClass.appendToSignature(signature); | ||
|
||
// interfaces | ||
if (!superInterfaces.isEmpty()) { | ||
for (Type superInterface : superInterfaces) { | ||
superInterface.appendToSignature(signature); | ||
} | ||
} | ||
return signature.toString(); | ||
} | ||
|
||
@Override | ||
public ClassSignatureBuilder addTypeParameter(TypeVariable typeParameter) { | ||
typeParameters.add(typeParameter); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClassSignatureBuilder setSuperClass(ClassType superClass) { | ||
this.superClass = superClass; | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClassSignatureBuilder setSuperClass(ParameterizedType superClass) { | ||
if (containsWildcard(superClass)) { | ||
throw new IllegalArgumentException("An extended class type may not specify a wildcard"); | ||
} | ||
|
||
this.superClass = superClass; | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClassSignatureBuilder addInterface(ClassType interfaceType) { | ||
superInterfaces.add(interfaceType); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClassSignatureBuilder addInterface(ParameterizedType interfaceType) { | ||
if (containsWildcard(interfaceType)) { | ||
throw new IllegalArgumentException("An implemented interface type may not specify a wildcard"); | ||
} | ||
superInterfaces.add(interfaceType); | ||
return this; | ||
} | ||
|
||
private boolean containsWildcard(Type type) { | ||
if (type.isWildcard()) { | ||
return true; | ||
} else if (type.isParameterizedType()) { | ||
for (Type typeArgument : type.asParameterizedType().getTypeArguments()) { | ||
if (containsWildcard(typeArgument)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/quarkus/gizmo/FieldSignatureBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import java.util.Objects; | ||
|
||
import io.quarkus.gizmo.SignatureBuilder.FieldSignatureBuilder; | ||
|
||
class FieldSignatureBuilderImpl implements FieldSignatureBuilder { | ||
private Type type; | ||
|
||
@Override | ||
public String build() { | ||
StringBuilder signature = new StringBuilder(); | ||
type.appendToSignature(signature); | ||
return signature.toString(); | ||
} | ||
|
||
@Override | ||
public FieldSignatureBuilder setType(Type type) { | ||
this.type = Objects.requireNonNull(type); | ||
return this; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/io/quarkus/gizmo/MethodSignatureBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
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 List<TypeVariable> typeParameters = new ArrayList<>(); | ||
private Type returnType = VoidType.INSTANCE; | ||
private List<Type> parameterTypes = new ArrayList<>(); | ||
private List<Type> exceptions = new ArrayList<>(); | ||
|
||
@Override | ||
public String build() { | ||
StringBuilder signature = new StringBuilder(); | ||
|
||
// type params | ||
if (!typeParameters.isEmpty()) { | ||
signature.append('<'); | ||
for (TypeVariable typeParameter : typeParameters) { | ||
typeParameter.appendTypeParameterToSignature(signature); | ||
} | ||
signature.append('>'); | ||
} | ||
|
||
// param types | ||
signature.append('('); | ||
for (Type parameterType : parameterTypes) { | ||
parameterType.appendToSignature(signature); | ||
} | ||
signature.append(')'); | ||
|
||
// return type | ||
returnType.appendToSignature(signature); | ||
|
||
// exception types | ||
if (!exceptions.isEmpty()) { | ||
for (Type exceptionType : exceptions) { | ||
signature.append('^'); | ||
exceptionType.appendToSignature(signature); | ||
} | ||
} | ||
|
||
return signature.toString(); | ||
} | ||
|
||
@Override | ||
public MethodSignatureBuilder addTypeParameter(TypeVariable typeParameter) { | ||
typeParameters.add(Objects.requireNonNull(typeParameter)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MethodSignatureBuilder setReturnType(Type returnType) { | ||
this.returnType = Objects.requireNonNull(returnType); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MethodSignatureBuilder addParameterType(Type parameterType) { | ||
this.parameterTypes.add(Objects.requireNonNull(parameterType)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MethodSignatureBuilder addException(ClassType exceptionType) { | ||
this.exceptions.add(Objects.requireNonNull(exceptionType)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public MethodSignatureBuilder addException(TypeVariable exceptionType) { | ||
this.exceptions.add(Objects.requireNonNull(exceptionType)); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import io.quarkus.gizmo.Type.ClassType; | ||
import io.quarkus.gizmo.Type.ParameterizedType; | ||
import io.quarkus.gizmo.Type.TypeVariable; | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
static MethodSignatureBuilder forMethod() { | ||
return new MethodSignatureBuilderImpl(); | ||
} | ||
|
||
static FieldSignatureBuilder forField() { | ||
return new FieldSignatureBuilderImpl(); | ||
} | ||
|
||
/** | ||
* @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); | ||
|
||
ClassSignatureBuilder setSuperClass(ParameterizedType superClass); | ||
|
||
ClassSignatureBuilder addInterface(ClassType interfaceType); | ||
|
||
ClassSignatureBuilder addInterface(ParameterizedType interfaceType); | ||
} | ||
|
||
/** | ||
* Builds a generic signature of a method (including constructors). | ||
*/ | ||
interface MethodSignatureBuilder extends SignatureBuilder { | ||
MethodSignatureBuilder addTypeParameter(TypeVariable typeParameter); | ||
|
||
MethodSignatureBuilder setReturnType(Type returnType); | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.