提交 84fd6dae authored 作者: Thomas Mueller's avatar Thomas Mueller

Merge pull request #197 from bedla/ISSUE-132

ISSUE-132 - SourceCompiler throws syntax error on javac warning
......@@ -5,6 +5,7 @@
*/
package org.h2.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
......@@ -13,6 +14,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
......@@ -361,9 +363,22 @@ public class SourceCompiler {
}
private static void handleSyntaxError(String output) {
if (output.startsWith("Note:") || output.startsWith("warning:")) {
boolean syntaxError = false;
final BufferedReader reader = new BufferedReader(new StringReader(output));
try {
for (String line; (line = reader.readLine()) != null; ) {
if (line.startsWith("Note:") || line.startsWith("warning:")) {
// just a warning (e.g. unchecked or unsafe operations)
} else if (output.length() > 0) {
} else {
syntaxError = true;
break;
}
}
} catch (IOException ignored) {
// exception ignored
}
if (syntaxError) {
output = StringUtils.replaceAll(output, COMPILE_DIR, "");
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, output);
}
......
org.h2.test.ap.TestAnnotationProcessor
\ No newline at end of file
package org.h2.test.ap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
public class TestAnnotationProcessor extends AbstractProcessor {
public static final String MESSAGES_KEY = TestAnnotationProcessor.class.getName() + "-messages";
public Set<String> getSupportedAnnotationTypes() {
for (OutputMessage outputMessage : findMessages()) {
processingEnv.getMessager().printMessage(outputMessage.kind, outputMessage.message);
}
return Collections.emptySet();
}
private List<OutputMessage> findMessages() {
final String messagesStr = System.getProperty(MESSAGES_KEY);
if (messagesStr == null || messagesStr.isEmpty()) {
return Collections.emptyList();
} else {
final List<OutputMessage> outputMessages = new ArrayList<OutputMessage>();
for (String msg : messagesStr.split("\\|")) {
final String[] split = msg.split(",");
if (split.length == 2) {
outputMessages.add(new OutputMessage(Diagnostic.Kind.valueOf(split[0]), split[1]));
} else {
throw new IllegalStateException("Unable to parse messages definition for: '" + messagesStr + "'");
}
}
return outputMessages;
}
}
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_6;
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
private static class OutputMessage {
public final Diagnostic.Kind kind;
public final String message;
private OutputMessage(Diagnostic.Kind kind, String message) {
this.kind = kind;
this.message = message;
}
}
}
......@@ -41,8 +41,10 @@ import org.h2.api.Aggregate;
import org.h2.api.AggregateFunction;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.jdbc.JdbcSQLException;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
import org.h2.test.ap.TestAnnotationProcessor;
import org.h2.tools.SimpleResultSet;
import org.h2.util.IOUtils;
import org.h2.util.New;
......@@ -103,6 +105,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testTranslate();
testGenerateSeries();
testFileWrite();
testAnnotationProcessorsOutput();
deleteDb("functions");
}
......@@ -1163,7 +1166,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
call.execute();
assertEquals(Integer[].class.getName(), call.getArray(1).getArray()
.getClass().getName());
assertEquals(new Integer[] { 2, 1 }, (Integer[]) call.getObject(1));
assertEquals(new Integer[]{2, 1}, (Integer[]) call.getObject(1));
stat.execute("drop alias array_test");
......@@ -1778,6 +1781,83 @@ public class TestFunctions extends TestBase implements AggregateFunction {
conn.close();
}
private void testAnnotationProcessorsOutput() throws SQLException {
testAnnotationProcessorsOutput_emptyKey();
testAnnotationProcessorsOutput_invalidKey();
testAnnotationProcessorsOutput_oneInvalidKey();
testAnnotationProcessorsOutput_warnAndError();
}
private void testAnnotationProcessorsOutput_emptyKey() throws SQLException {
try {
System.setProperty(TestAnnotationProcessor.MESSAGES_KEY, "");
callCompiledFunction("test_atp_empty_key");
} finally {
System.clearProperty(TestAnnotationProcessor.MESSAGES_KEY);
}
}
private void testAnnotationProcessorsOutput_invalidKey() throws SQLException {
try {
System.setProperty(TestAnnotationProcessor.MESSAGES_KEY, "invalid");
callCompiledFunction("test_atp_invalid_key");
fail();
} catch (JdbcSQLException e) {
assertEquals(ErrorCode.SYNTAX_ERROR_1, e.getErrorCode());
assertContains(e.getMessage(), "'invalid'");
} finally {
System.clearProperty(TestAnnotationProcessor.MESSAGES_KEY);
}
}
private void testAnnotationProcessorsOutput_oneInvalidKey() throws SQLException {
try {
System.setProperty(TestAnnotationProcessor.MESSAGES_KEY, "invalid,foo");
callCompiledFunction("test_atp_one_invalid_key");
fail();
} catch (JdbcSQLException e) {
assertEquals(ErrorCode.SYNTAX_ERROR_1, e.getErrorCode());
assertContains(e.getMessage(), "enum");
assertContains(e.getMessage(), "Kind.invalid");
} finally {
System.clearProperty(TestAnnotationProcessor.MESSAGES_KEY);
}
}
private void testAnnotationProcessorsOutput_warnAndError() throws SQLException {
try {
System.setProperty(TestAnnotationProcessor.MESSAGES_KEY, "WARNING,foo1|ERROR,foo2");
callCompiledFunction("test_atp_warn_and_error");
fail();
} catch (JdbcSQLException e) {
assertEquals(ErrorCode.SYNTAX_ERROR_1, e.getErrorCode());
assertContains(e.getMessage(), "foo1");
assertContains(e.getMessage(), "foo2");
} finally {
System.clearProperty(TestAnnotationProcessor.MESSAGES_KEY);
}
}
private void callCompiledFunction(String functionName) throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("create alias " + functionName + " AS "
+ "$$ boolean " + functionName + "() "
+ "{ return true; } $$;");
PreparedStatement stmt = conn.prepareStatement(
"select " + functionName + "() from dual");
rs = stmt.executeQuery();
rs.next();
assertEquals(Boolean.class.getName(), rs.getObject(1).getClass().getName());
stat.execute("drop alias " + functionName + "");
conn.close();
}
private void assertCallResult(String expected, Statement stat, String sql)
throws SQLException {
ResultSet rs = stat.executeQuery("CALL " + sql);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论