private static void doDecompileIntoDocument()

in extra/java.debugjavac/src/main/java/org/netbeans/modules/java/debugjavac/DecompiledTab.java [146:207]


    private static void doDecompileIntoDocument(FileObject source) {
        FileObject decompiled = findDecompiled(source, true);
        final Document doc = decompiledCodeDocument(source);
        
        if (doc == null || doc.getProperty(DECOMPILE_TAB_ACTIVE) != Boolean.TRUE) return ;
        
        try {
            Object compilerDescription = decompiled.getAttribute(CompilerDescription.class.getName());
            Object decompilerId = decompiled.getAttribute(DecompilerDescription.class.getName());
            Object extraParams = decompiled.getAttribute(PROP_EXTRA_PARAMS);
            
            if (!(compilerDescription instanceof CompilerDescription) || !(decompilerId instanceof String)) {
                return ;
            }

            if (!(extraParams instanceof String) || extraParams == null) {
                extraParams = "";
            }

            final String decompiledCode;
            
            if (((CompilerDescription) compilerDescription).isValid()) {
                final String code = Source.create(source).createSnapshot().getText().toString();
                UpToDateStatusProviderImpl.get(doc).update(UpToDateStatus.UP_TO_DATE_PROCESSING);
                DecompilerDescription decompiler = findDecompiler((String) decompilerId);
                Result decompileResult = ((CompilerDescription) compilerDescription).decompile(decompiler, new Input(code, Utilities.commandLineParameters(source, (String) extraParams)));
                if (decompileResult.exception != null) {
                    decompiledCode = "#Section(text/plain) Ooops, an exception occurred while decompiling:\n" + decompileResult.exception;
                } else {
                    decompiledCode = (decompileResult.decompiledOutput != null ? "#Section(" + decompileResult.decompiledMimeType + ") Output:\n" + decompileResult.decompiledOutput + "\n" : "") +
                                     (decompileResult.compileErrors != null ? "#Section(text/plain) Processing Errors:\n" + decompileResult.compileErrors + "\n" : "");
                }
            } else {
                decompiledCode = "Unusable compiler";
            }

            NbDocument.runAtomic((StyledDocument) doc, new Runnable() {
                @Override public void run() {
                    try {
                        doc.remove(0, doc.getLength());
                        if (doc instanceof GuardedDocument) {
                            ((GuardedDocument) doc).getGuardedBlockChain().removeEmptyBlocks();
                        }
                        doc.insertString(0, decompiledCode, null);
                        if (doc instanceof GuardedDocument) {
                            ((GuardedDocument) doc).getGuardedBlockChain().addBlock(0, doc.getLength(), true);
                        }
                    } catch (BadLocationException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            });
            
            SaveCookie sc = DataObject.find(decompiled).getLookup().lookup(SaveCookie.class);
            
            if (sc != null) sc.save();

            UpToDateStatusProviderImpl.get(doc).update(UpToDateStatus.UP_TO_DATE_OK);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }