Example usage for java.lang StackTraceElement getLineNumber

List of usage examples for java.lang StackTraceElement getLineNumber

Introduction

In this page you can find the example usage for java.lang StackTraceElement getLineNumber.

Prototype

public int getLineNumber() 

Source Link

Document

Returns the line number of the source line containing the execution point represented by this stack trace element.

Usage

From source file:com.simiacryptus.util.io.HtmlNotebookOutput.java

@javax.annotation.Nonnull
@SuppressWarnings("unchecked")
@Override//from  ww  w. j a  va2 s .  c o m
public <T> T code(@javax.annotation.Nonnull final UncheckedSupplier<T> fn, final int maxLog,
        final int framesNo) {
    try {
        final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        final StackTraceElement callingFrame = stackTrace[framesNo];
        final String sourceCode = CodeUtil.getInnerText(callingFrame);
        @javax.annotation.Nonnull
        final SysOutInterceptor.LoggedResult<TimedResult<Object>> result = SysOutInterceptor.withOutput(() -> {
            long priorGcMs = ManagementFactory.getGarbageCollectorMXBeans().stream()
                    .mapToLong(x -> x.getCollectionTime()).sum();
            final long start = System.nanoTime();
            try {
                @Nullable
                Object result1 = null;
                try {
                    result1 = fn.get();
                } catch (@javax.annotation.Nonnull final RuntimeException e) {
                    throw e;
                } catch (@javax.annotation.Nonnull final Exception e) {
                    throw new RuntimeException(e);
                }
                long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream()
                        .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs;
                return new TimedResult<Object>(result1, System.nanoTime() - start, gcTime);
            } catch (@javax.annotation.Nonnull final Throwable e) {
                long gcTime = ManagementFactory.getGarbageCollectorMXBeans().stream()
                        .mapToLong(x -> x.getCollectionTime()).sum() - priorGcMs;
                return new TimedResult<Object>(e, System.nanoTime() - start, gcTime);
            }
        });
        try {
            @javax.annotation.Nonnull
            final URI resolved = URI.create(sourceRoot)
                    .resolve(Util.pathTo(CodeUtil.projectRoot, CodeUtil.findFile(callingFrame)));
            out("<p>Code from <a href='%s#L%s'>%s:%s</a> executed in %.2f seconds: <br/>", resolved,
                    callingFrame.getLineNumber(), callingFrame.getFileName(), callingFrame.getLineNumber(),
                    result.obj.seconds());
        } catch (@javax.annotation.Nonnull final Exception e) {
            out("<p>Code from %s:%s executed in %.2f seconds: <br/>", callingFrame.getFileName(),
                    callingFrame.getLineNumber(), result.obj.seconds());
        }
        out("<pre>");
        out(sourceCode);
        out("</pre>");

        if (!result.log.isEmpty()) {
            out("Logging: <br/>");
            out("<pre>");
            out(summarize(maxLog, result.log));
            out("</pre>");
        }
        out("");

        final Object eval = result.obj.result;
        if (null != eval) {
            out("Returns: <br/>");
            String str;
            boolean escape;
            if (eval instanceof Throwable) {
                @javax.annotation.Nonnull
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                ((Throwable) eval).printStackTrace(new PrintStream(out));
                str = new String(out.toByteArray(), "UTF-8");
                escape = true;//
            } else if (eval instanceof Component) {
                str = image(Util.toImage((Component) eval), "Result");
                escape = false;
            } else if (eval instanceof BufferedImage) {
                str = image((BufferedImage) eval, "Result");
                escape = false;
            } else if (eval instanceof TableOutput) {
                str = ((TableOutput) eval).toHtmlTable();
                escape = false;
            } else {
                str = eval.toString();
                escape = true;
            }
            if (escape) {
                out("<pre>" + summarize(maxLog, str) + "</pre>");
            } else {
                out(summarize(maxLog, str));
            }
            if (escape) {
            }
            out("\n\n");
            if (eval instanceof Throwable) {
                throw new RuntimeException((Throwable) result.obj.result);
            }
        }
        out("</p>");
        return (T) eval;
    } catch (@javax.annotation.Nonnull final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.europa.esig.dss.xades.validation.XAdESSignature.java

@Override
public SignatureCryptographicVerification checkSignatureIntegrity() {

    if (signatureCryptographicVerification != null) {
        return signatureCryptographicVerification;
    }/*from  ww  w .  j a  v a2s .c  om*/
    signatureCryptographicVerification = new SignatureCryptographicVerification();
    final Document document = signatureElement.getOwnerDocument();
    final Element rootElement = document.getDocumentElement();

    DSSXMLUtils.setIDIdentifier(rootElement);
    DSSXMLUtils.recursiveIdBrowse(rootElement);
    try {

        final XMLSignature santuarioSignature = new XMLSignature(signatureElement, "");
        santuarioSignature.addResourceResolver(new XPointerResourceResolver(signatureElement));
        santuarioSignature.addResourceResolver(new OfflineResolver(detachedContents));

        boolean coreValidity = false;
        final List<CertificateValidity> certificateValidityList = getSigningCertificateValidityList(
                santuarioSignature, signatureCryptographicVerification, providedSigningCertificateToken);
        for (final CertificateValidity certificateValidity : certificateValidityList) {

            try {

                final PublicKey publicKey = certificateValidity.getPublicKey();
                coreValidity = santuarioSignature.checkSignatureValue(publicKey);
                if (coreValidity) {

                    candidatesForSigningCertificate.setTheCertificateValidity(certificateValidity);
                    break;
                }
            } catch (XMLSignatureException e) {
                LOG.warn("Exception when validating signature: ", e);
                signatureCryptographicVerification.setErrorMessage(e.getMessage());
            }
        }
        final SignedInfo signedInfo = santuarioSignature.getSignedInfo();
        final int length = signedInfo.getLength();

        boolean referenceDataFound = length > 0;
        boolean referenceDataHashValid = length > 0;

        boolean foundSignedProperties = false;
        for (int ii = 0; ii < length; ii++) {
            final Reference reference = signedInfo.item(ii);
            if (xPathQueryHolder.XADES_SIGNED_PROPERTIES.equals(reference.getType())) {
                foundSignedProperties = true;
            }
            if (!coreValidity) {
                referenceDataHashValid = referenceDataHashValid && reference.verify();
            }
            references.add(reference);
        }

        // 1 reference for SignedProperties + 1 reference / signed object
        referenceDataFound = referenceDataFound && foundSignedProperties;

        signatureCryptographicVerification.setReferenceDataFound(referenceDataFound);
        signatureCryptographicVerification.setReferenceDataIntact(referenceDataHashValid);
        signatureCryptographicVerification.setSignatureIntact(coreValidity);
    } catch (Exception e) {

        LOG.error(e.getMessage(), e);
        StackTraceElement[] stackTrace = e.getStackTrace();
        final String name = XAdESSignature.class.getName();
        int lineNumber = 0;
        for (StackTraceElement element : stackTrace) {

            final String className = element.getClassName();
            if (className.equals(name)) {

                lineNumber = element.getLineNumber();
                break;
            }
        }
        signatureCryptographicVerification
                .setErrorMessage(e.getMessage() + "/ XAdESSignature/Line number/" + lineNumber);
    }
    return signatureCryptographicVerification;
}