Skip to content

Commit

Permalink
Add more assertions when a string should be used as an identifier - #502
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Jan 15, 2017
1 parent 22a97ae commit b55c4de
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 7 deletions.
Expand Up @@ -141,6 +141,7 @@ public EnumDeclaration setImplementedTypes(NodeList<ClassOrInterfaceType> implem
}

public EnumConstantDeclaration addEnumConstant(String name) {
assertNonEmpty(name);
EnumConstantDeclaration enumConstant = new EnumConstantDeclaration(name);
getEntries().add(enumConstant);
enumConstant.setParentNode(this);
Expand Down
Expand Up @@ -35,6 +35,7 @@

import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNonEmpty;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
Expand Down Expand Up @@ -133,7 +134,7 @@ public VariableDeclarator setInitializer(Expression initializer) {
* @return this, the VariableDeclarator
*/
public VariableDeclarator setInitializer(String init) {
return setInitializer(new NameExpr(assertNotNull(init)));
return setInitializer(new NameExpr(assertNonEmpty(init)));
}

@Override
Expand Down
Expand Up @@ -32,6 +32,8 @@

import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNonEmpty;

/**
* Method reference expressions introduced in Java 8 specifically designed to simplify lambda Expressions.
* Note that the field "identifier", indicating the word to the right of the ::, is not always a method name,
Expand Down Expand Up @@ -116,6 +118,7 @@ public String getIdentifier() {

@Override
public MethodReferenceExpr setIdentifier(String identifier) {
assertNonEmpty(identifier);
notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifier, identifier);
this.identifier = identifier;
return this;
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNonEmpty;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* A name that may consist of multiple identifiers.
Expand Down Expand Up @@ -86,7 +85,7 @@ public final String getIdentifier() {
public Name setIdentifier(final String identifier) {
assertNonEmpty(identifier);
notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifier, identifier);
this.identifier = assertNotNull(identifier);
this.identifier = identifier;
return this;
}

Expand Down
Expand Up @@ -24,6 +24,8 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Name;

import static com.github.javaparser.utils.Utils.assertNonEmpty;

/**
* A node with a (qualified) name.
* <p>
Expand All @@ -38,6 +40,7 @@ public interface NodeWithName<N extends Node> {

@SuppressWarnings("unchecked")
default N setName(String name) {
assertNonEmpty(name);
return setName(Name.parse(name));
}

Expand Down
Expand Up @@ -5,6 +5,8 @@

import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNonEmpty;

/**
* A node that can have a label.
*/
Expand All @@ -14,6 +16,7 @@ public interface NodeWithOptionalLabel<T extends Node> {
T setLabel(SimpleName label);

default T setLabel(String label) {
assertNonEmpty(label);
return setLabel(new SimpleName(label));
}

Expand Down
Expand Up @@ -24,6 +24,8 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.SimpleName;

import static com.github.javaparser.utils.Utils.assertNonEmpty;

/**
* A node with a name.
* <p>
Expand All @@ -36,6 +38,7 @@ public interface NodeWithSimpleName<N extends Node> {

@SuppressWarnings("unchecked")
default N setName(String name) {
assertNonEmpty(name);
return setName(new SimpleName(name));
}

Expand Down
Expand Up @@ -26,6 +26,8 @@
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;

import static com.github.javaparser.utils.Utils.assertNonEmpty;

/**
* A node with a type.
* <p>
Expand Down Expand Up @@ -64,6 +66,7 @@ default N setType(Class<?> typeClass) {

@SuppressWarnings("unchecked")
default N setType(final String type) {
assertNonEmpty(type);
ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(type);
return setType((T) classOrInterfaceType);
}
Expand Down
Expand Up @@ -50,11 +50,11 @@ public static <T> T assertNotNull(T o) {
return o;
}

public static String assertNonEmpty(String id) {
if (id == null || id.isEmpty()) {
throw new AssertionError("An identifier was unexpectedly empty.");
public static String assertNonEmpty(String string) {
if (string == null || string.isEmpty()) {
throw new AssertionError("A string was unexpectedly empty.");
}
return id;
return string;
}

/**
Expand Down

0 comments on commit b55c4de

Please sign in to comment.