Skip to content

Commit

Permalink
InvalidInputException: fix "illegally instantiates" warnings
Browse files Browse the repository at this point in the history
InvalidInputException is marked @noinstantiate and should only be
constructed by Scannner
  • Loading branch information
EcljpseB0T authored and jukzi committed Nov 3, 2023
1 parent b5e9b87 commit ef6ff8a
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.util.Set;

import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

Expand Down Expand Up @@ -69,7 +68,7 @@ public TypeBinding clone(TypeBinding enclosingType) {
}

@Override
protected MethodBinding[] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidInputException {
protected MethodBinding[] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidBindingException {
int typesLength = this.intersectingTypes.length;
MethodBinding[][] methods = new MethodBinding[typesLength][];
int contractsLength = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.Set;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching;
import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
Expand Down Expand Up @@ -1702,7 +1701,7 @@ public RecordComponentBinding[] unResolvedComponents() {
}

@Override
protected MethodBinding[] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidInputException {
protected MethodBinding[] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidBindingException {
if (replaceWildcards) {
TypeBinding[] types = getNonWildcardParameters(scope);
if (types == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import java.util.Comparator;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.internal.compiler.ast.LambdaExpression;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.NullAnnotationMatching;
Expand Down Expand Up @@ -2221,10 +2220,10 @@ protected int applyCloseableInterfaceWhitelists() {
return 0;
}

protected MethodBinding [] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidInputException {
protected MethodBinding [] getInterfaceAbstractContracts(Scope scope, boolean replaceWildcards, boolean filterDefaultMethods) throws InvalidBindingException {

if (!isInterface() || !isValidBinding()) {
throw new InvalidInputException("Not a functional interface"); //$NON-NLS-1$
throw new InvalidBindingException("Not a functional interface"); //$NON-NLS-1$
}

MethodBinding [] methods = methods();
Expand All @@ -2251,7 +2250,7 @@ protected int applyCloseableInterfaceWhitelists() {
if (method == null || method.isStatic() || method.redeclaresPublicObjectMethod(scope) || method.isPrivate())
continue;
if (!method.isValidBinding())
throw new InvalidInputException("Not a functional interface"); //$NON-NLS-1$
throw new InvalidBindingException("Not a functional interface"); //$NON-NLS-1$
for (int j = 0; j < contractsCount;) {
if ( contracts[j] != null && MethodVerifier.doesMethodOverride(method, contracts[j], environment)) {
contractsCount--;
Expand Down Expand Up @@ -2344,7 +2343,7 @@ public MethodBinding getSingleAbstractMethod(Scope scope, boolean replaceWildcar
return this.singleAbstractMethod[index] = samProblemBinding;
}
}
} catch (InvalidInputException e) {
} catch (InvalidBindingException e) {
return this.singleAbstractMethod[index] = samProblemBinding;
}
if (methods.length == 1)
Expand Down Expand Up @@ -2492,4 +2491,11 @@ public boolean hasEnclosingInstanceContext() {
return !enclosingMethod.isStatic();
return false;
}
static class InvalidBindingException extends Exception {
private static final long serialVersionUID = 1L;

InvalidBindingException(String message) {
super(message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ protected Object parseArguments(Object receiver, boolean checkVerifySpaceOrEndCo
}

// Something wrong happened => Invalid input
throw new InvalidInputException();
throw Scanner.invalidInput();
} finally {
// we have to make sure that this is reset to the previous value even if an exception occurs
this.scanner.tokenizeWhiteSpace = tokenWhiteSpace;
Expand Down Expand Up @@ -1175,11 +1175,11 @@ protected Object parseQualifiedName(boolean reset, boolean allowModule) throws I
break;

case TerminalTokens.TokenNameRestrictedIdentifierYield:
throw new InvalidInputException(); // unexpected.
throw Scanner.invalidInput(); // unexpected.

case TerminalTokens.TokenNameDOT :
if ((iToken & 1) == 0) { // dots must be even tokens
throw new InvalidInputException();
throw Scanner.invalidInput();
}
consumeToken();
break;
Expand Down Expand Up @@ -1246,7 +1246,7 @@ protected Object parseQualifiedName(boolean reset, boolean allowModule) throws I
case TerminalTokens.TokenNameDIVIDE:
if (parsingJava15Plus && lookForModule) {
if (((iToken & 1) == 0) || (moduleRefTokenCount > 0)) { // '/' must be even token
throw new InvalidInputException();
throw Scanner.invalidInput();
}
moduleRefTokenCount = (iToken+1) / 2;
consumeToken();
Expand Down Expand Up @@ -1284,7 +1284,7 @@ protected Object parseQualifiedName(boolean reset, boolean allowModule) throws I
}
// $FALL-THROUGH$ - fall through default case to raise exception
default:
throw new InvalidInputException();
throw Scanner.invalidInput();
}
}
break nextToken;
Expand Down Expand Up @@ -1505,7 +1505,7 @@ protected boolean parseSnippet() throws InvalidInputException {
boolean parsingJava18Plus = this.scanner != null ? this.scanner.sourceLevel >= ClassFileConstants.JDK18 : false;
boolean valid = true;
if (!parsingJava18Plus) {
throw new InvalidInputException();
throw Scanner.invalidInput();
}
Object snippetTag = null;
this.nonRegionTagCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected Object createArgumentReference(char[] name, int dim, boolean isVarargs
return new JavadocArgumentExpression(name, argTypeRef.sourceStart, argEnd, argTypeRef);
}
catch (ClassCastException ex) {
throw new InvalidInputException();
throw Scanner.invalidInput();
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ protected Object createFieldReference(Object receiver) throws InvalidInputExcept
return field;
}
catch (ClassCastException ex) {
throw new InvalidInputException();
throw Scanner.invalidInput();
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ protected Object createMethodReference(Object receiver, List arguments) throws I
}
}
} else {
throw new InvalidInputException();
throw Scanner.invalidInput();
}
}
// Create node
Expand Down Expand Up @@ -336,7 +336,7 @@ protected Object createMethodReference(Object receiver, List arguments) throws I
}
}
catch (ClassCastException ex) {
throw new InvalidInputException();
throw Scanner.invalidInput();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
public class JavadocScanner extends Scanner{

public boolean tokenizeSingleQuotes = false;

public boolean considerRegexInStringLiteral = false;

public JavadocScanner() {
this(false /*comment*/, false /*whitespace*/, false /*nls*/, ClassFileConstants.JDK1_3 /*sourceLevel*/, null/*taskTag*/, null/*taskPriorities*/, true /*taskCaseSensitive*/);
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public JavadocScanner(
taskPriorities,
isTaskCaseSensitive,
isPreviewEnabled);

}

public JavadocScanner(
Expand All @@ -94,7 +94,7 @@ public JavadocScanner(
isTaskCaseSensitive,
false);
}

public JavadocScanner(
boolean tokenizeComments,
boolean tokenizeWhiteSpace,
Expand Down Expand Up @@ -144,7 +144,7 @@ protected int scanForStringLiteral() throws InvalidInputException {
while (this.currentCharacter != '"') {
boolean isRegex = false;
if (this.currentPosition >= this.eofPosition) {
throw new InvalidInputException(UNTERMINATED_STRING);
throw unterminatedString();
}
/**** \r and \n are not valid in string literals ****/
if ((this.currentCharacter == '\n') || (this.currentCharacter == '\r')) {
Expand All @@ -167,13 +167,13 @@ protected int scanForStringLiteral() throws InvalidInputException {
break;
}
if (this.currentCharacter == '\"') {
throw new InvalidInputException(INVALID_CHAR_IN_STRING);
throw invalidCharInString();
}
}
} else {
this.currentPosition--; // set current position on new line character
}
throw new InvalidInputException(INVALID_CHAR_IN_STRING);
throw invalidCharInString();
}
if (this.currentCharacter == '\\') {
if (this.unicodeAsBackSlash) {
Expand Down Expand Up @@ -225,7 +225,7 @@ protected int scanForStringLiteral() throws InvalidInputException {
}
} catch (IndexOutOfBoundsException e) {
this.currentPosition--;
throw new InvalidInputException(UNTERMINATED_STRING);
throw unterminatedString();
} catch (InvalidInputException e) {
if (e.getMessage().equals(INVALID_ESCAPE)) {
// relocate if finding another quote fairly close: thus unicode '/u000D' will be fully consumed
Expand All @@ -246,9 +246,9 @@ protected int scanForStringLiteral() throws InvalidInputException {
return TokenNameStringLiteral;
} else {
return super.scanForStringLiteral();
}
}
}

@Override
protected int processSingleQuotes(boolean checkIfUnicode) throws InvalidInputException{
if (this.tokenizeSingleQuotes) {
Expand Down Expand Up @@ -278,7 +278,7 @@ protected int scanForSingleQuoteStringLiteral() throws InvalidInputException {
while (this.currentCharacter != '\'') {
boolean isRegex = false;
if (this.currentPosition >= this.eofPosition) {
throw new InvalidInputException(UNTERMINATED_STRING);
throw unterminatedString();
}
/**** \r and \n are not valid in string literals ****/
if ((this.currentCharacter == '\n') || (this.currentCharacter == '\r')) {
Expand All @@ -301,13 +301,13 @@ protected int scanForSingleQuoteStringLiteral() throws InvalidInputException {
break;
}
if (this.currentCharacter == '\'') {
throw new InvalidInputException(INVALID_CHAR_IN_STRING);
throw invalidCharInString();
}
}
} else {
this.currentPosition--; // set current position on new line character
}
throw new InvalidInputException(INVALID_CHAR_IN_STRING);
throw invalidCharInString();
}
if (this.currentCharacter == '\\') {
if (this.unicodeAsBackSlash) {
Expand Down Expand Up @@ -361,7 +361,7 @@ protected int scanForSingleQuoteStringLiteral() throws InvalidInputException {
}
} catch (IndexOutOfBoundsException e) {
this.currentPosition--;
throw new InvalidInputException(UNTERMINATED_STRING);
throw unterminatedString();
} catch (InvalidInputException e) {
if (e.getMessage().equals(INVALID_ESCAPE)) {
// relocate if finding another quote fairly close: thus unicode '/u000D' will be fully consumed
Expand All @@ -379,7 +379,7 @@ protected int scanForSingleQuoteStringLiteral() throws InvalidInputException {
}
throw e; // rethrow
}
return TokenNameSingleQuoteStringLiteral;
return TokenNameSingleQuoteStringLiteral;
}

protected boolean scanRegexCharacter() {
Expand Down
Loading

0 comments on commit ef6ff8a

Please sign in to comment.