Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.util.StringUtils;

Expand All @@ -42,6 +45,66 @@
import io.swagger.models.properties.Property;

public final class ClassUtils {
// reference:
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
// https://en.wikipedia.org/wiki/List_of_Java_keywords
private static final Set<String> JAVA_RESERVED_WORDS = new HashSet<>();
static {
JAVA_RESERVED_WORDS.addAll(Arrays.asList("true",
"false",
"null",
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while"));
}

private ClassUtils() {
}

Expand Down Expand Up @@ -217,6 +280,20 @@ public static String correctMethodParameterName(String name) {
}

public static String correctClassName(String name) {
return name.replace("-", "_");
String parts[] = name.split("\\.", -1);
for (int idx = 0; idx < parts.length; idx++) {
String part = parts[idx];
if (part.isEmpty()) {
parts[idx] = "_";
continue;
}

part = part.replace('-', '_');
if (Character.isDigit(part.charAt(0)) || JAVA_RESERVED_WORDS.contains(part)) {
part = "_" + part;
}
parts[idx] = part;
}
return String.join(".", parts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,86 @@

package io.servicecomb.swagger.generator.core;

import static org.hamcrest.core.Is.is;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ws.rs.Path;

import org.junit.Assert;
import org.junit.Test;

import io.servicecomb.swagger.generator.core.SwaggerGenerator;
import io.servicecomb.swagger.generator.core.schema.User;
import io.servicecomb.swagger.generator.core.unittest.UnitTestSwaggerUtils;
import io.servicecomb.swagger.generator.core.utils.ClassUtils;
import io.swagger.annotations.SwaggerDefinition;

@SwaggerDefinition
public class TestClassUtils {
// copy from ClassUtils
// do not use JAVA_RESERVED_WORDS in ClassUtils directly
// because that's the test target.
private static final Set<String> JAVA_RESERVED_WORDS = new HashSet<>();
static {
JAVA_RESERVED_WORDS.addAll(Arrays.asList("true",
"false",
"null",
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while"));
}

@Test
public void testHasAnnotation() {
Assert.assertEquals(true, ClassUtils.hasAnnotation(TestClassUtils.class, SwaggerDefinition.class));
Expand Down Expand Up @@ -61,4 +125,37 @@ public void testCreateInterface() {
Assert.assertEquals("java.util.List<io.servicecomb.swagger.generator.core.schema.User>",
method.getGenericReturnType().getTypeName());
}

@Test
public void testCorrectClassNameInTheCrossed() {
String result = ClassUtils.correctClassName("a-b");
Assert.assertThat(result, is("a_b"));

result = ClassUtils.correctClassName("a.a-b");
Assert.assertThat(result, is("a.a_b"));
}

@Test
public void testCorrectClassNameStartWithNumber() {
String result = ClassUtils.correctClassName("100");
Assert.assertThat(result, is("_100"));

result = ClassUtils.correctClassName("a.100");
Assert.assertThat(result, is("a._100"));
}

@Test
public void testCorrectClassNameReservedWords() {
String name = String.join(".", JAVA_RESERVED_WORDS);
String expectResult = "_" + String.join("._", JAVA_RESERVED_WORDS);

String result = ClassUtils.correctClassName(name);
Assert.assertThat(result, is(expectResult));
}

@Test
public void testCorrectClassNameEmptyPart() {
String result = ClassUtils.correctClassName("..a..a..");
Assert.assertThat(result, is("_._.a._.a._._"));
}
}