Skip to content

Commit

Permalink
Merge pull request #708 from arturbosch/master
Browse files Browse the repository at this point in the history
Check that users don't put empty strings in places where they make no sense
  • Loading branch information
matozoid committed Jan 15, 2017
2 parents d5dd852 + 8c18981 commit 8f090e0
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedList;
import java.util.List;

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

/**
Expand Down Expand Up @@ -109,12 +110,12 @@ public EnumConstantDeclaration getEntry(int i) {
return getEntries().get(i);
}

public EnumDeclaration setEntry(int i, EnumConstantDeclaration element){
public EnumDeclaration setEntry(int i, EnumConstantDeclaration element) {
getEntries().set(i, element);
return this;
}

public EnumDeclaration addEntry(EnumConstantDeclaration element){
public EnumDeclaration addEntry(EnumConstantDeclaration element) {
getEntries().add(element);
return this;
}
Expand All @@ -140,7 +141,8 @@ public EnumDeclaration setImplementedTypes(NodeList<ClassOrInterfaceType> implem
}

public EnumConstantDeclaration addEnumConstant(String name) {
EnumConstantDeclaration enumConstant = new EnumConstantDeclaration(assertNotNull(name));
assertNonEmpty(name);
EnumConstantDeclaration enumConstant = new EnumConstantDeclaration(name);
getEntries().add(enumConstant);
enumConstant.setParentNode(this);
return enumConstant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,8 @@ public MethodDeclaration createGetter() {
String fieldName = variable.getNameAsString();
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());
final MethodDeclaration getter;
if (parentClass.isPresent())
getter = parentClass.get().addMethod("get" + fieldNameUpper, PUBLIC);
else
getter = parentEnum.get().addMethod("get" + fieldNameUpper, PUBLIC);
getter = parentClass.map(clazz -> clazz.addMethod("get" + fieldNameUpper, PUBLIC))
.orElseGet(() -> parentEnum.get().addMethod("get" + fieldNameUpper, PUBLIC));
getter.setType(variable.getType());
BlockStmt blockStmt = new BlockStmt();
getter.setBody(blockStmt);
Expand Down Expand Up @@ -214,10 +212,8 @@ public MethodDeclaration createSetter() {
String fieldNameUpper = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length());

final MethodDeclaration setter;
if (parentClass.isPresent())
setter = parentClass.get().addMethod("set" + fieldNameUpper, PUBLIC);
else
setter = parentEnum.get().addMethod("set" + fieldNameUpper, PUBLIC);
setter = parentClass.map(clazz -> clazz.addMethod("set" + fieldNameUpper, PUBLIC))
.orElseGet(() -> parentEnum.get().addMethod("set" + fieldNameUpper, PUBLIC));
setter.setType(new VoidType());
setter.getParameters().add(new Parameter(variable.getType(), fieldName));
BlockStmt blockStmt2 = new BlockStmt();
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import java.util.Optional;

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

/**
* A name that may consist of multiple identifiers.
Expand Down Expand Up @@ -76,13 +76,16 @@ public <A> void accept(final VoidVisitor<A> v, final A arg) {
v.visit(this, arg);
}

@Override
public final String getIdentifier() {
return identifier;
}

@Override
public Name setIdentifier(final String identifier) {
assertNonEmpty(identifier);
notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifier, identifier);
this.identifier = assertNotNull(identifier);
this.identifier = identifier;
return this;
}

Expand All @@ -94,6 +97,7 @@ public Name setIdentifier(final String identifier) {
* @return instanceof {@link Name}
*/
public static Name parse(String qualifiedName) {
assertNonEmpty(qualifiedName);
String[] split = qualifiedName.split("\\.");
Name ret = new Name(split[0]);
for (int i = 1; i < split.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

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

/**
* A name that consists of a single identifier.
Expand Down Expand Up @@ -69,8 +69,9 @@ public final String getIdentifier() {

@Override
public SimpleName setIdentifier(final String identifier) {
assertNonEmpty(identifier);
notifyPropertyChange(ObservableProperty.IDENTIFIER, this.identifier, identifier);
this.identifier = assertNotNull(identifier);
this.identifier = identifier;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.lang.annotation.Annotation;
import java.util.Optional;

import static com.github.javaparser.ast.expr.Name.parse;

/**
* A node that can be annotated.
*
Expand Down Expand Up @@ -66,7 +64,7 @@ default N addAnnotation(AnnotationExpr element) {
*/
default NormalAnnotationExpr addAnnotation(String name) {
NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr(
parse(name), new NodeList<>());
Name.parse(name), new NodeList<>());
getAnnotations().add(normalAnnotationExpr);
normalAnnotationExpr.setParentNode((Node) this);
return normalAnnotationExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import com.github.javaparser.ast.Node;

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

public interface NodeWithIdentifier<N extends Node> {
String getIdentifier();

Expand All @@ -33,6 +35,7 @@ default String getId() {
}

default N setId(String identifier) {
assertNonEmpty(identifier);
return setIdentifier(identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Name;

import static com.github.javaparser.ast.expr.Name.parse;
import static com.github.javaparser.utils.Utils.assertNonEmpty;

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

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

default String getNameAsString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ default BlockStmt createBody() {
BlockStmt block = new BlockStmt();
setBody(block);
block.setParentNode((Node) this);

return block;
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2017 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/

package com.github.javaparser.ast.nodeTypes;

import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.javadoc.description.JavadocDescription;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -37,6 +36,7 @@
* of this writing this comment does not contain any block tags (such as <code>@see AnotherClass</code>)
*/
public class Javadoc {

private JavadocDescription description;
private List<JavadocBlockTag> blockTags;

Expand Down Expand Up @@ -102,15 +102,22 @@ public JavadocComment toComment(String indentation) {
return new JavadocComment(sb.toString());
}

public JavadocDescription getDescription() {
return description;
}

public List<JavadocBlockTag> getBlockTags() {
return blockTags;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Javadoc document = (Javadoc) o;

if (!description.equals(document.description)) return false;
return blockTags.equals(document.blockTags);
return description.equals(document.description) && blockTags.equals(document.blockTags);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public static <T> T assertNotNull(T o) {
return o;
}

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

/**
* @return string with ASCII characters 10 and 13 replaced by the text "\n" and "\r".
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
* Copyright (C) 2011, 2013-2017 The JavaParser Team.
*
* This file is part of JavaParser.
*
Expand Down Expand Up @@ -37,4 +37,9 @@ public void parsingAndUnparsingWorks() {
Name name = Name.parse("a.b.c");
assertEquals("a.b.c", name.asString());
}

@Test(expected = AssertionError.class)
public void parsingEmptyNameThrowsException() {
Name.parse("");
}
}

0 comments on commit 8f090e0

Please sign in to comment.