aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java8
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java6
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java36
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/CodeReader.java5
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java19
-rw-r--r--sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChunker.java2
-rw-r--r--sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChunkerTest.java46
7 files changed, 44 insertions, 78 deletions
diff --git a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java
index 032f2d67ef6..2dd85d7b424 100644
--- a/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java
+++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelCodeReaderFilter.java
@@ -60,7 +60,7 @@ public final class ChannelCodeReaderFilter<OUTPUT> extends CodeReaderFilter<OUTP
}
/**
- * ${@inheritDoc}
+ * {@inheritDoc}
*/
@Override
public void setReader(Reader reader) {
@@ -69,11 +69,13 @@ public final class ChannelCodeReaderFilter<OUTPUT> extends CodeReaderFilter<OUTP
}
/**
- * ${@inheritDoc}
+ * {@inheritDoc}
*/
@Override
public int read(char[] filteredBuffer, int offset, int length) throws IOException {
- if (internalCodeReader.peek() == -1) return -1;
+ if (internalCodeReader.peek() == -1) {
+ return -1;
+ }
int initialOffset = offset;
while (offset < filteredBuffer.length) {
if (internalCodeReader.peek() == -1) {
diff --git a/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java b/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java
index d21ac683abd..1936c0c1bb0 100644
--- a/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java
+++ b/sonar-channel/src/main/java/org/sonar/channel/ChannelDispatcher.java
@@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory;
public class ChannelDispatcher<OUTPUT> extends Channel<OUTPUT> {
- private static final Logger logger = LoggerFactory.getLogger(ChannelDispatcher.class);
+ private static final Logger LOG = LoggerFactory.getLogger(ChannelDispatcher.class);
private final boolean failIfNoChannelToConsumeOneCharacter;
@SuppressWarnings("rawtypes")
@@ -79,13 +79,13 @@ public class ChannelDispatcher<OUTPUT> extends Channel<OUTPUT> {
}
}
if ( !characterConsumed) {
- if (logger.isDebugEnabled() || failIfNoChannelToConsumeOneCharacter) {
+ if (LOG.isDebugEnabled() || failIfNoChannelToConsumeOneCharacter) {
String message = "None of the channel has been able to handle character '" + (char) code.peek() + "' (decimal value "
+ code.peek() + ") at line " + code.getLinePosition() + ", column " + code.getColumnPosition();
if (failIfNoChannelToConsumeOneCharacter) {
throw new IllegalStateException(message);
}
- logger.debug(message);
+ LOG.debug(message);
}
code.pop();
}
diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
index 2093644b756..705b287f7bb 100644
--- a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
+++ b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
@@ -53,40 +53,28 @@ public class CodeBuffer implements CharSequence {
this(new StringReader(code), configuration);
}
+ /**
+ * Note that this constructor will read everything from reader and will close it.
+ */
protected CodeBuffer(Reader initialCodeReader, CodeReaderConfiguration configuration) {
Reader reader = null;
-
+
try {
lastChar = -1;
cursor = new Cursor();
tabWidth = configuration.getTabWidth();
-
+
/* Setup the filters on the reader */
reader = initialCodeReader;
for (CodeReaderFilter<?> codeReaderFilter : configuration.getCodeReaderFilters()) {
reader = new Filter(reader, codeReaderFilter, configuration);
}
-
- fillBuffer(reader);
- }
- finally {
- if (reader != null) IOUtils.closeQuietly(reader);
- }
- }
-
- private void fillBuffer(Reader reader) {
- try {
- StringBuilder sb = new StringBuilder();
- char[] buffer = new char[4096];
- int read = reader.read(buffer);
- while(read != -1 && read != 0) {
- sb.append(buffer, 0, read);
- read = reader.read(buffer);
- }
- this.buffer = new char[sb.length()];
- sb.getChars(0, sb.length(), this.buffer, 0);
+
+ buffer = IOUtils.toCharArray(reader);
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new ChannelException(e.getMessage(), e);
+ } finally {
+ IOUtils.closeQuietly(reader);
}
}
@@ -243,6 +231,7 @@ public class CodeBuffer implements CharSequence {
return column;
}
+ @Override
public Cursor clone() {
Cursor clone = new Cursor();
clone.column = column;
@@ -272,7 +261,8 @@ public class CodeBuffer implements CharSequence {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
- return codeReaderFilter.read(cbuf, off, len);
+ int read = codeReaderFilter.read(cbuf, off, len);
+ return read == 0 ? -1 : read;
}
@Override
diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java
index 1033ad20668..0c09c9d4680 100644
--- a/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java
+++ b/sonar-channel/src/main/java/org/sonar/channel/CodeReader.java
@@ -47,6 +47,7 @@ public class CodeReader extends CodeBuffer {
/**
* Creates a code reader with specific configuration parameters.
+ * Note that this constructor will read everything from reader and will close it.
*
* @param code
* the Reader to read code from
@@ -123,7 +124,7 @@ public class CodeReader extends CodeBuffer {
}
/**
- * @deprecated use peekTo(EndMatcher matcher, Appendable appendable)
+ * @deprecated in 2.2, use {@link #peekTo(EndMatcher matcher, Appendable appendable)} instead
*/
@Deprecated
public final String peekTo(EndMatcher matcher) {
@@ -133,7 +134,7 @@ public class CodeReader extends CodeBuffer {
}
/**
- * @deprecated use popTo(Matcher matcher, Appendable appendable)
+ * @deprecated in 2.2, use {@link #popTo(Matcher matcher, Appendable appendable)} instead
*/
@Deprecated
public final void popTo(EndMatcher matcher, Appendable appendable) {
diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
index 387978fe069..b56b0fde1ea 100644
--- a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
+++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
@@ -257,11 +257,29 @@ public class CodeBufferTest {
assertThat(code.pop(), is( -1));
}
+ /**
+ * Backward compatibility with a COBOL plugin: filter returns 0 instead of -1, when end of the stream has been reached.
+ */
+ @Test(timeout = 1000)
+ public void testWrongEndOfStreamFilter() {
+ CodeReaderConfiguration configuration = new CodeReaderConfiguration();
+ configuration.setCodeReaderFilters(new WrongEndOfStreamFilter());
+ new CodeBuffer("foo", configuration);
+ }
+
+ class WrongEndOfStreamFilter extends CodeReaderFilter<Object> {
+ @Override
+ public int read(char[] filteredBuffer, int offset, int length) throws IOException {
+ return 0;
+ }
+ }
+
class ReplaceNumbersFilter extends CodeReaderFilter<Object> {
private Pattern pattern = Pattern.compile("\\d");
private String REPLACEMENT = "-";
+ @Override
public int read(char[] cbuf, int off, int len) throws IOException {
char[] tempBuffer = new char[cbuf.length];
int charCount = getReader().read(tempBuffer, off, len);
@@ -278,6 +296,7 @@ public class CodeBufferTest {
private Pattern pattern = Pattern.compile("[a-zA-Z]");
private String REPLACEMENT = "*";
+ @Override
public int read(char[] cbuf, int off, int len) throws IOException {
char[] tempBuffer = new char[cbuf.length];
int charCount = getReader().read(tempBuffer, off, len);
diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChunker.java b/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChunker.java
index ef5c0522dbb..d946811dacc 100644
--- a/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChunker.java
+++ b/sonar-duplications/src/main/java/org/sonar/duplications/token/TokenChunker.java
@@ -21,9 +21,9 @@ package org.sonar.duplications.token;
import java.io.Reader;
import java.io.StringReader;
+
import org.sonar.channel.ChannelDispatcher;
import org.sonar.channel.CodeReader;
-import org.sonar.channel.CodeReaderConfiguration;
import org.sonar.duplications.DuplicationsException;
public final class TokenChunker {
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChunkerTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChunkerTest.java
deleted file mode 100644
index d9a421ac4d0..00000000000
--- a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenChunkerTest.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.duplications.token;
-
-import org.junit.Test;
-
-public class TokenChunkerTest {
-
- /**
- * In fact this test does not guarantee that we will be able to consume even more great comments,
- * because {@link org.sonar.channel.CodeBuffer} does not expand dynamically - see issue SONAR-2632.
- * But at least guarantees that we able to consume source files from JDK 1.6,
- * because buffer capacity has been increased in comparison with default value,
- * which is {@link org.sonar.channel.CodeReaderConfiguration#DEFAULT_BUFFER_CAPACITY}.
- */
- @Test(timeout = 5000)
- public void shouldConsumeBigComments() {
- int capacity = 80000;
- StringBuilder sb = new StringBuilder(capacity);
- sb.append("/");
- for (int i = 3; i < capacity; i++) {
- sb.append('*');
- }
- sb.append("/");
- TokenChunker chunker = TokenChunker.builder().token("/.*/", "LITERAL").build();
- chunker.chunk(sb.toString());
- }
-
-}