-4

I was writing code to merge .pdf documents when I wrote this line:

PDFmerger.mergeDocuments();

When I wrote it, Java told me that The method mergeDocuments() from the type PDFMergerUtility is deprecated, and told me to add this line:

@SuppressWarnings("deprecation")

and later, it has the command mergeDocuments(); struck through. This command still works, but I would like to ask: Why did my code cross out the line? The code still works, but I don't know why this happens. By the way, I am using PDFBox to merge two different pdfs. The full code is below (and imagine a line running across the text .mergedocuments()):

JButton btnMerge = new JButton("Merge");
    btnMerge.addActionListener(new ActionListener() {
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e) {
             try {
                PDDocument doc1 = PDDocument.load(rearrange[0]);
                PDDocument doc2 = PDDocument.load(rearrange[1]);
                PDDocument doc3 = PDDocument.load(rearrange[2]);
                //Instantiating PDFMergerUtility class
                PDFMergerUtility PDFmerger = new PDFMergerUtility();
                String finalname = name.substring(0, 8);
                PDFmerger.setDestinationFileName(path.replace(name, finalname+"_Pin Plug List.pdf"));
                PDFmerger.addSource(rearrange[0]);
                PDFmerger.addSource(rearrange[1]);
                PDFmerger.addSource(rearrange[2]);
                PDFmerger.mergeDocuments();
                doc1.close();
                doc2.close();
                doc3.close();

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    btnMerge.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnMerge.setBounds(12, 261, 388, 50);
    contentPane.add(btnMerge);
Elliot Lin
  • 19
  • 8
  • what IDE are you using? – Jason V Jul 17 '17 at 19:32
  • the suppression override is not NEEDED it only serves to tell the compiler, "i don't care, compile it anyway" you can plug away without it and it will run or not just the same – Jason V Jul 17 '17 at 19:32
  • Eclipse Oxygen. – Elliot Lin Jul 17 '17 at 19:32
  • so it was eclipse that suggested the suppression, it just removes the 'problem' as far as the project builder is concerned. – Jason V Jul 17 '17 at 19:33
  • 2
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking."* –  Jul 17 '17 at 19:47
  • 2
    A quick look at the javadoc or at the source would have shown that the call is to be replaced by `mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());` – Tilman Hausherr Jul 18 '17 at 08:03

1 Answers1

0

The underlying problem, as indicated in the initial error message, is that the mergeDocuments() method of PDFMerger is deprecated. This means that the vendor considers this method outdated, may remove it in the future (but hasn't yet, as a grace for the existing codebase), and thinks you should not be using it - especially in newly-written code.

So the correct fix is to refer to the documentation for PDFMerger and find out what you should be doing instead. (Proper documentation of a deprecated element should include instructions for what has replaced the element.)

Adding @SuppressWarnings() annotations is a "quick fix" to keep that particular warning from cluttering your build logs or, in the case of a "clean build" policy, causing the build to be considered broken. But it is rarely the right thing to do. It exists for those cases when it simply isn't practical to remove the usage of the deprecated element.

Your IDE is striking through the method name as a reminder that it is, in fact, deprecated - i.e. that this bit of code should be rewritten.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52