Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Enfore google java style #502

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,19 @@ configurations {
check.dependsOn jacocoTestReport

// TODO: fix code style in main and test source code
subprojects {
apply plugin: 'checkstyle'
checkstyle {
configFile rootProject.file("config/checkstyle/google_checks.xml")
toolVersion "8.20"
configProperties = [
"org.checkstyle.google.suppressionfilter.config": rootProject.file("config/checkstyle/suppressions.xml")]
ignoreFailures = false
}
}
checkstyle {
configFile file("config/checkstyle/checkstyle.xml")
}

checkstyleMain.ignoreFailures = false
checkstyleTest.ignoreFailures = true

Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {

dependencies {
compile "org.antlr:antlr4-runtime:4.7.1"
compile group: 'com.google.guava', name: 'guava', version:'23.0'
compile group: 'com.google.guava', name: 'guava', version: '23.0'

testCompile group: 'junit', name: 'junit', version: '4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,64 @@
import org.antlr.v4.runtime.misc.Interval;

/**
* Custom stream to convert character to upper case for case insensitive grammar before sending to lexer.
* Custom stream to convert character to upper case for case insensitive grammar before sending to
* lexer.
*/
public class CaseInsensitiveCharStream implements CharStream {

/** Character stream */
private final CharStream charStream;
/** Character stream. */
private final CharStream charStream;

public CaseInsensitiveCharStream(String sql) {
this.charStream = CharStreams.fromString(sql);
}
public CaseInsensitiveCharStream(String sql) {
this.charStream = CharStreams.fromString(sql);
}

@Override
public String getText(Interval interval) {
return charStream.getText(interval);
}
@Override
public String getText(Interval interval) {
return charStream.getText(interval);
}

@Override
public void consume() {
charStream.consume();
}
@Override
public void consume() {
charStream.consume();
}

@Override
public int LA(int i) {
int c = charStream.LA(i);
if (c <= 0) {
return c;
}
return Character.toUpperCase(c);
@Override
public int LA(int i) {
int c = charStream.LA(i);
if (c <= 0) {
return c;
}
return Character.toUpperCase(c);
}

@Override
public int mark() {
return charStream.mark();
}
@Override
public int mark() {
return charStream.mark();
}

@Override
public void release(int marker) {
charStream.release(marker);
}
@Override
public void release(int marker) {
charStream.release(marker);
}

@Override
public int index() {
return charStream.index();
}
@Override
public int index() {
return charStream.index();
}

@Override
public void seek(int index) {
charStream.seek(index);
}
@Override
public void seek(int index) {
charStream.seek(index);
}

@Override
public int size() {
return charStream.size();
}
@Override
public int size() {
return charStream.size();
}

@Override
public String getSourceName() {
return charStream.getSourceName();
}
@Override
public String getSourceName() {
return charStream.getSourceName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,63 @@

package com.amazon.opendistroforelasticsearch.sql.common.antlr;

import java.util.Locale;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.IntervalSet;

import java.util.Locale;

/**
* Syntax analysis error listener that handles any syntax error by throwing exception with useful information.
* Syntax analysis error listener that handles any syntax error by throwing exception with useful
* information.
*/
public class SyntaxAnalysisErrorListener extends BaseErrorListener {

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg,
RecognitionException e) {
@Override
public void syntaxError(
Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {

CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
Token offendingToken = (Token) offendingSymbol;
String query = tokens.getText();
CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
Token offendingToken = (Token) offendingSymbol;
String query = tokens.getText();

throw new RuntimeException(
String.format(Locale.ROOT,
"Failed to parse query due to offending symbol [%s] at: '%s' <--- HERE... More details: %s",
getOffendingText(offendingToken),
truncateQueryAtOffendingToken(query, offendingToken),
getDetails(recognizer, msg, e)
)
);
}
throw new RuntimeException(
String.format(
Locale.ROOT,
"Failed to parse query due to offending symbol [%s] "
+ "at: '%s' <--- HERE... More details: %s",
getOffendingText(offendingToken),
truncateQueryAtOffendingToken(query, offendingToken),
getDetails(recognizer, msg, e)));
}

private String getOffendingText(Token offendingToken) {
return offendingToken.getText();
}
private String getOffendingText(Token offendingToken) {
return offendingToken.getText();
}

private String truncateQueryAtOffendingToken(String query, Token offendingToken) {
return query.substring(0, offendingToken.getStopIndex() + 1);
}
private String truncateQueryAtOffendingToken(String query, Token offendingToken) {
return query.substring(0, offendingToken.getStopIndex() + 1);
}

/**
* As official JavaDoc says, e=null means parser was able to recover from the error.
* In other words, "msg" argument includes the information we want.
*/
private String getDetails(Recognizer<?, ?> recognizer, String msg, RecognitionException e) {
String details;
if (e == null) {
details = msg;
} else {
IntervalSet followSet = e.getExpectedTokens();
details = "Expecting tokens in " + followSet.toString(recognizer.getVocabulary());
}
return details;
/**
* As official JavaDoc says, e=null means parser was able to recover from the error. In other
* words, "msg" argument includes the information we want.
*/
private String getDetails(Recognizer<?, ?> recognizer, String msg, RecognitionException e) {
String details;
if (e == null) {
details = msg;
} else {
IntervalSet followSet = e.getExpectedTokens();
details = "Expecting tokens in " + followSet.toString(recognizer.getVocabulary());
}

return details;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@
package com.amazon.opendistroforelasticsearch.sql.common.response;

/**
* Response listener for response post-processing callback.
* This is necessary because execution engine may schedule and execute in different thread.
* Response listener for response post-processing callback. This is necessary because execution
* engine may schedule and execute in different thread.
*
* @param <Response> response class
* @param <R> response class
*/
public interface ResponseListener<Response> {
public interface ResponseListener<R> {

/**
* Handle successful response.
* @param response successful response
*/
void onResponse(Response response);

/**
* Handle failed response.
* @param e exception captured
*/
void onFailure(Exception e);
/**
* Handle successful response.
*
* @param response successful response
*/
void onResponse(R response);

/**
* Handle failed response.
*
* @param e exception captured
*/
void onFailure(Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,35 @@
import com.google.common.base.Strings;

public class StringUtils {
/**
* @param text string
* @param mark quotation mark
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been removed
*/
public static String unquoteIdentifier(String text, String mark) {
if (isQuoted(text, mark)) {
return text.substring(mark.length(), text.length() - mark.length());
}
return text;
/**
* Unquote Identifier with mark.
* @param text string
* @param mark quotation mark
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been
* removed
*/
public static String unquoteIdentifier(String text, String mark) {
if (isQuoted(text, mark)) {
return text.substring(mark.length(), text.length() - mark.length());
}
return text;
}

public static String unquoteIdentifier(String text) {
if (isQuoted(text, "\"") || isQuoted(text, "'") || isQuoted(text, "`")) {
return text.substring(1, text.length() - 1);
} else {
return text;
}
/**
* Unquote Identifier which has " or ' or ` as mark.
* @param text string
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been
* removed
*/
public static String unquoteIdentifier(String text) {
if (isQuoted(text, "\"") || isQuoted(text, "'") || isQuoted(text, "`")) {
return text.substring(1, text.length() - 1);
} else {
return text;
}
}

private static boolean isQuoted(String text, String mark) {
return !Strings.isNullOrEmpty(text) && text.startsWith(mark) && text.endsWith(mark);
}
private static boolean isQuoted(String text, String mark) {
return !Strings.isNullOrEmpty(text) && text.startsWith(mark) && text.endsWith(mark);
}
}