当前位置: 首页 > 知识库问答 >
问题:

如何使用Spoon从java类中删除注释?

姬锐
2023-03-14

public class CWE23_Relative_Path_Traversal__connect_tcp_01 extends AbstractTestCase
{
    /* uses badsource and badsink */
    public void bad() throws Throwable
    {
        String data;

        data = ""; /* Initialize data */

        /* Read data using an outbound tcp connection */
        {
            Socket socket = null;
            BufferedReader readerBuffered = null;
            InputStreamReader readerInputStream = null;

            try
            {
                /* Read data using an outbound tcp connection */
                socket = new Socket("host.example.org", 39544);

                /* read input from socket */

                readerInputStream = new InputStreamReader(socket.getInputStream(), "UTF-8");
                readerBuffered = new BufferedReader(readerInputStream);

                /* POTENTIAL FLAW: Read data using an outbound tcp connection */
                data = readerBuffered.readLine();
            }
            catch (IOException exceptIO)
            {
                IO.logger.log(Level.WARNING, "Error with stream reading", exceptIO);
            }
            finally
            {
                /* clean up stream reading objects */
                try
                {
                    if (readerBuffered != null)
                    {
                        readerBuffered.close();
                    }
                }
                catch (IOException exceptIO)
                {
                    IO.logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO);
                }

                try
                {
                    if (readerInputStream != null)
                    {
                        readerInputStream.close();
                    }
                }
                catch (IOException exceptIO)
                {
                    IO.logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO);
                }

                /* clean up socket objects */
                try
                {
                    if (socket != null)
                    {
                        socket.close();
                    }
                }
                catch (IOException exceptIO)
                {
                    IO.logger.log(Level.WARNING, "Error closing Socket", exceptIO);
                }
            }
        }

        String root;
        if(System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
        {
            /* running on Windows */
            root = "C:\\uploads\\";
        }
        else
        {
            /* running on non-Windows */
            root = "/home/user/uploads/";
        }

        if (data != null)
        {
            /* POTENTIAL FLAW: no validation of concatenated value */
            File file = new File(root + data);
            FileInputStream streamFileInputSink = null;
            InputStreamReader readerInputStreamSink = null;
            BufferedReader readerBufferdSink = null;
            if (file.exists() && file.isFile())
            {
                try
                {
                    streamFileInputSink = new FileInputStream(file);
                    readerInputStreamSink = new InputStreamReader(streamFileInputSink, "UTF-8");
                    readerBufferdSink = new BufferedReader(readerInputStreamSink);
                    IO.writeLine(readerBufferdSink.readLine());
                }
                catch (IOException exceptIO)
                {
                    IO.logger.log(Level.WARNING, "Error with stream reading", exceptIO);
                }
                finally
                {
                    /* Close stream reading objects */
                    try
                    {
                        if (readerBufferdSink != null)
                        {
                            readerBufferdSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO);
                    }

                    try
                    {
                        if (readerInputStreamSink != null)
                        {
                            readerInputStreamSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO);
                    }

                    try
                    {
                        if (streamFileInputSink != null)
                        {
                            streamFileInputSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing FileInputStream", exceptIO);
                    }
                }
            }
        }

    }

    public void good() throws Throwable
    {
        goodG2B();
    }

    /* goodG2B() - uses goodsource and badsink */
    private void goodG2B() throws Throwable
    {
        String data;

        /* FIX: Use a hardcoded string */
        data = "foo";

        String root;
        if(System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
        {
            /* running on Windows */
            root = "C:\\uploads\\";
        }
        else
        {
            /* running on non-Windows */
            root = "/home/user/uploads/";
        }

        if (data != null)
        {
            /* POTENTIAL FLAW: no validation of concatenated value */
            File file = new File(root + data);
            FileInputStream streamFileInputSink = null;
            InputStreamReader readerInputStreamSink = null;
            BufferedReader readerBufferdSink = null;
            if (file.exists() && file.isFile())
            {
                try
                {
                    streamFileInputSink = new FileInputStream(file);
                    readerInputStreamSink = new InputStreamReader(streamFileInputSink, "UTF-8");
                    readerBufferdSink = new BufferedReader(readerInputStreamSink);
                    IO.writeLine(readerBufferdSink.readLine());
                }
                catch (IOException exceptIO)
                {
                    IO.logger.log(Level.WARNING, "Error with stream reading", exceptIO);
                }
                finally
                {
                    /* Close stream reading objects */
                    try
                    {
                        if (readerBufferdSink != null)
                        {
                            readerBufferdSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO);
                    }

                    try
                    {
                        if (readerInputStreamSink != null)
                        {
                            readerInputStreamSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO);
                    }

                    try
                    {
                        if (streamFileInputSink != null)
                        {
                            streamFileInputSink.close();
                        }
                    }
                    catch (IOException exceptIO)
                    {
                        IO.logger.log(Level.WARNING, "Error closing FileInputStream", exceptIO);
                    }
                }
            }
        }

    }

    /* Below is the main(). It is only used when building this testcase on
     * its own for testing or for building a binary to use in testing binary
     * analysis tools. It is not used when compiling all the testcases as one
     * application, which is how source code analysis tools are tested.
     */
    public static void main(String[] args) throws ClassNotFoundException,
           InstantiationException, IllegalAccessException
    {
        mainFromParent(args);
    }
}

    public static void generateAbstraction(File buggy_file, int buggy_line, File working_dir) {
        Launcher launcher = new Launcher();
        launcher.getEnvironment().setAutoImports(true);
        launcher.getEnvironment().setNoClasspath(true);
        launcher.getEnvironment().setCommentEnabled(true);
        launcher.addInputResource(buggy_file.toString());
        try {
            launcher.buildModel();
        } catch (Exception e) {

        }

        CtModel model = launcher.getModel();

        CtMethod topLevelmethod = null;
        CtElement buggy_ctElement = null;
        CtElement tmp_ctElement = null;
        CtPath buggy_ctElement_ctPath = null;


        // This is the main part

        for (CtType<?> ctType : model.getAllTypes()) {

            for (Iterator<CtElement> desIter = ctType.descendantIterator(); desIter.hasNext(); ) {
                tmp_ctElement = desIter.next();

                try {

                    // Main problem resides here

                    if (tmp_ctElement.getPosition().getLine() == buggy_line && !(tmp_ctElement instanceof CtComment)) {
                        buggy_ctElement = tmp_ctElement;
                        buggy_ctElement_ctPath = tmp_ctElement.getPath();

                        topLevelmethod = getTopLevelMethod(buggy_ctElement);

                        List<CtComment> comments = topLevelmethod.getElements(
                                new TypeFilter<CtComment>(CtComment.class)
                        );

                        for(CtComment c: comments){
                               topLevelmethod.removeComment(c);
                        }

                        break;
                    }
                } catch (java.lang.UnsupportedOperationException e) {
                    continue;
                }
            }

            // ......
        }

    }

    public static CtMethod getTopLevelMethod(CtElement ctElement) {
        CtMethod topLevelMethod = null;
        topLevelMethod = ctElement.getParent(CtMethod.class);
        while (topLevelMethod != null && topLevelMethod.getParent(CtMethod.class) != null) {
            System.out.println();
            topLevelMethod = topLevelMethod.getParent(CtMethod.class);
        }
        return topLevelMethod;
    }
public class CWE23_Relative_Path_Traversal__connect_tcp_01 extends AbstractTestCase {
    public void bad() throws Throwable {
        String data;
        data = "";/* Initialize data */

        /* Read data using an outbound tcp connection */
        {
            Socket socket = null;
            BufferedReader readerBuffered = null;
            InputStreamReader readerInputStream = null;
            try {
                /* Read data using an outbound tcp connection */
                socket = new Socket("host.example.org", 39544);
                /* read input from socket */
                readerInputStream = new InputStreamReader(socket.getInputStream(), "UTF-8");
                readerBuffered = new BufferedReader(readerInputStream);
                /* POTENTIAL FLAW: Read data using an outbound tcp connection */
                data = readerBuffered.readLine();
            } catch (IOException exceptIO) {
                logger.log(Level.WARNING, "Error with stream reading", exceptIO);
            } finally {
                /* clean up stream reading objects */
                try {
                    if (readerBuffered != null) {
                        readerBuffered.close();
                    }
                } catch (IOException exceptIO) {
                    logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO);
                }
                try {
                    if (readerInputStream != null) {
                        readerInputStream.close();
                    }
                } catch (IOException exceptIO) {
                    logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO);
                }
                /* clean up socket objects */
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException exceptIO) {
                    logger.log(Level.WARNING, "Error closing Socket", exceptIO);
                }
            }
        }
        String root;
        if ((System.getProperty("os.name").toLowerCase().indexOf("win")) >= 0) {
            /* running on Windows */
            root = "C:\\uploads\\";
        }else {
            /* running on non-Windows */
            root = "/home/user/uploads/";
        }
        if (data != null) {
            /* POTENTIAL FLAW: no validation of concatenated value */
            File file = new File((root + data));
            FileInputStream streamFileInputSink = null;
            InputStreamReader readerInputStreamSink = null;
            BufferedReader readerBufferdSink = null;
            if ((file.exists()) && (file.isFile())) {
                try {
                    streamFileInputSink = new FileInputStream(file);
                    readerInputStreamSink = new InputStreamReader(streamFileInputSink, "UTF-8");
                    readerBufferdSink = new BufferedReader(readerInputStreamSink);
                    IO.writeLine(readerBufferdSink.readLine());
                } catch (IOException exceptIO) {
                    logger.log(Level.WARNING, "Error with stream reading", exceptIO);
                } finally {
                    /* Close stream reading objects */
                    try {
                        if (readerBufferdSink != null) {
                            readerBufferdSink.close();
                        }
                    } catch (IOException exceptIO) {
                        logger.log(Level.WARNING, "Error closing BufferedReader", exceptIO);
                    }
                    try {
                        if (readerInputStreamSink != null) {
                            readerInputStreamSink.close();
                        }
                    } catch (IOException exceptIO) {
                        logger.log(Level.WARNING, "Error closing InputStreamReader", exceptIO);
                    }
                    try {
                        if (streamFileInputSink != null) {
                            streamFileInputSink.close();
                        }
                    } catch (IOException exceptIO) {
                        logger.log(Level.WARNING, "Error closing FileInputStream", exceptIO);
                    }
                }
            }
        }
    }

    public void good() throws Throwable {
    }

    /* goodG2B() - uses goodsource and badsink */
    private void goodG2B() throws Throwable {
    }

    /* Below is the main(). It is only used when building this testcase on
    its own for testing or for building a binary to use in testing binary
    analysis tools. It is not used when compiling all the testcases as one
    application, which is how source code analysis tools are tested.
     */
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    }
}

/* uses badsource and badsink */

共有1个答案

哈骞仕
2023-03-14

几分钟前我无意中找到了答案。我不会删除这个答案,因为有人可能会发现这很有用。要删除文件的所有注释,只需关闭一个标志。

这句台词起到了神奇的作用-

launcher.getEnvironment().setCommentEnabled(false);

它所做的是设置setcommentenabledfalse,并完全忽略注释。所以,你根本不需要担心他们。这个简单的解决方案花了我6个小时才想出来。

 类似资料:
  • 有没有办法从一个巨大的xml文件中删除注释( 两者,根元素前的注释 和内的注释 最好的解决方案是使用xPath。我试过了 它适用于DOM,但不适用于vtd xml 这是我选择评论的代码 但此处的屏幕上打印的是nothing。 有没有办法用vtd xml做到这一点? 谢谢你的帮助。

  • 我想使用docx4j删除docx文件中的所有注释。 我可以使用如下所示的一段代码删除实际的注释,但我认为我也需要从主文档部分删除注释引用(否则文档已损坏),但我不知道如何做到这一点。 感谢您的指导! 我还在docx4j论坛上发布了这个问题:http://www.docx4java.org/forums/docx-java-f6/how-to-remove-all-comments-from-doc

  • 问题内容: 我想做一个获取字符串的函数,以防它有内联注释时将其删除。我知道这听起来很简单,但是我想确保即时消息正确执行,例如: 我考虑了两种方法:否则请随时咨询 迭代字符串并找到双行括号并使用substring方法。 正则表达式的方式..(我不太确定回合吧) 您能告诉我什么是最好的方法,并告诉我应该怎么做吗?(请不要建议太高级的解决方案) 编辑:可以使用Scanner对象以某种方式完成此操作吗?(

  • 有没有一种标准且简单的方法可以使用Maven插件从HTML模板(Thymeleaf)中删除注释?如果它能按惯例只为

  • 我正在做一个个人项目,我需要从这样的输入字符串中提取实际的注释。 案例1: 输出: 案例2: 输出: 案例3: 输出: 案例4: 输出: 我在Java工作,我试图替换和使用简单的方法,如,但由于注释可以以不同的方式格式化如上所述,替换方法似乎不是实现此目的的可行方法。如何使用正则表达式实现上述输出? 这是我正在使用的测试注释文件。

  • 我无法使用ApachePOI删除docx文件中的所有注释。有没有其他方法可以使用docx4j api删除注释?