Skip to content

Commit 4c01e45

Browse files
author
Sebastian Höfer
committed
Eliminate Java 9 language components to be JRE8 compatible
1 parent 8474ec2 commit 4c01e45

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

src/main/java/org/jqassistant/contrib/plugin/javascript/scanner/listener/JavaScriptListener.java

+24-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Iterator;
77
import java.util.List;
88
import java.util.ListIterator;
9+
import java.util.NoSuchElementException;
910
import java.util.Optional;
1011
import java.util.stream.Collectors;
1112
import java.util.stream.IntStream;
@@ -47,6 +48,7 @@
4748
import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.ObjectStoreHelper;
4849
import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.ParameterStoreHelper;
4950
import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.PrimitiveStoreHelper;
51+
import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.StreamHelper;
5052
import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.VariableStoreHelper;
5153

5254
import com.buschmais.jqassistant.core.store.api.Store;
@@ -172,38 +174,37 @@ public void exitMethodDefinition(MethodDefinitionContext ctx) {
172174
artifactStack.pop();
173175
}
174176

175-
private FunctionDescriptor getLastDefinedFunction() {
177+
@Override
178+
public void enterFormalParameterArg(FormalParameterArgContext ctx) {
179+
int countParamtersBefore = countLastParameters().intValue();
180+
FunctionDescriptor function = getLastDefinedFunction();
181+
FunctionParameterDescriptor ecmaParam = new ParameterStoreHelper(countParamtersBefore).createNodeIn(store, ctx);
182+
setQualifiedName(function, ecmaParam, ecmaParam.getName());
183+
amendStack(ecmaParam);
184+
function.getParameters().add(ecmaParam);
185+
}
186+
187+
private Long countLastParameters() {
176188
// https://stackoverflow.com/questions/24010109/java-8-stream-reverse-order
177189
ListIterator<JavaScriptDescriptor> listIterator = artifacts.listIterator(artifacts.size());
178-
return Stream.generate(listIterator::previous)
179-
.limit(artifacts.size())
180-
.filter((e) -> e instanceof FunctionDescriptor)
181-
.map((e) -> (FunctionDescriptor)e)
182-
.findFirst()
183-
.orElseThrow();
190+
Stream<JavaScriptDescriptor> reverseParamterStream = Stream.generate(listIterator::previous).limit(artifacts.size());
191+
return StreamHelper.takeWhile(reverseParamterStream, (desc) -> desc instanceof FunctionParameterDescriptor)
192+
.collect(Collectors.counting());
193+
184194

185-
186195
}
187196

188-
private Long countLastParameters() {
197+
private FunctionDescriptor getLastDefinedFunction() {
189198
// https://stackoverflow.com/questions/24010109/java-8-stream-reverse-order
190199
ListIterator<JavaScriptDescriptor> listIterator = artifacts.listIterator(artifacts.size());
191200
return Stream.generate(listIterator::previous)
192-
.limit(artifacts.size())
193-
.takeWhile((desc) -> desc instanceof FunctionParameterDescriptor) // JAVA 9
194-
.collect(Collectors.counting());
201+
.limit(artifacts.size())
202+
.filter((e) -> e instanceof FunctionDescriptor)
203+
.map((e) -> (FunctionDescriptor)e)
204+
.findFirst()
205+
.orElseThrow(() -> new NoSuchElementException("the last function wasn't found")); // Function not found shoudn't be happening
206+
195207

196-
197-
}
198-
199-
@Override
200-
public void enterFormalParameterArg(FormalParameterArgContext ctx) {
201-
int countParamtersBefore = countLastParameters().intValue();
202-
FunctionDescriptor function = getLastDefinedFunction();
203-
FunctionParameterDescriptor ecmaParam = new ParameterStoreHelper(countParamtersBefore).createNodeIn(store, ctx);
204-
setQualifiedName(function, ecmaParam, ecmaParam.getName());
205-
amendStack(ecmaParam);
206-
function.getParameters().add(ecmaParam);
207208
}
208209

209210
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers;
2+
3+
import java.util.Spliterator;
4+
import java.util.Spliterators;
5+
import java.util.function.Consumer;
6+
import java.util.function.Predicate;
7+
import java.util.stream.Stream;
8+
import java.util.stream.StreamSupport;
9+
10+
public class StreamHelper {
11+
12+
// https://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate
13+
public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<? super T> predicate) {
14+
return StreamSupport.stream(takeWhile(stream.spliterator(), predicate), false);
15+
}
16+
17+
private static <T> Spliterator<T> takeWhile(Spliterator<T> splitr, Predicate<? super T> predicate) {
18+
return new Spliterators.AbstractSpliterator<T>(splitr.estimateSize(), 0) {
19+
boolean stillGoing = true;
20+
21+
@Override
22+
public boolean tryAdvance(Consumer<? super T> consumer) {
23+
if (stillGoing) {
24+
boolean hadNext = splitr.tryAdvance(elem -> {
25+
if (predicate.test(elem)) {
26+
consumer.accept(elem);
27+
} else {
28+
stillGoing = false;
29+
}
30+
});
31+
return hadNext && stillGoing;
32+
}
33+
return false;
34+
}
35+
};
36+
}
37+
}

0 commit comments

Comments
 (0)