diff --git a/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java b/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java index 1533a95..9dfa4ea 100644 --- a/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java +++ b/src/main/java/com/caoccao/javet/swc4j/ast/interfaces/ISwc4jAst.java @@ -64,7 +64,7 @@ default Optional eval() { * @return the list of AST nodes * @since 1.3.0 */ - default List find(Class clazz) { + default List find(Class clazz) { return find(clazz, -1); } @@ -77,12 +77,13 @@ default List find(Class clazz) { * @return the list of AST nodes * @since 1.3.0 */ - default List find(Class clazz, int depth) { + @SuppressWarnings("unchecked") + default List find(Class clazz, int depth) { AssertionUtils.notNull(clazz, "Class"); - List nodes = SimpleList.of(); + List nodes = SimpleList.of(); getChildNodes().forEach((childNode) -> { if (clazz.isAssignableFrom(childNode.getClass())) { - nodes.add(childNode); + nodes.add((T) childNode); } if (depth != 0) { final int newDepth = depth > 0 ? depth - 1 : depth; diff --git a/src/test/java/com/caoccao/javet/swc4j/ast/expr/TestSwc4jAstBinExpr.java b/src/test/java/com/caoccao/javet/swc4j/ast/expr/TestSwc4jAstBinExpr.java index f14f1bc..c08bd73 100644 --- a/src/test/java/com/caoccao/javet/swc4j/ast/expr/TestSwc4jAstBinExpr.java +++ b/src/test/java/com/caoccao/javet/swc4j/ast/expr/TestSwc4jAstBinExpr.java @@ -19,7 +19,6 @@ import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst; import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp; import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType; -import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst; import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript; import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt; import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitor; @@ -91,9 +90,9 @@ public void testGetBangCount() throws Swc4jCoreException { String code = entry.getKey(); int bangCount = entry.getValue(); Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions); - List nodes = output.getProgram().find(Swc4jAstBinExpr.class); + List nodes = output.getProgram().find(Swc4jAstBinExpr.class); assertEquals(1, nodes.size()); - Swc4jAstBinExpr binExpr = nodes.get(0).as(Swc4jAstBinExpr.class); + Swc4jAstBinExpr binExpr = nodes.get(0); assertEquals("a", binExpr.getLeft().as(Swc4jAstIdent.class).getSym()); assertEquals("b", binExpr.getRight().as(Swc4jAstIdent.class).getSym()); assertEquals(bangCount, binExpr.getBangCount()); diff --git a/src/test/java/com/caoccao/javet/swc4j/ast/expr/lit/TestSwc4jAstNumber.java b/src/test/java/com/caoccao/javet/swc4j/ast/expr/lit/TestSwc4jAstNumber.java index 55a18cd..b334ab6 100644 --- a/src/test/java/com/caoccao/javet/swc4j/ast/expr/lit/TestSwc4jAstNumber.java +++ b/src/test/java/com/caoccao/javet/swc4j/ast/expr/lit/TestSwc4jAstNumber.java @@ -18,7 +18,6 @@ import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst; import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType; -import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst; import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript; import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstExprStmt; import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException; @@ -88,9 +87,9 @@ public void testGetMinusCount() throws Swc4jCoreException { String code = entry.getKey(); int minusCount = entry.getValue(); Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions); - List nodes = output.getProgram().find(Swc4jAstNumber.class); + List nodes = output.getProgram().find(Swc4jAstNumber.class); assertEquals(1, nodes.size()); - Swc4jAstNumber number = nodes.get(0).as(Swc4jAstNumber.class); + Swc4jAstNumber number = nodes.get(0); assertEquals(12345, number.asInt()); assertEquals("12345", number.getRaw().get()); assertEquals(minusCount, number.getMinusCount());