@Test void test_source_map() throws Exception { SensorContextTester ctx = jspContext(JSP_SOURCE); Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList()); assertThat(generatedFiles).hasSize(1); GeneratedFile generatedFile = generatedFiles.iterator().next(); assertThat(generatedFile.sourceMap()).isNotNull(); }
public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem()); LOG.debug("Found {} JSP files.", jspFiles.size()); if (jspFiles.isEmpty()) { return Collections.emptyList(); } Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()) .orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath()); LOG.debug("Context root set to {}", uriRoot); Path outputDir = outputDir(sensorContext); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = initClassLoader(javaClasspath); Thread.currentThread().setContextClassLoader(classLoader); JspFactory.setDefaultFactory(new JspFactoryImpl()); JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader); JasperOptions options = getJasperOptions(outputDir, servletContext); JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options); boolean errorTranspiling = false; Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>(); for (InputFile jsp : jspFiles) { try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); } catch (Exception | LinkageError e) { errorTranspiling = true; StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { LOG.warn("Some JSP pages failed to transpile. Enable debug log for details."); } runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem())); return generatedJavaFiles.values(); } catch (Exception e) { LOG.warn("Failed to transpile JSP files.", e); return Collections.emptyList(); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }
Jasper { public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem()); LOG.debug("Found {} JSP files.", jspFiles.size()); if (jspFiles.isEmpty()) { return Collections.emptyList(); } Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()) .orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath()); LOG.debug("Context root set to {}", uriRoot); Path outputDir = outputDir(sensorContext); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = initClassLoader(javaClasspath); Thread.currentThread().setContextClassLoader(classLoader); JspFactory.setDefaultFactory(new JspFactoryImpl()); JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader); JasperOptions options = getJasperOptions(outputDir, servletContext); JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options); boolean errorTranspiling = false; Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>(); for (InputFile jsp : jspFiles) { try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); } catch (Exception | LinkageError e) { errorTranspiling = true; StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { LOG.warn("Some JSP pages failed to transpile. Enable debug log for details."); } runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem())); return generatedJavaFiles.values(); } catch (Exception e) { LOG.warn("Failed to transpile JSP files.", e); return Collections.emptyList(); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } } }
Jasper { public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem()); LOG.debug("Found {} JSP files.", jspFiles.size()); if (jspFiles.isEmpty()) { return Collections.emptyList(); } Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()) .orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath()); LOG.debug("Context root set to {}", uriRoot); Path outputDir = outputDir(sensorContext); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = initClassLoader(javaClasspath); Thread.currentThread().setContextClassLoader(classLoader); JspFactory.setDefaultFactory(new JspFactoryImpl()); JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader); JasperOptions options = getJasperOptions(outputDir, servletContext); JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options); boolean errorTranspiling = false; Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>(); for (InputFile jsp : jspFiles) { try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); } catch (Exception | LinkageError e) { errorTranspiling = true; StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { LOG.warn("Some JSP pages failed to transpile. Enable debug log for details."); } runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem())); return generatedJavaFiles.values(); } catch (Exception e) { LOG.warn("Failed to transpile JSP files.", e); return Collections.emptyList(); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }  }
Jasper { public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem()); LOG.debug("Found {} JSP files.", jspFiles.size()); if (jspFiles.isEmpty()) { return Collections.emptyList(); } Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()) .orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath()); LOG.debug("Context root set to {}", uriRoot); Path outputDir = outputDir(sensorContext); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = initClassLoader(javaClasspath); Thread.currentThread().setContextClassLoader(classLoader); JspFactory.setDefaultFactory(new JspFactoryImpl()); JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader); JasperOptions options = getJasperOptions(outputDir, servletContext); JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options); boolean errorTranspiling = false; Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>(); for (InputFile jsp : jspFiles) { try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); } catch (Exception | LinkageError e) { errorTranspiling = true; StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { LOG.warn("Some JSP pages failed to transpile. Enable debug log for details."); } runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem())); return generatedJavaFiles.values(); } catch (Exception e) { LOG.warn("Failed to transpile JSP files.", e); return Collections.emptyList(); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }  Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath); }
Jasper { public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem()); LOG.debug("Found {} JSP files.", jspFiles.size()); if (jspFiles.isEmpty()) { return Collections.emptyList(); } Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()) .orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath()); LOG.debug("Context root set to {}", uriRoot); Path outputDir = outputDir(sensorContext); ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try { ClassLoader classLoader = initClassLoader(javaClasspath); Thread.currentThread().setContextClassLoader(classLoader); JspFactory.setDefaultFactory(new JspFactoryImpl()); JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader); JasperOptions options = getJasperOptions(outputDir, servletContext); JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options); boolean errorTranspiling = false; Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>(); for (InputFile jsp : jspFiles) { try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); } catch (Exception | LinkageError e) { errorTranspiling = true; StringWriter w = new StringWriter(); e.printStackTrace(new PrintWriter(w)); LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { LOG.warn("Some JSP pages failed to transpile. Enable debug log for details."); } runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem())); return generatedJavaFiles.values(); } catch (Exception e) { LOG.warn("Failed to transpile JSP files.", e); return Collections.emptyList(); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }  Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath);  }