@Override public Reader getInputReader() { return new InputStreamReader(getInputStream()); }
@Override protected FileModule makeFile(File file) { String rootPath = root.getAbsolutePath(); if (!rootPath.endsWith(File.separator)) { rootPath = rootPath + File.separator; } String filePath = file.getAbsolutePath(); assert filePath.startsWith(rootPath); return new SourceFileModule(file, filePath.substring(rootPath.length()), this); }
@Override public String toString() { return "SourceFileModule:" + getFile().toString(); }
@Override public void loadAllSources(Set<ModuleEntry> modules) { List<String> sources = new LinkedList<>(); Map<String, ModuleEntry> sourceMap = HashMapFactory.make(); for(ModuleEntry m : modules) { if (m.isSourceFile()) { SourceFileModule s = (SourceFileModule)m; sourceMap.put(s.getAbsolutePath(), s); sources.add(s.getAbsolutePath()); } } String[] sourceFiles = sources.toArray(new String[0]); @SuppressWarnings("deprecation") final ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setEnvironment(libs, this.sources, null, false); Hashtable<String, String> options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_SOURCE, "1.8"); parser.setCompilerOptions(options); parser.createASTs(sourceFiles, null, new String[0], new ECJAstToIR(sourceMap), new NullProgressMonitor()); }
@Override public TranslatorToCAst make(CAst ast, final ModuleEntry M) { String N; if (M instanceof SourceFileModule) { N = ((SourceFileModule) M).getClassName(); } else { N = M.getName(); } TranslatorToCAst xlator = translateInternal(ast, (SourceModule)M, N); xlator.addRewriter(ast1 -> new AstLoopUnwinder(ast1, true, unwindFactor), false); return xlator; } }
public SourceFileModule(File f, SourceFileModule clonedFrom) { super(f, clonedFrom.getContainer()); this.fileName = clonedFrom.fileName; }
} else if (m instanceof SourceFileModule) { moduleType = "sourceFile"; modulePath = ((SourceFileModule) m).getAbsolutePath(); } else { Assertions.UNREACHABLE("Module type isn't supported - " + m);
@Override public TranslatorToCAst make(CAst ast, final ModuleEntry M) { String N; if (M instanceof SourceFileModule) { N = ((SourceFileModule) M).getClassName(); } else { N = M.getName(); } TranslatorToCAst xlator = translateInternal(ast, (SourceModule)M, N); xlator.addRewriter(ast1 -> new AstLoopUnwinder(ast1, true, unwindFactor), false); return xlator; } }
public SourceFileModule(File f, SourceFileModule clonedFrom) { super(f, clonedFrom.getContainer()); this.fileName = clonedFrom.fileName; }
@Override public Reader getInputReader() { return new InputStreamReader(getInputStream()); }
@Override protected FileModule makeFile(File file) { String rootPath = root.getAbsolutePath(); if (!rootPath.endsWith(File.separator)) { rootPath = rootPath + File.separator; } String filePath = file.getAbsolutePath(); assert filePath.startsWith(rootPath); return new SourceFileModule(file, filePath.substring(rootPath.length()), this); }
@Override public String toString() { return "SourceFileModule:" + getFile().toString(); }
} else if (m instanceof SourceFileModule) { moduleType = "sourceFile"; modulePath = ((SourceFileModule) m).getAbsolutePath(); } else { Assertions.UNREACHABLE("Module type isn't supported - " + m);
@Override public InputStream getInputStream() { BOMInputStream bs = new BOMInputStream(super.getInputStream(), false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); try { if (bs.hasBOM()) { System.err.println("removing BOM " + bs.getBOM()); } return bs; } catch (IOException e) { return super.getInputStream(); } } };
/** * Add a class file to the scope for a loader */ public void addSourceFileToScope(ClassLoaderReference loader, File file, String fileName) throws IllegalArgumentException { List<Module> s = MapUtil.findOrCreateList(moduleMap, loader); s.add(new SourceFileModule(file, fileName, null)); }
@Override public InputStream getInputStream() { BOMInputStream bs = new BOMInputStream(super.getInputStream(), false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); try { if (bs.hasBOM()) { System.err.println("removing BOM " + bs.getBOM()); } return bs; } catch (IOException e) { return super.getInputStream(); } } };
/** * Add a class file to the scope for a loader */ public void addSourceFileToScope(ClassLoaderReference loader, File file, String fileName) throws IllegalArgumentException { List<Module> s = MapUtil.findOrCreateList(moduleMap, loader); s.add(new SourceFileModule(file, fileName, null)); }
@Override public InputStream getInputStream() { String moduleSource = null; try (final InputStream inputStream = super.getInputStream()) { moduleSource = IOUtils.toString(inputStream); } catch (IOException e) { Assertions.UNREACHABLE(e.getMessage()); } String wrapperSource = null; String ext = FilenameUtils.getExtension(getFile().toString()).toLowerCase(); if (ext.equals("js")) { // JS file -> use module wrapper wrapperSource = MODULE_WRAPPER_SOURCE; } else if (ext.equals("json")) { // JSON file -> use JSON wrapper wrapperSource = JSON_WRAPPER_SOURCE; } else { // No clue -> try module wrapper System.err.println("NodejsRequiredSourceModule: Unsupported file type ("+ext+"), continue anyway."); wrapperSource = MODULE_WRAPPER_SOURCE; } String wrappedModuleSource = wrapperSource .replace(FILENAME_PLACEHOLDER, getFile().getName()) .replace(DIRNAME_PLACEHOLDER, getFile().getParent()) .replace(CODE_PLACEHOLDER, moduleSource); return IOUtils.toInputStream(wrappedModuleSource); }