All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.code.java.InternalSourceFolderFinder Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.code.java;

import com.softicar.platform.common.core.exceptions.SofticarException;
import com.softicar.platform.common.core.java.packages.name.JavaPackageName;
import java.io.File;

/**
 * Determines the source folder for a given reference class.
 * 

* This class is only used internally by the {@link JavaCodePrinter}. The use of * this class makes only sense in a development environment like Eclipse. For * this reason, we cannot write a unit test for it. *

* This class assumes that the source code of the given reference class is * available in the file system. It is assumed that the name of the source * folder is either src/main/java or src. The former is preferred * when available. * * @author Oliver Richers */ class InternalSourceFolderFinder { private final Class referenceClass; public InternalSourceFolderFinder(Class referenceClass) { this.referenceClass = referenceClass; } public File findSourceFolder() { return findSourceFolder(getClasspathRootFolder()); } private File getClasspathRootFolder() { File absoluteFolder = new File(referenceClass.getResource(".").getFile()); File relativeFolder = new File(new JavaPackageName(referenceClass).getName("/")); while (relativeFolder != null && absoluteFolder.getName().equals(relativeFolder.getName())) { absoluteFolder = absoluteFolder.getParentFile(); relativeFolder = relativeFolder.getParentFile(); } return absoluteFolder; } private File findSourceFolder(File startFolder) { File sourceFolder = findFolder(startFolder, "src"); File sourceMainJavaFolder = new File(sourceFolder, "main/java"); if (sourceMainJavaFolder.exists() && sourceMainJavaFolder.isDirectory()) { return sourceMainJavaFolder; } else { return sourceFolder; } } private File findFolder(File startFolder, String folderName) { File folder = startFolder; while (folder != null) { File sourceFolder = new File(folder, folderName); if (sourceFolder.exists() && sourceFolder.isDirectory()) { return sourceFolder; } folder = folder.getParentFile(); } throw new SofticarException("Failed to find '%s' sub-folder in '%s' or any of its parents.", folderName, startFolder); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy