Skip to content

Commit

Permalink
edit the error message from the TypeCastingGenerator, when the types …
Browse files Browse the repository at this point in the history
…do not match, to show the actual type -- and run generators
  • Loading branch information
MysterAitch committed Nov 29, 2020
1 parent df2dc28 commit 0a678a5
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nod
final String typeName = nodeMetaModel.getTypeName();
final ClassOrInterfaceDeclaration baseCoid = baseCode.b;
final CompilationUnit baseCu = baseCode.a;

generateIsType(baseCu, nodeCoid, baseCoid, typeName);
generateAsType(baseCu, nodeCoid, baseCoid, typeName);
generateToType(nodeCu, baseCu, nodeCoid, baseCoid, typeName);
generateIfType(nodeCu, baseCu, nodeCoid, baseCoid, typeName);
}

private void generateAsType(CompilationUnit baseCu, ClassOrInterfaceDeclaration nodeCoid, ClassOrInterfaceDeclaration baseCoid, String typeName) {
final MethodDeclaration asTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(f("public %s as%s() { throw new IllegalStateException(f(\"%%s is not an %s\", this)); }", typeName, typeName, typeName));
final MethodDeclaration asTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(f("public %s as%s() { throw new IllegalStateException(f(\"%%s is not %s, it is %%s\", this, this.getClass().getSimpleName())); }", typeName, typeName, typeName));
final MethodDeclaration asTypeNodeMethod = (MethodDeclaration) parseBodyDeclaration(f("@Override public %s as%s() { return this; }", typeName, typeName));
addOrReplaceWhenSameSignature(baseCoid, asTypeBaseMethod);
addOrReplaceWhenSameSignature(nodeCoid, asTypeNodeMethod);
Expand Down
116 changes: 57 additions & 59 deletions javaparser-core/src/main/java/com/github/javaparser/JavaToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import static com.github.javaparser.utils.CodeGenerationUtils.f;
import static com.github.javaparser.utils.Utils.SYSTEM_EOL;
import static com.github.javaparser.utils.Utils.assertNotNull;

import java.util.List;
import java.util.Optional;

import com.github.javaparser.ast.Generated;

/**
Expand Down Expand Up @@ -135,7 +133,7 @@ public JavaToken(Range range, int kind, String text, JavaToken previousToken, Ja
public Optional<Range> getRange() {
return Optional.ofNullable(range);
}

/*
* Returns true if the token has a range
*/
Expand Down Expand Up @@ -369,36 +367,36 @@ public enum Kind {
TILDE(109),
HOOK(110),
COLON(111),
EQ(112),
LE(113),
ARROW(112),
EQ(113),
GE(114),
NE(115),
SC_OR(116),
LE(115),
NE(116),
SC_AND(117),
INCR(118),
DECR(119),
PLUS(120),
MINUS(121),
STAR(122),
SLASH(123),
BIT_AND(124),
BIT_OR(125),
XOR(126),
REM(127),
LSHIFT(128),
PLUSASSIGN(129),
MINUSASSIGN(130),
STARASSIGN(131),
SLASHASSIGN(132),
ANDASSIGN(133),
ORASSIGN(134),
XORASSIGN(135),
REMASSIGN(136),
LSHIFTASSIGN(137),
RSIGNEDSHIFTASSIGN(138),
RUNSIGNEDSHIFTASSIGN(139),
ELLIPSIS(140),
ARROW(141),
SC_OR(118),
INCR(119),
DECR(120),
PLUS(121),
MINUS(122),
STAR(123),
SLASH(124),
BIT_AND(125),
BIT_OR(126),
XOR(127),
REM(128),
LSHIFT(129),
PLUSASSIGN(130),
MINUSASSIGN(131),
STARASSIGN(132),
SLASHASSIGN(133),
ANDASSIGN(134),
ORASSIGN(135),
XORASSIGN(136),
REMASSIGN(137),
LSHIFTASSIGN(138),
RSIGNEDSHIFTASSIGN(139),
RUNSIGNEDSHIFTASSIGN(140),
ELLIPSIS(141),
DOUBLECOLON(142),
RUNSIGNEDSHIFT(143),
RSIGNEDSHIFT(144),
Expand All @@ -424,65 +422,65 @@ public static Kind valueOf(int kind) {
case 142:
return DOUBLECOLON;
case 141:
return ARROW;
case 140:
return ELLIPSIS;
case 139:
case 140:
return RUNSIGNEDSHIFTASSIGN;
case 138:
case 139:
return RSIGNEDSHIFTASSIGN;
case 137:
case 138:
return LSHIFTASSIGN;
case 136:
case 137:
return REMASSIGN;
case 135:
case 136:
return XORASSIGN;
case 134:
case 135:
return ORASSIGN;
case 133:
case 134:
return ANDASSIGN;
case 132:
case 133:
return SLASHASSIGN;
case 131:
case 132:
return STARASSIGN;
case 130:
case 131:
return MINUSASSIGN;
case 129:
case 130:
return PLUSASSIGN;
case 128:
case 129:
return LSHIFT;
case 127:
case 128:
return REM;
case 126:
case 127:
return XOR;
case 125:
case 126:
return BIT_OR;
case 124:
case 125:
return BIT_AND;
case 123:
case 124:
return SLASH;
case 122:
case 123:
return STAR;
case 121:
case 122:
return MINUS;
case 120:
case 121:
return PLUS;
case 119:
case 120:
return DECR;
case 118:
case 119:
return INCR;
case 118:
return SC_OR;
case 117:
return SC_AND;
case 116:
return SC_OR;
case 115:
return NE;
case 115:
return LE;
case 114:
return GE;
case 113:
return LE;
case 112:
return EQ;
case 112:
return ARROW;
case 111:
return COLON;
case 110:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static com.github.javaparser.ast.Modifier.createModifierList;
import static com.github.javaparser.utils.CodeGenerationUtils.subtractPaths;
import static com.github.javaparser.utils.Utils.assertNotNull;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
Expand All @@ -40,7 +39,6 @@
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.javaparser.JavaParser;
import com.github.javaparser.JavaToken;
import com.github.javaparser.ParseResult;
Expand Down Expand Up @@ -506,7 +504,7 @@ public AnnotationDeclaration addAnnotationDeclaration(String name, Modifier.Keyw
public Optional<ClassOrInterfaceDeclaration> getClassByName(String className) {
return getTypes().stream().filter(type -> type.getNameAsString().equals(className) && type instanceof ClassOrInterfaceDeclaration && !((ClassOrInterfaceDeclaration) type).isInterface()).findFirst().map(t -> (ClassOrInterfaceDeclaration) t);
}

/**
* Try to get all local class declarations ending by its name (top level or inner class)
*
Expand Down Expand Up @@ -685,7 +683,7 @@ public static class Storage {
private final Path path;

private final Charset encoding;

private Printable printer;

private Storage(CompilationUnit compilationUnit, Path path) {
Expand All @@ -699,14 +697,14 @@ private Storage(CompilationUnit compilationUnit, Path path, Charset encoding) {
// default printer
this.printer = new PrettyPrinter();
}

/**
* Set a new printer
*/
public void setPrinter(Printable printer) {
this.printer = printer;
}

/**
* Returns the internal printer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static java.util.Collections.unmodifiableList;
import static java.util.Spliterator.DISTINCT;
import static java.util.Spliterator.NONNULL;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
Expand All @@ -44,7 +43,6 @@
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.github.javaparser.HasParentNode;
import com.github.javaparser.Position;
import com.github.javaparser.Range;
Expand Down Expand Up @@ -744,21 +742,18 @@ public LineSeparator getLineEndingStyleOrDefault(LineSeparator defaultLineSepara

public LineSeparator getLineEndingStyle() {
Node current = this;

// First check this node
if(current.containsData(Node.LINE_SEPARATOR_KEY)) {
if (current.containsData(Node.LINE_SEPARATOR_KEY)) {
LineSeparator lineSeparator = current.getData(Node.LINE_SEPARATOR_KEY);
return lineSeparator;
}

// Then check parent/ancestor nodes
while(current.getParentNode().isPresent()) {
while (current.getParentNode().isPresent()) {
current = current.getParentNode().get();
if(current.containsData(Node.LINE_SEPARATOR_KEY)) {
if (current.containsData(Node.LINE_SEPARATOR_KEY)) {
return current.getData(Node.LINE_SEPARATOR_KEY);
}
}

// Default to the system line separator if it's not already set within the parsed node/code.
return LineSeparator.SYSTEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public boolean isAnnotationDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public AnnotationDeclaration asAnnotationDeclaration() {
throw new IllegalStateException(f("%s is not an AnnotationDeclaration", this));
throw new IllegalStateException(f("%s is not AnnotationDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -145,7 +145,7 @@ public boolean isAnnotationMemberDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public AnnotationMemberDeclaration asAnnotationMemberDeclaration() {
throw new IllegalStateException(f("%s is not an AnnotationMemberDeclaration", this));
throw new IllegalStateException(f("%s is not AnnotationMemberDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -155,7 +155,7 @@ public boolean isCallableDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public CallableDeclaration asCallableDeclaration() {
throw new IllegalStateException(f("%s is not an CallableDeclaration", this));
throw new IllegalStateException(f("%s is not CallableDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -165,7 +165,7 @@ public boolean isClassOrInterfaceDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public ClassOrInterfaceDeclaration asClassOrInterfaceDeclaration() {
throw new IllegalStateException(f("%s is not an ClassOrInterfaceDeclaration", this));
throw new IllegalStateException(f("%s is not ClassOrInterfaceDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -175,7 +175,7 @@ public boolean isConstructorDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public ConstructorDeclaration asConstructorDeclaration() {
throw new IllegalStateException(f("%s is not an ConstructorDeclaration", this));
throw new IllegalStateException(f("%s is not ConstructorDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -185,7 +185,7 @@ public boolean isEnumConstantDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public EnumConstantDeclaration asEnumConstantDeclaration() {
throw new IllegalStateException(f("%s is not an EnumConstantDeclaration", this));
throw new IllegalStateException(f("%s is not EnumConstantDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -195,7 +195,7 @@ public boolean isEnumDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public EnumDeclaration asEnumDeclaration() {
throw new IllegalStateException(f("%s is not an EnumDeclaration", this));
throw new IllegalStateException(f("%s is not EnumDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -205,7 +205,7 @@ public boolean isFieldDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public FieldDeclaration asFieldDeclaration() {
throw new IllegalStateException(f("%s is not an FieldDeclaration", this));
throw new IllegalStateException(f("%s is not FieldDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -215,7 +215,7 @@ public boolean isInitializerDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public InitializerDeclaration asInitializerDeclaration() {
throw new IllegalStateException(f("%s is not an InitializerDeclaration", this));
throw new IllegalStateException(f("%s is not InitializerDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -225,7 +225,7 @@ public boolean isMethodDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public MethodDeclaration asMethodDeclaration() {
throw new IllegalStateException(f("%s is not an MethodDeclaration", this));
throw new IllegalStateException(f("%s is not MethodDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand All @@ -235,7 +235,7 @@ public boolean isTypeDeclaration() {

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public TypeDeclaration asTypeDeclaration() {
throw new IllegalStateException(f("%s is not an TypeDeclaration", this));
throw new IllegalStateException(f("%s is not TypeDeclaration, it is %s", this, this.getClass().getSimpleName()));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
Expand Down
Loading

0 comments on commit 0a678a5

Please sign in to comment.