diff --git a/src/main/java/org/cqfn/astranaut/codegen/java/MatcherClassFiller.java b/src/main/java/org/cqfn/astranaut/codegen/java/MatcherClassFiller.java index a4d2441..ba4f755 100644 --- a/src/main/java/org/cqfn/astranaut/codegen/java/MatcherClassFiller.java +++ b/src/main/java/org/cqfn/astranaut/codegen/java/MatcherClassFiller.java @@ -41,6 +41,7 @@ * * @since 0.1.5 */ +@SuppressWarnings("PMD.TooManyMethods") public class MatcherClassFiller { /** * The 'String' type name. @@ -585,14 +586,15 @@ private void createMagicNumber(final String brief, final String name, final int * @return Source code */ private String createExtractorWithTypedHoles() { - final StringBuilder extractor = new StringBuilder(); - llist = true; - extractor.append("final LinkedList batch = new LinkedList<>(node.getChildrenList());\n"); + final StringBuilder extractor = new StringBuilder(128); + this.llist = true; + extractor.append( + "final LinkedList batch = new LinkedList<>(node.getChildrenList());\n" + ); for (final Parameter parameter : this.descriptor.getParameters()) { if (parameter instanceof Hole) { extractor.append(this.formatIteratedHoleExtractor((Hole) parameter)); - } - else if (parameter instanceof Descriptor) { + } else if (parameter instanceof Descriptor) { final String subclass = this.generator.generate((Descriptor) parameter); final List code = Arrays.asList( "if (result && !batch.isEmpty()) {", diff --git a/src/test/resources/codegen/java/matcher_generator_extract_particular_type.txt b/src/test/resources/codegen/java/matcher_generator_extract_particular_type.txt index e16a878..75e9fda 100644 --- a/src/test/resources/codegen/java/matcher_generator_extract_particular_type.txt +++ b/src/test/resources/codegen/java/matcher_generator_extract_particular_type.txt @@ -24,8 +24,8 @@ package org.uast; -import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedList; import java.util.List; import java.util.Map; import org.cqfn.astranaut.core.Matcher; @@ -53,14 +53,14 @@ public final class Matcher0 implements Matcher { private static final int FIRST_HOLE_ID = 1; /** - * The number of the second hole. + * The type of the first hole. */ - private static final int SECOND_HOLE_ID = 2; + private static final String FIRST_HOLE_TYPE = "CCC"; /** - * The index of the first child. + * The number of the second hole. */ - private static final int FIRST_CHILD_ID = 2; + private static final int SECOND_HOLE_ID = 2; /** * Constructor. @@ -72,22 +72,33 @@ public final class Matcher0 implements Matcher { public boolean match(final Node node, final Map> children, final Map data) { - final boolean result = node.belongsToGroup(Matcher0.EXPECTED_TYPE) - && Matcher1.INSTANCE.match(node.getChild(0), children, data); + boolean result = node.belongsToGroup(Matcher0.EXPECTED_TYPE); + final LinkedList batch = new LinkedList<>(node.getChildrenList()); + if (result && !batch.isEmpty()) { + result = Matcher1.INSTANCE.match(batch.pollFirst(), children, data); + } else { + result = false; + } if (result) { - final int count = node.getChildCount(); - final List list = new ArrayList<>(count - 1); - for (int index = 1; index < count; index = index + 1) { - final Node child = node.getChild(index); - if ("CCC".equals(child.getTypeName())) { + final List list = new LinkedList<>(); + while (!batch.isEmpty()) { + final Node child = batch.pollFirst(); + if (Matcher0.FIRST_HOLE_TYPE.equals(child.getTypeName())) { list.add(child); + } else { + batch.addFirst(child); + break; } } children.put(Matcher0.FIRST_HOLE_ID, list); + } + if (result && !batch.isEmpty()) { children.put( Matcher0.SECOND_HOLE_ID, - Collections.singletonList(node.getChild(Matcher0.FIRST_CHILD_ID)) + Collections.singletonList(batch.pollFirst()) ); + } else { + result = false; } return result; }