@Test public void testTokensNotNull() { final IllegalInstantiationCheck check = new IllegalInstantiationCheck(); Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens()); Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens()); Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens()); }
if (isSamePackage(className, pkgNameLen, illegal) || isStandardClass(className, illegal)) { fullClassName = illegal; fullClassName = checkImportStatements(className);
/** * Processes one of the collected "new" tokens when walking tree * has finished. * @param newTokenAst the "new" token. */ private void postProcessLiteralNew(DetailAST newTokenAst) { final DetailAST typeNameAst = newTokenAst.getFirstChild(); final AST nameSibling = typeNameAst.getNextSibling(); if (nameSibling.getType() != TokenTypes.ARRAY_DECLARATOR) { // ast != "new Boolean[]" final FullIdent typeIdent = FullIdent.createFullIdent(typeNameAst); final String typeName = typeIdent.getText(); final String fqClassName = getIllegalInstantiation(typeName); if (fqClassName != null) { log(newTokenAst, MSG_KEY, fqClassName); } } }
@Override public void visitToken(DetailAST ast) { switch (ast.getType()) { case TokenTypes.LITERAL_NEW: processLiteralNew(ast); break; case TokenTypes.PACKAGE_DEF: processPackageDef(ast); break; case TokenTypes.IMPORT: processImport(ast); break; case TokenTypes.CLASS_DEF: processClassDef(ast); break; default: throw new IllegalArgumentException("Unknown type " + ast); } }
@Override public int[] getDefaultTokens() { return getAcceptableTokens(); }
@Test public void testImproperToken() { final IllegalInstantiationCheck check = new IllegalInstantiationCheck(); final DetailAST lambdaAst = new DetailAST(); lambdaAst.setType(TokenTypes.LAMBDA); try { check.visitToken(lambdaAst); Assert.fail("IllegalArgumentException is expected"); } catch (IllegalArgumentException ex) { // it is OK } }
/** * We cannot reproduce situation when visitToken is called and leaveToken is not. * So, we have to use reflection to be sure that even in such situation * state of the field will be cleared. * * @throws Exception when code tested throws exception */ @Test public void testClearStateImports() throws Exception { final IllegalInstantiationCheck check = new IllegalInstantiationCheck(); final DetailAST root = JavaParser.parseFile(new File( getPath("InputIllegalInstantiationSemantic.java")), JavaParser.Options.WITHOUT_COMMENTS); final Optional<DetailAST> importDef = TestUtil.findTokenInAstByPredicate(root, ast -> ast.getType() == TokenTypes.IMPORT); Assert.assertTrue("Ast should contain IMPORT_DEF", importDef.isPresent()); Assert.assertTrue("State is not cleared on beginTree", TestUtil.isStatefulFieldClearedDuringBeginTree(check, importDef.get(), "imports", imports -> ((Collection<?>) imports).isEmpty())); }
@Override public void visitToken(DetailAST ast) { switch (ast.getType()) { case TokenTypes.LITERAL_NEW: processLiteralNew(ast); break; case TokenTypes.PACKAGE_DEF: processPackageDef(ast); break; case TokenTypes.IMPORT: processImport(ast); break; case TokenTypes.CLASS_DEF: processClassDef(ast); break; default: throw new IllegalArgumentException("Unknown type " + ast); } }
@Override public int[] getDefaultTokens() { return getAcceptableTokens(); }
/** * We cannot reproduce situation when visitToken is called and leaveToken is not. * So, we have to use reflection to be sure that even in such situation * state of the field will be cleared. * * @throws Exception when code tested throws exception */ @Test @SuppressWarnings("unchecked") public void testClearStateClassNames() throws Exception { final IllegalInstantiationCheck check = new IllegalInstantiationCheck(); final DetailAST root = JavaParser.parseFile( new File(getPath("InputIllegalInstantiationSemantic.java")), JavaParser.Options.WITHOUT_COMMENTS); final Optional<DetailAST> classDef = TestUtil.findTokenInAstByPredicate(root, ast -> ast.getType() == TokenTypes.CLASS_DEF); Assert.assertTrue("Ast should contain CLASS_DEF", classDef.isPresent()); Assert.assertTrue("State is not cleared on beginTree", TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(), "classNames", classNames -> ((Collection<String>) classNames).isEmpty())); }
if (isSamePackage(className, pkgNameLen, illegal) || isStandardClass(className, illegal)) { fullClassName = illegal; fullClassName = checkImportStatements(className);
/** * Processes one of the collected "new" tokens when walking tree * has finished. * @param newTokenAst the "new" token. */ private void postProcessLiteralNew(DetailAST newTokenAst) { final DetailAST typeNameAst = newTokenAst.getFirstChild(); final AST nameSibling = typeNameAst.getNextSibling(); if (nameSibling.getType() != TokenTypes.ARRAY_DECLARATOR) { // ast != "new Boolean[]" final FullIdent typeIdent = FullIdent.createFullIdent(typeNameAst); final String typeName = typeIdent.getText(); final String fqClassName = getIllegalInstantiation(typeName); if (fqClassName != null) { log(newTokenAst, MSG_KEY, fqClassName); } } }
/** * We cannot reproduce situation when visitToken is called and leaveToken is not. * So, we have to use reflection to be sure that even in such situation * state of the field will be cleared. * * @throws Exception when code tested throws exception */ @Test @SuppressWarnings("unchecked") public void testClearStateInstantiations() throws Exception { final IllegalInstantiationCheck check = new IllegalInstantiationCheck(); final DetailAST root = JavaParser.parseFile(new File( getNonCompilablePath("InputIllegalInstantiationLang.java")), JavaParser.Options.WITHOUT_COMMENTS); final Optional<DetailAST> literalNew = TestUtil.findTokenInAstByPredicate(root, ast -> ast.getType() == TokenTypes.LITERAL_NEW); Assert.assertTrue("Ast should contain LITERAL_NEW", literalNew.isPresent()); Assert.assertTrue("State is not cleared on beginTree", TestUtil.isStatefulFieldClearedDuringBeginTree(check, literalNew.get(), "instantiations", instantiations -> ((Collection<DetailAST>) instantiations).isEmpty())); } }