Skip to content

Commit

Permalink
Reorganize toString/equals/hashCode methods in the Type hiera…
Browse files Browse the repository at this point in the history
…rchy
  • Loading branch information
Ladicek committed Sep 16, 2022
1 parent a174a3f commit c0268ae
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 170 deletions.
58 changes: 29 additions & 29 deletions core/src/main/java/org/jboss/jandex/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ public DotName name() {
return DotName.createSimple(builder.toString());
}

/**
* The number of dimensions this array type has. For example, <code>String[][]</code>, would return a value
* of 2.
*
* @return the number of dimensions of this array type
*/
public int dimensions() {
return dimensions;
}

@Override
public Kind kind() {
return Kind.ARRAY;
}

@Override
public ArrayType asArrayType() {
return this;
}

@Override
Type copyType(AnnotationInstance[] newAnnotations) {
return new ArrayType(component, dimensions, newAnnotations);
}

Type copyType(Type component, int dimensions) {
return new ArrayType(component, dimensions, annotationArray());
}

@Override
String toString(boolean simple) {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -123,26 +152,6 @@ private void appendArraySyntax(StringBuilder builder) {
}
}

/**
* The number of dimensions this array type has. For example, <code>String[][]</code>, would return a value
* of 2.
*
* @return the number of dimensions of this array type
*/
public int dimensions() {
return dimensions;
}

@Override
public Kind kind() {
return Kind.ARRAY;
}

@Override
public ArrayType asArrayType() {
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -169,13 +178,4 @@ public int hashCode() {
hash = 31 * hash + dimensions;
return this.hash = hash;
}

@Override
Type copyType(AnnotationInstance[] newAnnotations) {
return new ArrayType(component, dimensions, newAnnotations);
}

Type copyType(Type component, int dimensions) {
return new ArrayType(component, dimensions, annotationArray());
}
}
46 changes: 23 additions & 23 deletions core/src/main/java/org/jboss/jandex/ParameterizedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ public ParameterizedType asParameterizedType() {
return this;
}

@Override
ParameterizedType copyType(AnnotationInstance[] newAnnotations) {
return new ParameterizedType(name(), arguments, owner, newAnnotations);
}

ParameterizedType copyType(Type[] arguments) {
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

ParameterizedType copyType(int argumentIndex, Type argument) {
if (argumentIndex > this.arguments.length) {
throw new IllegalArgumentException("Type argument index outside of bounds");
}

Type[] arguments = this.arguments.clone();
arguments[argumentIndex] = argument;
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

ParameterizedType copyType(Type owner) {
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

@Override
String toString(boolean simple) {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -153,29 +176,6 @@ String toString(boolean simple) {
return builder.toString();
}

@Override
ParameterizedType copyType(AnnotationInstance[] newAnnotations) {
return new ParameterizedType(name(), arguments, owner, newAnnotations);
}

ParameterizedType copyType(Type[] arguments) {
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

ParameterizedType copyType(int argumentIndex, Type argument) {
if (argumentIndex > this.arguments.length) {
throw new IllegalArgumentException("Type argument index outside of bounds");
}

Type[] arguments = this.arguments.clone();
arguments[argumentIndex] = argument;
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

ParameterizedType copyType(Type owner) {
return new ParameterizedType(name(), arguments, owner, annotationArray());
}

public boolean equals(Object o) {
if (this == o) {
return true;
Expand Down
42 changes: 21 additions & 21 deletions core/src/main/java/org/jboss/jandex/PrimitiveType.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,11 @@ public PrimitiveType asPrimitiveType() {
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof PrimitiveType)) {
return false;
}

PrimitiveType that = (PrimitiveType) o;
return super.equals(o) && primitive == that.primitive;
}

@Override
Type copyType(AnnotationInstance[] newAnnotations) {
return new PrimitiveType(primitive, newAnnotations);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + primitive.hashCode();
return result;
}

char toCode() {
Primitive primitive = this.primitive;
if (primitive == Primitive.BYTE) {
Expand Down Expand Up @@ -205,4 +184,25 @@ static PrimitiveType fromOridinal(int ordinal) {
throw new IllegalArgumentException();
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof PrimitiveType)) {
return false;
}

PrimitiveType that = (PrimitiveType) o;
return super.equals(o) && primitive == that.primitive;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + primitive.hashCode();
return result;
}
}
110 changes: 55 additions & 55 deletions core/src/main/java/org/jboss/jandex/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,61 +332,6 @@ public VoidType asVoidType() {
throw new IllegalArgumentException("Not a void type!");
}

/**
* Returns a string representation for this type. It is similar, yet not equivalent
* to a Java source code representation.
*
* @return the string representation.
*/
public String toString() {
return toString(false);
}

String toString(boolean simple) {
StringBuilder builder = new StringBuilder();
String packagePrefix = name.packagePrefix();
if (packagePrefix != null) {
builder.append(packagePrefix).append('.');
}
appendAnnotations(builder);
builder.append(name.withoutPackagePrefix());

return builder.toString();
}

void appendAnnotations(StringBuilder builder) {
AnnotationInstance[] annotations = this.annotations;
if (annotations.length > 0) {
for (AnnotationInstance instance : annotations) {
builder.append(instance.toString(true)).append(' ');
}
}
}

/**
* Compares this Type with another type, and returns true if they are equivalent.
* A type is equivalent to another type if it is the same kind, and all of its
* fields are equal. This includes annotations, which must be equal as well.
*
* @param o the type to compare to
* @return true if equal
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Type type = (Type) o;

return name.equals(type.name) && Arrays.equals(annotations, type.annotations);
}

AnnotationInstance[] annotationArray() {
return annotations;
}
Expand Down Expand Up @@ -488,6 +433,61 @@ Type addAnnotation(AnnotationInstance annotation) {

abstract Type copyType(AnnotationInstance[] newAnnotations);

/**
* Returns a string representation for this type. It is similar, yet not equivalent
* to a Java source code representation.
*
* @return the string representation.
*/
public String toString() {
return toString(false);
}

String toString(boolean simple) {
StringBuilder builder = new StringBuilder();
String packagePrefix = name.packagePrefix();
if (packagePrefix != null) {
builder.append(packagePrefix).append('.');
}
appendAnnotations(builder);
builder.append(name.withoutPackagePrefix());

return builder.toString();
}

void appendAnnotations(StringBuilder builder) {
AnnotationInstance[] annotations = this.annotations;
if (annotations.length > 0) {
for (AnnotationInstance instance : annotations) {
builder.append(instance.toString(true)).append(' ');
}
}
}

/**
* Compares this Type with another type, and returns true if they are equivalent.
* A type is equivalent to another type if it is the same kind, and all of its
* fields are equal. This includes annotations, which must be equal as well.
*
* @param o the type to compare to
* @return true if equal
* @see Object#equals(Object)
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Type type = (Type) o;

return name.equals(type.name) && Arrays.equals(annotations, type.annotations);
}

/**
* Computes a hash code representing this type.
*
Expand Down
30 changes: 15 additions & 15 deletions core/src/main/java/org/jboss/jandex/TypeVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ public TypeVariable asTypeVariable() {
return this;
}

@Override
Type copyType(AnnotationInstance[] newAnnotations) {
return new TypeVariable(name, bounds, newAnnotations, hasImplicitObjectBound());
}

TypeVariable copyType(int boundIndex, Type bound) {
if (boundIndex > bounds.length) {
throw new IllegalArgumentException("Bound index outside of bounds");
}

Type[] bounds = this.bounds.clone();
bounds[boundIndex] = bound;
return new TypeVariable(name, bounds, annotationArray(), hasImplicitObjectBound());
}

@Override
String toString(boolean simple) {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -149,21 +164,6 @@ public boolean equals(Object o) {
&& hasImplicitObjectBound() == that.hasImplicitObjectBound();
}

@Override
Type copyType(AnnotationInstance[] newAnnotations) {
return new TypeVariable(name, bounds, newAnnotations, hasImplicitObjectBound());
}

TypeVariable copyType(int boundIndex, Type bound) {
if (boundIndex > bounds.length) {
throw new IllegalArgumentException("Bound index outside of bounds");
}

Type[] bounds = this.bounds.clone();
bounds[boundIndex] = bound;
return new TypeVariable(name, bounds, annotationArray(), hasImplicitObjectBound());
}

@Override
public int hashCode() {
int hash = this.hash & HASH_MASK;
Expand Down

0 comments on commit c0268ae

Please sign in to comment.