Skip to content

Commit

Permalink
Make everything work again
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed May 19, 2017
1 parent 4f56498 commit 60df115
Show file tree
Hide file tree
Showing 19 changed files with 494 additions and 436 deletions.
Expand Up @@ -52,24 +52,8 @@ protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nod

body.addStatement("customInitialization();");

ConstructorDeclaration rangeConstructor = constructor.clone();
rangeConstructor.getParameter(0).setType(Range.class);

replaceWhenSameSignature(nodeCoid, rangeConstructor, constructor);
replaceWhenSameSignature(nodeCoid, constructor);
nodeCu.addImport(TokenRange.class);
annotateGenerated(constructor);
}

protected void replaceWhenSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callableWithSignature, CallableDeclaration<?> callableToReplaceWith) {
final List<CallableDeclaration<?>> existingCallables = containingClassOrInterface.getCallablesWithSignature(callableWithSignature.getSignature());
if (existingCallables.isEmpty()) {
throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but it wasn't there.", callableWithSignature.getSignature(), containingClassOrInterface.getNameAsString()));
}
if (existingCallables.size() > 1) {
throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but found more than one.", callableWithSignature.getSignature(), containingClassOrInterface.getNameAsString()));
}
final CallableDeclaration<?> existingCallable = existingCallables.get(0);
callableToReplaceWith.setJavadocComment(callableToReplaceWith.getJavadocComment().orElse(existingCallable.getJavadocComment().orElse(null)));
containingClassOrInterface.getMembers().replace(existingCallable, callableToReplaceWith);
}
}
Expand Up @@ -39,7 +39,7 @@ protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration
}

SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder(f("%s r = new %s(", node.getTypeNameGenerified(), node.getTypeNameGenerified()), ",", ");");
builder.append("n.getRange().orElse(null)");
builder.append("n.getTokenRange().orElse(null)");
for (PropertyMetaModel field : node.getConstructorParameters()) {
if (field.getName().equals("comment")) {
continue;
Expand Down
30 changes: 14 additions & 16 deletions javaparser-core/src/main/java/com/github/javaparser/JavaParser.java
Expand Up @@ -47,7 +47,6 @@
import static com.github.javaparser.ParseStart.*;
import static com.github.javaparser.Problem.PROBLEM_BY_BEGIN_POSITION;
import static com.github.javaparser.Providers.*;
import static com.github.javaparser.Range.range;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
Expand Down Expand Up @@ -112,8 +111,7 @@ private GeneratedJavaParser getParserForProvider(Provider provider) {
* The start indicates what can be found in the source code (compilation unit, block, import...)
*
* @param start refer to the constants in ParseStart to see what can be parsed.
* @param provider refer to Providers to see how you can read source.
* The provider will be closed after parsing.
* @param provider refer to Providers to see how you can read source. The provider will be closed after parsing.
* @param <N> the subclass of Node that is the result of parsing in the start.
* @return the parse result, a collection of encountered problems, and some extra data.
*/
Expand All @@ -134,9 +132,14 @@ public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provi
return new ParseResult<>(resultNode, parser.problems, parser.getTokens(),
parser.getCommentsCollection());
} catch (ParseException p) {
final Token token = p.currentToken;
final Range range = range(token.beginLine, token.beginColumn, token.endLine, token.endColumn);
parser.problems.add(new Problem(makeMessageForParseException(p), range, p));
TokenRange tokenRange = null;
if (p.currentToken != null) {
if (p.currentToken instanceof GeneratedJavaParser.CustomToken) {
final JavaToken token = ((GeneratedJavaParser.CustomToken) p.currentToken).javaToken;
tokenRange = new TokenRange(token, token);
}
}
parser.problems.add(new Problem(makeMessageForParseException(p), tokenRange, p));
return new ParseResult<>(null, parser.problems, parser.getTokens(), parser.getCommentsCollection());
} catch (Exception e) {
final String message = e.getMessage() == null ? "Unknown error" : e.getMessage();
Expand Down Expand Up @@ -213,8 +216,7 @@ private String makeMessageForParseException(ParseException exception) {
* Parses the Java code contained in the {@link InputStream} and returns a
* {@link CompilationUnit} that represents it.
*
* @param in {@link InputStream} containing Java source code.
* It will be closed after parsing.
* @param in {@link InputStream} containing Java source code. It will be closed after parsing.
* @param encoding encoding of the source code
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
Expand All @@ -228,8 +230,7 @@ public static CompilationUnit parse(final InputStream in, Charset encoding) {
* {@link CompilationUnit} that represents it.<br>
* Note: Uses UTF-8 encoding
*
* @param in {@link InputStream} containing Java source code.
* It will be closed after parsing.
* @param in {@link InputStream} containing Java source code. It will be closed after parsing.
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
*/
Expand All @@ -241,8 +242,7 @@ public static CompilationUnit parse(final InputStream in) {
* Parses the Java code contained in a {@link File} and returns a
* {@link CompilationUnit} that represents it.
*
* @param file {@link File} containing Java source code.
* It will be closed after parsing.
* @param file {@link File} containing Java source code. It will be closed after parsing.
* @param encoding encoding of the source code
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
Expand All @@ -257,8 +257,7 @@ public static CompilationUnit parse(final File file, final Charset encoding) thr
* {@link CompilationUnit} that represents it.<br>
* Note: Uses UTF-8 encoding
*
* @param file {@link File} containing Java source code.
* It will be closed after parsing.
* @param file {@link File} containing Java source code. It will be closed after parsing.
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
* @throws FileNotFoundException the file was not found
Expand Down Expand Up @@ -344,8 +343,7 @@ public static CompilationUnit parseResource(final ClassLoader classLoader, final
* Parses Java code from a Reader and returns a
* {@link CompilationUnit} that represents it.<br>
*
* @param reader the reader containing Java source code.
* It will be closed after parsing.
* @param reader the reader containing Java source code. It will be closed after parsing.
* @return CompilationUnit representing the Java source code
* @throws ParseProblemException if the source code has parser errors
*/
Expand Down
34 changes: 34 additions & 0 deletions javaparser-core/src/main/java/com/github/javaparser/JavaToken.java
Expand Up @@ -24,17 +24,29 @@
import java.util.List;
import java.util.Optional;

import static com.github.javaparser.Position.*;
import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* A token from a parsed source file.
* (Awkwardly named "Java"Token since JavaCC already generates an internal class Token.)
*/
public class JavaToken {
public static final JavaToken INVALID = new JavaToken();

private final Range range;
private final int kind;
private final String text;
private final Optional<JavaToken> previousToken;
private Optional<JavaToken> nextToken = Optional.empty();

private JavaToken() {
range = new Range(pos(-1,-1), pos(-1,-1));
kind = 0;
text = "INVALID";
previousToken = Optional.empty();
}

public JavaToken(Token token, List<JavaToken> tokens) {
Range range = Range.range(token.beginLine, token.beginColumn, token.endLine, token.endColumn);
String text = token.image;
Expand Down Expand Up @@ -114,4 +126,26 @@ public Optional<JavaToken> getPreviousToken() {
public String toString() {
return text;
}

/**
* Check if the position is usable. Does not know what it is pointing at, so it can't check if the position is after
* the end of the source.
*/
public boolean valid() {
return !invalid();
}

public boolean invalid() {
return this == INVALID;
}

public JavaToken orIfInvalid(JavaToken anotherToken) {
assertNotNull(anotherToken);
if (valid() || anotherToken.invalid()) {
return this;
}
return anotherToken;
}


}
12 changes: 6 additions & 6 deletions javaparser-core/src/main/java/com/github/javaparser/Problem.java
Expand Up @@ -32,10 +32,10 @@
*/
public class Problem {
private final String message;
private final Range location;
private final TokenRange location;
private final Throwable cause;

public Problem(String message, Range location, Throwable cause) {
public Problem(String message, TokenRange location, Throwable cause) {
assertNotNull(message);

this.message = message;
Expand Down Expand Up @@ -69,21 +69,21 @@ public String getMessage() {
* @return the message plus location information.
*/
public String getVerboseMessage() {
return getLocation().map(l -> l.begin + " " + message).orElse(message);
return getLocation().map(l -> l.getBegin().getRange().begin + " " + message).orElse(message);
}

/**
* @return the location that was passed into the constructor.
*/
public Optional<Range> getLocation() {
public Optional<TokenRange> getLocation() {
return Optional.ofNullable(location);
}

/**
* @deprecated use getLocation()
*/
@Deprecated
public Optional<Range> getRange() {
public Optional<TokenRange> getRange() {
return getLocation();
}

Expand All @@ -99,7 +99,7 @@ public Optional<Throwable> getCause() {
*/
public static Comparator<Problem> PROBLEM_BY_BEGIN_POSITION = (a, b) -> {
if (a.getLocation().isPresent() && b.getLocation().isPresent()) {
return a.getLocation().get().begin.compareTo(b.getLocation().get().begin);
return a.getLocation().get().getBegin().getRange().begin.compareTo(b.getLocation().get().getBegin().getRange().begin);
}
if (a.getLocation().isPresent() || b.getLocation().isPresent()) {
if (a.getLocation().isPresent()) {
Expand Down
@@ -1,15 +1,19 @@
package com.github.javaparser;

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

/**
* The range of tokens covered by this node.
*/
public class TokenRange {
public static final TokenRange INVALID = new TokenRange(JavaToken.INVALID, JavaToken.INVALID);

private final JavaToken begin;
private final JavaToken end;

public TokenRange(JavaToken begin, JavaToken end) {
this.begin = begin;
this.end = end;
this.begin = assertNotNull(begin);
this.end = assertNotNull(end);
}

public JavaToken getBegin() {
Expand All @@ -23,4 +27,12 @@ public JavaToken getEnd() {
public Range getRange() {
return new Range(begin.getRange().begin, end.getRange().end);
}

public TokenRange withBegin(JavaToken begin) {
return new TokenRange(assertNotNull(begin), end);
}

public TokenRange withEnd(JavaToken end) {
return new TokenRange(begin, assertNotNull(end));
}
}
22 changes: 16 additions & 6 deletions javaparser-core/src/main/java/com/github/javaparser/ast/Node.java
Expand Up @@ -27,6 +27,7 @@
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.nodeTypes.NodeWithRange;
import com.github.javaparser.ast.nodeTypes.NodeWithTokenRange;
import com.github.javaparser.ast.observer.AstObserver;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.observer.PropagatingAstObserver;
Expand All @@ -39,9 +40,9 @@
import com.github.javaparser.metamodel.NodeMetaModel;
import com.github.javaparser.printer.PrettyPrinter;
import com.github.javaparser.printer.PrettyPrinterConfiguration;
import javax.annotation.Generated;
import java.util.*;
import static java.util.Collections.unmodifiableList;
import javax.annotation.Generated;
import com.github.javaparser.ast.Node;

/**
Expand Down Expand Up @@ -87,7 +88,7 @@
*
* @author Julio Vilmar Gesser
*/
public abstract class Node implements Cloneable, HasParentNode<Node>, Visitable, NodeWithRange<Node> {
public abstract class Node implements Cloneable, HasParentNode<Node>, Visitable, NodeWithRange<Node>, NodeWithTokenRange<Node> {

/**
* Different registration mode for observers on nodes.
Expand Down Expand Up @@ -133,7 +134,7 @@ public enum ObserverRegistrationMode {
private Range range;

@InternalProperty
private final TokenRange tokenRange;
private TokenRange tokenRange;

@InternalProperty
private Node parentNode;
Expand All @@ -153,8 +154,7 @@ public enum ObserverRegistrationMode {
private List<AstObserver> observers = new ArrayList<>();

protected Node(TokenRange tokenRange) {
this.range = new Range(tokenRange.getBegin().getRange().begin, tokenRange.getEnd().getRange().end);
this.tokenRange = tokenRange;
setTokenRange(tokenRange);
}

/**
Expand Down Expand Up @@ -189,6 +189,16 @@ public Optional<TokenRange> getTokenRange() {
return Optional.ofNullable(tokenRange);
}

public Node setTokenRange(TokenRange tokenRange) {
this.tokenRange = tokenRange;
if (tokenRange == null) {
range = null;
} else {
range = new Range(tokenRange.getBegin().getRange().begin, tokenRange.getEnd().getRange().end);
}
return this;
}

/**
* @param range the range of characters in the source code that this node covers. null can be used to indicate that
* no range information is known, or that it is not of interest.
Expand Down Expand Up @@ -449,7 +459,7 @@ public boolean remove() {
* it will try to remove its parent instead,
* until it finds a node that can be removed,
* or no parent can be found.
*
* <p>
* Since everything at CompilationUnit level is removable,
* this method will only (silently) fail when the node is in a detached AST fragment.
*/
Expand Down
@@ -0,0 +1,18 @@
package com.github.javaparser.ast.nodeTypes;

import com.github.javaparser.Position;
import com.github.javaparser.Range;
import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.Node;

import java.util.Optional;

/**
* A node that has a Range, which is every Node.
*
*/
public interface NodeWithTokenRange<N> {
Optional<TokenRange> getTokenRange();

N setTokenRange(TokenRange range);
}
Expand Up @@ -33,13 +33,11 @@
import com.github.javaparser.metamodel.ArrayTypeMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.utils.Pair;

import javax.annotation.Generated;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static com.github.javaparser.ast.NodeList.nodeList;
import static com.github.javaparser.utils.Utils.assertNotNull;

Expand All @@ -60,9 +58,7 @@ public ArrayType(Type type, AnnotationExpr... annotations) {
this(type, nodeList(annotations));
}

/**
* This constructor is used by the parser and is considered private.
*/
/**This constructor is used by the parser and is considered private.*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public ArrayType(TokenRange tokenRange, Type componentType, NodeList<AnnotationExpr> annotations) {
super(tokenRange, annotations);
Expand Down

0 comments on commit 60df115

Please sign in to comment.