walk = Files.walk(Paths.get(path))) {
- walk.map(Path::toString)
- .filter(f -> f.endsWith(".java"))
- .map(Parser::pullFileNameFromPath)
- .collect(Collectors.toCollection(() -> result));
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return result;
- }
-
- /**
- * Takes the passed in file path and extracts the filename without extension.
- *
- * @param path
- * @return
- */
- protected static String pullFileNameFromPath(String path) {
- int lastSlashIndex = path.lastIndexOf('/');
- int lastPeriodIndex = path.lastIndexOf('.');
- return path.substring(lastSlashIndex + 1, lastPeriodIndex);
- }
-
- /**
- * The parser works by listing method calls within the individual resource conversion methods as
- * {@link MethodCallExpr}. To extract the information we need to refactor the code,
- * such as method body, references, signature, etc, we rely on the javaparser {@link TypeSolver} to parse the code
- * library and convert the expressions to concrete {@link MethodDeclaration}.
- *
- * NB. The more source files in the directory you pass in (this will search recursively), the longer the
- * MethodDeclaration lookups will take. Be smart, choose S-Mart.
- *
- * @param rootProjectDirectory
- * @param projectDirectory {@link String} path to the directory that contains the souce files we want to be available for
- */
- public static void initializeResolver(String rootProjectDirectory, String projectDirectory) throws IOException {
- System.out.println("Initializing resolver against the following root directory:\n" + rootProjectDirectory);
- System.out.println("Project codebase located here:\n" + projectDirectory);
-
- TypeSolver myTypeSolver = new CombinedTypeSolver(
- new ReflectionTypeSolver(),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.convertors/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.utilities/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.dstu2/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.dstu3/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.dstu2016may/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.r4/src/main/java/")),
- new JavaParserTypeSolver(new File(rootProjectDirectory + "/org.hl7.fhir.r5/src/main/java/")),
- new JarTypeSolver("/Users/markiantorno/.m2/repository/ca/uhn/hapi/fhir/hapi-fhir-structures-r4/4.1.0/hapi-fhir-structures-r4-4.1.0.jar"),
- new JarTypeSolver("/Users/markiantorno/.m2/repository/ca/uhn/hapi/fhir/hapi-fhir-base/4.1.0/hapi-fhir-base-4.1.0.jar")
- );
-
- JavaSymbolSolver symbolSolver = new JavaSymbolSolver(myTypeSolver);
- StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
- }
-
- /**
- * Initializes the parser and runs it against the file located at the passed in path.
- *
- * @param path {@link String} path to the file.
- * @return {@link Optional }
- */
- public static Optional initializeParser(String path) {
- System.out.println("Initializing parser, and parsing the following file:\n" + path);
- CompilationUnit compilationUnit = null;
- try {
- compilationUnit = StaticJavaParser.parse(new File(path));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return Optional.ofNullable(compilationUnit);
- }
-
- /**
- * Loads a class using the {@link CompilationUnit} passed in and returns the resulting declaration for parsing. This
- * class must exist within the directory parsed originally in {@link #initializeParser(String)}
- *
- * @param cu {@link CompilationUnit}
- * @param classname {@link String} The name of the class to load.
- * @return {@link Optional } for the named class.
- */
- protected static Optional loadClass(CompilationUnit cu, String classname) {
- return cu.getClassByName(classname);
- }
-
- /**
- * Takes a given {@link MethodCallExpr} and uses the initialized {@link JavaSymbolSolver} to search the source code
- * for the appropriate {@link MethodDeclaration}.
- *
- * @param methodCallExpr {@link MethodCallExpr}
- * @return An {@link Optional}, containing the corresponding {@link MethodDeclaration}
- */
- protected static Optional resolveMethodDeclaration(MethodCallExpr methodCallExpr) {
- MethodDeclaration wrappedDeclaration = null;
- ResolvedMethodDeclaration correspondingDeclaration = methodCallExpr.resolve();
-
- if (correspondingDeclaration instanceof JavaParserMethodDeclaration) {
- JavaParserMethodDeclaration declaration = (JavaParserMethodDeclaration) correspondingDeclaration;
- Node wrappedNode = declaration.getWrappedNode();
- wrappedDeclaration = (MethodDeclaration) wrappedNode;
- System.out.println();
- }
-
- return Optional.ofNullable(wrappedDeclaration);
- }
-
- /**
- * Takes the content {@link String} passed in, and writes it to a java file with the provided name, in the provided
- * directory location.
- *
- * @param filename Name of the file, including extension, ex: "Book.java"
- * @param directory Path to directory to create the file
- * @param content {@link String} content of the file
- * @throws IOException
- */
- public static void writeJavaCodeToFile(String filename, String directory, String content) throws IOException {
- BufferedWriter writer = new BufferedWriter(new FileWriter(directory + filename));
- writer.write(content);
- writer.close();
- }
-
- /**
- * Returns the list of class methods.
- *
- * @param classOrInterfaceDeclaration {@link ClassOrInterfaceDeclaration}
- * @return {@link List} of all {@link MethodDeclaration}
- */
- protected static List getListOfClassMethods(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
- return getListOfClassMethods("", classOrInterfaceDeclaration);
- }
-
- /**
- * Returns the list of class methods containing the passed in method name.
- *
- * @param methodName {@link String} method name to search for
- * @param classOrInterfaceDeclaration {@link ClassOrInterfaceDeclaration}
- * @return {@link List} of all matching {@link MethodDeclaration}
- */
- protected static List getListOfClassMethods(String methodName, ClassOrInterfaceDeclaration classOrInterfaceDeclaration) {
- List result = new ArrayList<>();
- classOrInterfaceDeclaration.getMethods().stream()
- .filter(method -> method.getName().toString().equals(methodName))
- .collect(Collectors.toCollection(() -> result));
- return result;
- }
-
- private static List generateCommentReportData(String filepath) throws FileNotFoundException {
-
- CompilationUnit cu = StaticJavaParser.parse(new File(filepath));
-
- return cu.getAllContainedComments().stream()
- .filter(Comment::isOrphan)
- .filter(Comment::isLineComment)
- .map(p -> new CommentReportEntry(filepath.substring(filepath.lastIndexOf('/') + 1),
- p.getClass().getSimpleName(), p.getContent(), p.getRange().get().begin.line,
- !p.getCommentedNode().isPresent()))
- .collect(Collectors.toList());
- }
-
- /**
- * Class that visits all method calls within a given method and returns the results as a list.
- */
- public static class MethodCallVisitor extends VoidVisitorAdapter> {
-
- private final String keyword;
- private final boolean strict;
-
- public MethodCallVisitor(String keywords) {
- super();
- this.keyword = keywords;
- this.strict = false;
- }
-
- public MethodCallVisitor(String keywords, boolean strict) {
- super();
- this.keyword = keywords;
- this.strict = strict;
- }
-
- @Override
- public void visit(MethodCallExpr methodCallExpr, List collector) {
- if (strict) {
- if (methodCallExpr.getName().asString().equals(keyword)) {
- collector.add(methodCallExpr);
- }
- } else {
- if (methodCallExpr.getName().asString().contains(keyword)) {
- collector.add(methodCallExpr);
- }
- }
- super.visit(methodCallExpr, collector);
- }
- }
-
- /**
- * Class that visits all method calls within a given method and returns the results as a list.
- */
- public static class MethodFieldVisitor extends VoidVisitorAdapter> {
-
- public MethodFieldVisitor() {
- super();
- }
-
- @Override
- public void visit(NameExpr fieldAccessExpr, List collector) {
- super.visit(fieldAccessExpr, collector);
- collector.add(fieldAccessExpr);
- }
- }
-
- /**
- * Class that visits all method calls within a given method and returns the results as a list.
- */
- public static class VarAccessChanger extends ModifierVisitor {
-
- private final List varsToAccessInMainFile;
- private final String parentClassName;
-
- public VarAccessChanger(List varsToAccessInMainFile, String parentClassName) {
- super();
- this.varsToAccessInMainFile = varsToAccessInMainFile;
- this.parentClassName = parentClassName;
- }
-
- @Override
- public NameExpr visit(NameExpr nameExpr, Void arg) {
- super.visit(nameExpr, arg);
- if (varsToAccessInMainFile.contains(nameExpr.getNameAsString())) {
- nameExpr.setName(parentClassName + "." + nameExpr.getNameAsString());
- }
- return nameExpr;
- }
- }
-
- /**
- * Class that visits all methods and deletes them if they are in the list of passed in MethodDeclarations
- */
- public static class MethodDeleter extends ModifierVisitor {
-
- private List toDelete;
-
- public MethodDeleter(List toDelete) {
- this.toDelete = toDelete;
- }
-
- @Override
- public MethodDeclaration visit(MethodDeclaration md, Void arg) {
- super.visit(md, arg);
- if (toDelete.contains(md)) {
- return null;
- }
- return md;
- }
- }
-
- /**
- * Class that visits all methods and deletes them if they are in the list of passed in MethodDeclarations
- */
- public static class MethodExposer extends ModifierVisitor {
-
- private List toModify;
-
- public MethodExposer(List toModify) {
- this.toModify = toModify;
- }
-
- @Override
- public MethodDeclaration visit(MethodDeclaration md, Void arg) {
- super.visit(md, arg);
- if (toModify.contains(md)) {
- md.setModifier(Modifier.Keyword.PRIVATE, false);
- md.setModifier(Modifier.Keyword.STATIC, true);
- md.setModifier(Modifier.Keyword.PUBLIC, true);
- }
- return md;
- }
- }
-
- /**
- * Class that visits all calls in calls and changes the access to an external class if it matches one of the passed
- * in labels.
- */
- public static class MethodStaticCallAdder extends ModifierVisitor {
-
- private List methodNames;
- private String versionCode;
-
- public MethodStaticCallAdder(List methodNames, String versionCode) {
- this.methodNames = methodNames;
- this.versionCode = versionCode;
- }
-
- @Override
- public MethodCallExpr visit(MethodCallExpr methodCallExpr, Void arg) {
- super.visit(methodCallExpr, arg);
- if (methodNames.contains(methodCallExpr.getNameAsString())) {
- //eg. convertPatient -> Patient30_50.convertPatient
- methodCallExpr.setName(methodCallExpr.getNameAsString().replace(KEYWORD_CONVERT, "")
- + versionCode + "." + methodCallExpr.getNameAsString());
- }
- return methodCallExpr;
- }
- }
-
- private static class CommentReportEntry {
- public String filename;
- public String type;
- public String text;
- public int lineNumber;
- public boolean isOrphan;
-
- CommentReportEntry(String filename, String type, String text, int lineNumber,
- boolean isOrphan) {
- this.filename = filename;
- this.type = type;
- this.text = text;
- this.lineNumber = lineNumber;
- this.isOrphan = isOrphan;
- }
-
- @Override
- public String toString() {
- return filename + " | " + lineNumber + " | " + type + " | " + text.replaceAll("\\n", "").trim();
- }
- }
-
- // ------------- Version String Manipulation Stuff
-
- public static final String MODEL_IMPORT_PATH = "org.hl7.fhir.%1$s.model.%2$s";
- public static final String MODEL_ABSOLUTE_PATH = "/org.hl7.fhir.%1$s/src/main/java/org/hl7/fhir/%1$s/model/%2$s.java";
- public static final String MODEL_PATH_WITHOUT_IMPORT = "/org.hl7.fhir.%1$s/src/main/java/";
-
- public static String getVersionString(String version) {
- switch (version) {
- case ("10"):
- return "dstu2";
- case ("14"):
- return "dstu2016may";
- case ("30"):
- return "dstu3";
- case ("40"):
- return "r4";
- case ("50"):
- return "r5";
- default:
- throw new IllegalArgumentException("Passed in version " + version + ", no such mapping exists...");
- }
- }
-
- public static String getModelAbsolutePathFromVersion(String convertorClassName) {
- String projectDirectory = new File("").getAbsolutePath();
- String version = getFromVersion(convertorClassName);
- return projectDirectory + String.format(MODEL_ABSOLUTE_PATH, getVersionString(version), extractName(convertorClassName));
- }
-
- public static String getModelAbsolutePathToVersion(String convertorClassName) {
- String projectDirectory = new File("").getAbsolutePath();
- String version = getToVersion(convertorClassName);
- return projectDirectory + String.format(MODEL_ABSOLUTE_PATH, getVersionString(version), extractName(convertorClassName));
- }
-
- public static String importStatementToAbsolutePath(String importStatement) {
- String projectDirectory = new File("").getAbsolutePath();
-
- String placeholder = null;
- if (importStatement.contains("dstu2016may")) {
- placeholder = "dstu2016may";
- } else if (importStatement.contains("dstu2")) {
- placeholder = "dstu2";
- } else if (importStatement.contains("dstu3")) {
- placeholder = "dstu3";
- } else if (importStatement.contains("r4")) {
- placeholder = "r4";
- } else if (importStatement.contains("r5")) {
- placeholder = "r5";
- }
-
- return projectDirectory + String.format(MODEL_PATH_WITHOUT_IMPORT, placeholder) + importStatement.replace('.', '/') + ".java";
- }
-
- private static String extractName(String convertorClassName) {
- return convertorClassName.substring(0, convertorClassName.length() - 5);
- }
-
- public static String getModelImportToVersion(String convertorClassName) {
- String version = getToVersion(convertorClassName);
- return String.format(MODEL_IMPORT_PATH, getVersionString(version), extractName(convertorClassName));
- }
-
- public static String getModelImportFromVersion(String convertorClassName) {
- String version = getFromVersion(convertorClassName);
- return String.format(MODEL_IMPORT_PATH, getVersionString(version), extractName(convertorClassName));
- }
-
- public static String getFromVersion(String className) {
- String version = className.substring(className.length() - 5);
- return version.substring(0, version.indexOf('_'));
- }
-
- public static String getToVersion(String className) {
- String version = className.substring(className.length() - 5);
- return version.substring(version.indexOf('_') + 1, version.indexOf('_') + 3);
- }
-
- public static String getLowerVersionAbsPathFromAbsPath(String type1Path, String type2Path, String version) {
- String lowVersionString = getVersionString(version.substring(0, version.indexOf('_')));
- if (type1Path.contains(lowVersionString)) {
- return type1Path;
- } else {
- return type2Path;
- }
- }
-
- public static String getDirectoryOfOppositeModel(String importStatement, String version) {
- String lowVersionString = getVersionString(version.substring(0, version.indexOf('_')));
- String highVersionString = getVersionString(version.substring(version.indexOf('_') + 1, version.indexOf('_') + 3));
-
- if (importStatement.contains(lowVersionString)) {
- String temp = String.format(MODEL_IMPORT_PATH, highVersionString, "");
- return temp.substring(0, temp.lastIndexOf("model.") + "model.".length());
- } else {
- String temp = String.format(MODEL_IMPORT_PATH, lowVersionString, "");
- return temp.substring(0, temp.lastIndexOf("model.") + "model.".length());
- }
- }
-
- public static String getLowerVersionAbsPathFromImportStatement(String type1ImportStatement, String type2ImportStatement, String version) {
- String type1Path = importStatementToAbsolutePath(type1ImportStatement);
- String type2Path = importStatementToAbsolutePath(type2ImportStatement);
- return getLowerVersionAbsPathFromAbsPath(type1Path, type2Path, version);
- }
-
- public static String getHigherVersionAbsPath(String type1ImportStatement, String type2ImportStatement, String version) {
- String type1Path = importStatementToAbsolutePath(type1ImportStatement);
- String type2Path = importStatementToAbsolutePath(type2ImportStatement);
-
- String lowVersionString = getVersionString(version.substring(0, version.indexOf('_')));
- if (type1Path.contains(lowVersionString)) {
- return type2Path;
- } else {
- return type1Path;
- }
- }
-}
-
diff --git a/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/parser/ParserTest.java b/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/parser/ParserTest.java
deleted file mode 100644
index 5a38c98da..000000000
--- a/org.hl7.fhir.convertors/src/test/java/org/hl7/fhir/convertors/parser/ParserTest.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package org.hl7.fhir.convertors.parser;
-
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-public class ParserTest {
-
- @Test
- public void parseAllTheCodes() {
- }
-}
\ No newline at end of file