Skip to content

Commit

Permalink
Completed validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jan 7, 2016
1 parent 582e7cd commit 08e48a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Expand Up @@ -1738,12 +1738,24 @@ public String toString() {
} }
} }


/**
* A validator for Java types that are defined for a specified type use within a Java class file.
*/
enum Validator implements Visitor<Boolean> { enum Validator implements Visitor<Boolean> {


/**
* A validator for checking a type's non-null super class.
*/
SUPER_CLASS(false, false, false, false, false), SUPER_CLASS(false, false, false, false, false),


/**
* A validator for an interface type.
*/
INTERFACE(false, false, false, false, false), INTERFACE(false, false, false, false, false),


/**
* A validator for a type variable, either declared by a
*/
TYPE_VARIABLE(false, false, true, false, false), TYPE_VARIABLE(false, false, true, false, false),


FIELD(true, true, true, false, false), FIELD(true, true, true, false, false),
Expand Down
Expand Up @@ -805,10 +805,17 @@ public TypeDescription validated() {
} else if (!isValidIdentifier(variableSymbol)) { } else if (!isValidIdentifier(variableSymbol)) {
throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + this); throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + this);
} }
boolean interfaceBound = false;
for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) { for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) {
if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) { if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) {
throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + "for " + this); throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + " for " + this);
} else if (interfaceBound && (bound.getSort().isTypeVariable() || !bound.asErasure().isInterface())) {
throw new IllegalStateException("Illegal interface bound " + bound + " of " + typeVariable + " for " + this);
} }
interfaceBound = true;
}
if (!interfaceBound) {
throw new IllegalStateException("Type variable " + typeVariable + " for " + this + " does not define at least one bound");
} }
} }
Set<TypeDescription> typeAnnotationTypes = new HashSet<TypeDescription>(); Set<TypeDescription> typeAnnotationTypes = new HashSet<TypeDescription>();
Expand Down Expand Up @@ -848,10 +855,17 @@ public TypeDescription validated() {
} else if (!isValidIdentifier(variableSymbol)) { } else if (!isValidIdentifier(variableSymbol)) {
throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + methodDescription); throw new IllegalStateException("Illegal type variable name of " + typeVariable + " for " + methodDescription);
} }
boolean interfaceBound = false;
for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) { for (TypeDescription.Generic bound : typeVariable.getUpperBounds()) {
if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) { if (!bound.accept(Generic.Visitor.Validator.TYPE_VARIABLE)) {
throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + " for " + methodDescription); throw new IllegalStateException("Illegal type variable bound " + bound + " of " + typeVariable + " for " + methodDescription);
} else if (interfaceBound && (bound.getSort().isTypeVariable() || !bound.asErasure().isInterface())) {
throw new IllegalStateException("Illegal interface bound " + bound + " of " + typeVariable + " for " + methodDescription);
} }
interfaceBound = true;
}
if (!interfaceBound) {
throw new IllegalStateException("Type variable " + typeVariable + " for " + methodDescription + " does not define at least one bound");
} }
} }
Set<TypeDescription> methodAnnotationTypes = new HashSet<TypeDescription>(); Set<TypeDescription> methodAnnotationTypes = new HashSet<TypeDescription>();
Expand Down Expand Up @@ -903,26 +917,38 @@ public TypeDescription validated() {
return this; return this;
} }


private static boolean isValidIdentifier(String[] name) { /**
if (name.length == 0) { * Checks if an array of identifiers is a valid compound Java identifier.
*
* @param identifier an array of potentially invalid Java identifiers.
* @return {@code true} if all identifiers are valid and the array is not empty.
*/
private static boolean isValidIdentifier(String[] identifier) {
if (identifier.length == 0) {
return false; return false;
} }
for (String part : name) { for (String part : identifier) {
if (!isValidIdentifier(part)) { if (!isValidIdentifier(part)) {
return false; return false;
} }
} }
return true; return true;
} }


private static boolean isValidIdentifier(String name) { /**
if (name.isEmpty() || !Character.isJavaIdentifierStart(name.charAt(0))) { * Checks if a Java identifier is valid.
*
* @param identifier The identifier to check for validity.
* @return {@code true} if the given identifier is valid.
*/
private static boolean isValidIdentifier(String identifier) {
if (identifier.isEmpty() || !Character.isJavaIdentifierStart(identifier.charAt(0))) {
return false; return false;
} else if (name.equals(PackageDescription.PACKAGE_CLASS_NAME)) { } else if (identifier.equals(PackageDescription.PACKAGE_CLASS_NAME)) {
return true; return true;
} }
for (int index = 1; index < name.length(); index++) { for (int index = 1; index < identifier.length(); index++) {
if (!Character.isJavaIdentifierPart(name.charAt(index))) { if (!Character.isJavaIdentifierPart(identifier.charAt(index))) {
return false; return false;
} }
} }
Expand Down
Expand Up @@ -12,6 +12,8 @@


public class TypeDescriptionGenericVisitorValidatorTest { public class TypeDescriptionGenericVisitorValidatorTest {


// TODO

@Rule @Rule
public TestRule mockitoRule = new MockitoRule(this); public TestRule mockitoRule = new MockitoRule(this);


Expand Down
Expand Up @@ -472,4 +472,6 @@ public void testInterfaceTypesVisited() throws Exception {
verify(typeDescription).asGenericType(); verify(typeDescription).asGenericType();
verifyNoMoreInteractions(typeDescription); verifyNoMoreInteractions(typeDescription);
} }

// TODO: Validation tests
} }

0 comments on commit 08e48a1

Please sign in to comment.