public JavaProgram createJavaProgram( List sourceFiles, String libraryPath, String mainClassname, String projectType, String policyType) throws Exception { // Debug.setOn(true); long l = Debug.getTime(); LibraryData libraryData = new LibraryData(new File(libraryPath), projectType, language, false); Library library = new Library(libraryData); Parser parser = Parser.createParser(this.language, sourceFiles, library, new TranslationPolicy(policyType)); long lll = Debug.getTime(); System.out.println("Library Loaded in " + (lll - l) + "ms"); ParseTree tree = parser.parse(); TranslationReport report = tree.getTranslationReport(); long ll = Debug.getTime(); System.out.println("Code parsed in " + (ll - lll) + "ms"); Debug.setOn(true); Translator tt = new Translator(this.language); JavaProgram jp = tt.createJavaProgram(tree, mainClassname, projectType); if (report.hasTypeResolveErrors() || report.hasTranslationWarnings()) { List errors = new ArrayList(); errors.addAll(report.getTypeResolveExceptions()); report.doReport(errors, report.getTranslationWarnings()); } l = Debug.getTime(); System.out.println("Translated in " + (l - ll) + "ms"); jp.setTypeResolved(!report.hasTypeResolveErrors()); // resolved if no errors return jp; }
/** * Finds the specified class using <code>ClassPath</code>. If the source throws an exception, this * returns null. * * <p>This method can be overridden by a subclass of <code>Loader</code>. Note that the overridden * method must not throw an exception when it just fails to find a class file. * * @return null if the specified class could not be found. * @throws ClassNotFoundException if an exception is thrown while obtaining a class file. */ protected Class findClass(String name) throws ClassNotFoundException { byte[] classfile; try { if (source != null) { if (translator != null) translator.onLoad(source, name); try { classfile = source.get(name).toBytecode(); } catch (NotFoundException e) { return null; } } else { String jarname = "/" + name.replace('.', '/') + ".class"; InputStream in = this.getClass().getResourceAsStream(jarname); if (in == null) return null; classfile = ClassPoolTail.readStream(in); } } catch (Exception e) { throw new ClassNotFoundException( "caught an exception while obtaining a class file for " + name, e); } int i = name.lastIndexOf('.'); if (i != -1) { String pname = name.substring(0, i); if (getPackage(pname) == null) try { definePackage(pname, null, null, null, null, null, null, null); } catch (IllegalArgumentException e) { // ignore. maybe the package object for the same // name has been created just right away. } } if (domain == null) return defineClass(name, classfile, 0, classfile.length); else return defineClass(name, classfile, 0, classfile.length, domain); }
public void doTest() throws FileNotFoundException, ToolkitException { String sampleFilesDirectory = System.getProperty( BaseTestJAXBTranslator.class.getName() + ".sampleFilesDir", "src/test/data/sampleMessages"); if (sampleFilesDirectory == null) { Assert.fail( "Test failed because system property " + BaseTestJAXBTranslator.class.getName() + ".sampleFilesDir was not set. It must be set to a directory containing sample NCIP messages."); } String fileNamesPattern = System.getProperty( BaseTestJAXBTranslator.class.getName() + ".fileNamesPattern", DEFAULT_FILENAMES_PATTERN); String fileNamesToOmitPattern = System.getProperty( BaseTestJAXBTranslator.class.getName() + ".fileNamesToOmitPattern", DEFAULT_FILENAMES_TO_OMIT_PATTERN); int iterations = 1; String iterationsString = System.getProperty(BaseTestJAXBTranslator.class.getName() + ".iterations"); if (iterationsString != null) { iterations = Integer.parseInt(iterationsString); } File dir = new File(sampleFilesDirectory); FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isFile(); } }; StringBuilder failuresList = new StringBuilder(); File[] files = dir.listFiles(fileFilter); long initTranslateStreamToSvcElapsedTime = 0; long respTranslateStreamToSvcElapsedTime = 0; long initTranslateSvcToStreamElapsedTime = 0; long respTranslateSvcToStreamElapsedTime = 0; long initMsgStreamToSvcCount = 0; long respMsgStreamToSvcCount = 0; long initMsgSvcToStreamCount = 0; long respMsgSvcToStreamCount = 0; if (files != null) { long startTime = 0; long endTime = 0; for (int iteration = 0; iteration < iterations; ++iteration) { for (File file : files) { String fileName = file.getName(); if ((fileNamesPattern == null || fileNamesPattern.length() == 0 || fileName.matches(fileNamesPattern)) && (fileNamesToOmitPattern == null || fileNamesToOmitPattern.length() == 0 || !fileName.matches(fileNamesToOmitPattern))) { InputStream inStream = new FileInputStream(file); LOG.info("Testing " + fileName); ServiceContext serviceContext = serviceValidator.getInitialServiceContext(); if (fileName.contains("Response")) { try { startTime = System.currentTimeMillis(); NCIPResponseData responseData = translator.createResponseData(serviceContext, inStream); endTime = System.currentTimeMillis(); respTranslateStreamToSvcElapsedTime += (endTime - startTime); respMsgStreamToSvcCount++; Assert.assertNotNull( "createResponseData returned null for " + fileName + ".", responseData); if (responseData != null) { try { startTime = System.currentTimeMillis(); InputStream responseStream = translator.createResponseMessageStream(serviceContext, responseData); endTime = System.currentTimeMillis(); respTranslateSvcToStreamElapsedTime += (endTime - startTime); respMsgSvcToStreamCount++; Assert.assertNotNull(responseStream); if (performXMLDiff) { performXMLDiff(responseStream, failuresList, file); } } catch (ServiceException e) { LOG.debug( "Exception in createResponseMessageStream for '" + fileName + "'.", e); failuresList.append( collectException("createResponseMessageStream", fileName, e)); } catch (ValidationException e) { LOG.debug( "Exception in createResponseMessageStream for '" + fileName + "'.", e); failuresList.append( collectException("createResponseMessageStream", fileName, e)); } } } catch (ServiceException e) { LOG.debug("Exception in createResponseData for '" + fileName + "'.", e); failuresList.append(collectException("createResponseData", fileName, e)); } catch (ValidationException e) { LOG.debug("Exception in createResponseData for '" + fileName + "'.", e); failuresList.append(collectException("createResponseData", fileName, e)); } } else { try { startTime = System.currentTimeMillis(); NCIPInitiationData initiationData = translator.createInitiationData(serviceContext, inStream); endTime = System.currentTimeMillis(); initTranslateStreamToSvcElapsedTime += (endTime - startTime); initMsgStreamToSvcCount++; Assert.assertNotNull( "createInitiationData returned null for " + fileName + ".", initiationData); if (initiationData != null) { try { startTime = System.currentTimeMillis(); InputStream initiationStream = translator.createInitiationMessageStream(serviceContext, initiationData); Assert.assertNotNull(initiationStream); endTime = System.currentTimeMillis(); initTranslateSvcToStreamElapsedTime += (endTime - startTime); initMsgSvcToStreamCount++; if (performXMLDiff) { performXMLDiff(initiationStream, failuresList, file); } } catch (ServiceException e) { LOG.debug( "Exception in createInitiationMessageStream for '" + fileName + "'.", e); failuresList.append( collectException("createInitiationMessageStream", fileName, e)); } catch (ValidationException e) { LOG.debug( "Exception in createInitiationMessageStream for '" + fileName + "'.", e); failuresList.append( collectException("createInitiationMessageStream", fileName, e)); } } } catch (ServiceException e) { LOG.debug("Exception in createInitiationData for '" + fileName + "'.", e); failuresList.append(collectException("createInitiationData", fileName, e)); } catch (ValidationException e) { LOG.debug("Exception in createInitiationData for '" + fileName + "'.", e); failuresList.append(collectException("createInitiationData", fileName, e)); } } } } } Assert.assertTrue( "One or more messages failed:" + System.getProperty("line.separator") + failuresList, failuresList.length() == 0); } else { Assert.fail( "No files were found in " + sampleFilesDirectory + ". Note: do not include a trailing slash in the path." + "Working directory: " + System.getProperty("user.dir")); } LOG.info( "Elapsed time for translating stream-to-service for initiation messages: " + initTranslateStreamToSvcElapsedTime + " for " + initMsgStreamToSvcCount + " messages; average " + computeAverage(initTranslateStreamToSvcElapsedTime, initMsgStreamToSvcCount)); LOG.info( "Elapsed time for translating stream-to-service for response messages: " + respTranslateStreamToSvcElapsedTime + " for " + respMsgStreamToSvcCount + " messages; average " + computeAverage(respTranslateStreamToSvcElapsedTime, respMsgStreamToSvcCount)); LOG.info( "Elapsed time for translating service-to-stream for initiation messages: " + initTranslateSvcToStreamElapsedTime + " for " + initMsgSvcToStreamCount + " messages; average " + computeAverage(initTranslateSvcToStreamElapsedTime, initMsgSvcToStreamCount)); LOG.info( "Elapsed time for translating service-to-stream for response messages: " + respTranslateSvcToStreamElapsedTime + " for " + respMsgSvcToStreamCount + " messages; average " + computeAverage(respTranslateSvcToStreamElapsedTime, respMsgSvcToStreamCount)); String statsReport = statisticsBean.createCSVReport(); System.out.println(statsReport); }
/** * Adds a translator, which is called whenever a class is loaded. * * @param cp the <code>ClassPool</code> object for obtaining a class file. * @param t a translator. * @throws NotFoundException if <code>t.start()</code> throws an exception. * @throws CannotCompileException if <code>t.start()</code> throws an exception. */ public void addTranslator(ClassPool cp, Translator t) throws NotFoundException, CannotCompileException { source = cp; translator = t; t.start(cp); }