use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.
the class JavaInputReader method getInputObjects.
/**
* Returns all input objects for the given container input.
*
* @param input container input (only {@link PackageFolder} instances will be supported)
* @param inputCharset {@link Charset} to be used to read the children
* @param recursively states, whether the children should be retrieved recursively
* @return the list of children. In this case {@link File} objects
*/
public List<Object> getInputObjects(Object input, Charset inputCharset, boolean recursively) {
LOG.debug("Retrieve input object for input {} {}", input, recursively ? "recursively" : "");
List<Object> javaClasses = new LinkedList<>();
if (input instanceof PackageFolder) {
File packageFolder = new File(((PackageFolder) input).getLocation());
List<File> files = retrieveAllJavaSourceFiles(packageFolder, recursively);
for (File f : files) {
ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
classLibraryBuilder.appendDefaultClassLoaders();
ClassLoader containerClassloader = ((PackageFolder) input).getClassLoader();
if (containerClassloader != null) {
classLibraryBuilder.appendClassLoader(containerClassloader);
}
try (FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis, inputCharset)) {
classLibraryBuilder.addSource(isr);
JavaSource source = null;
for (JavaSource s : classLibraryBuilder.getClassLibrary().getJavaSources()) {
source = s;
// only consider one class per file
break;
}
if (source != null) {
// save cast as given by the customized builder
if (source.getClasses().size() > 0) {
JavaClass javaClass = source.getClasses().get(0);
// try loading class
if (containerClassloader != null) {
try {
Class<?> loadedClass = containerClassloader.loadClass(javaClass.getCanonicalName());
javaClasses.add(new Object[] { javaClass, loadedClass });
} catch (ClassNotFoundException e) {
LOG.info("Could not load Java type '{}' with the containers class loader. " + "Just returning the parsed Java model.", javaClass.getCanonicalName());
javaClasses.add(javaClass);
}
} else {
javaClasses.add(javaClass);
}
}
}
} catch (IOException e) {
LOG.error("The file {} could not be parsed as a java class", f.getAbsolutePath().toString(), e);
}
}
}
LOG.debug("{} java classes found!", javaClasses.size());
return javaClasses;
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.
the class JavaParserUtil method getFirstJavaClass.
/**
* Returns the first {@link JavaClass} parsed by the given {@link Reader}, all upcoming parsed java files will be
* added to the class library
*
* @param reader {@link Reader}s which contents should be parsed
* @return the parsed {@link JavaClass}
*/
public static JavaClass getFirstJavaClass(Reader... reader) {
ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
classLibraryBuilder.appendDefaultClassLoaders();
return getFirstJavaClass(classLibraryBuilder, reader);
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.
the class JavaParserUtil method getFirstJavaClass.
/**
* Returns the first {@link JavaClass} parsed by the given {@link Reader}, all upcoming parsed java files will be
* added to the class library. By passing a {@link ClassLoader}, you can take impact on the class name resolving
*
* @param classLoader which should be used for class name resolving
* @param reader {@link Reader}s which contents should be parsed
* @return the parsed {@link JavaClass}
*/
public static JavaClass getFirstJavaClass(ClassLoader classLoader, Reader... reader) {
ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
classLibraryBuilder.appendClassLoader(classLoader);
return getFirstJavaClass(classLibraryBuilder, reader);
}
use of com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder in project cobigen by devonfw.
the class JavaMergerTest method testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces.
/**
* Tests merging java without adding new lines to method bodies (was a bug)
*
* @throws IOException test fails
* @throws MergeException test fails
*/
@Test
public void testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces() throws IOException, MergeException {
File file = new File(testFileRootPath + "PatchFile_method.java");
ClassLibraryBuilder classLibraryBuilder = new ModifyableClassLibraryBuilder();
try (FileInputStream fileInputStream = new FileInputStream(file)) {
JavaSource source = classLibraryBuilder.addSource(fileInputStream);
JavaClass origClazz = source.getClasses().get(0);
String mergedContents = new JavaMerger("", true).merge(file, FileUtils.readFileToString(file, Charset.forName("UTF-8")), "UTF-8");
classLibraryBuilder = new ModifyableClassLibraryBuilder();
try (StringReader stringReader = new StringReader(mergedContents)) {
source = classLibraryBuilder.addSource(stringReader);
JavaClass resultClazz = source.getClasses().get(0);
for (JavaMethod method : resultClazz.getMethods()) {
JavaMethod origMethod = origClazz.getMethodBySignature(method.getName(), method.getParameterTypes());
assertThat(method.getCodeBlock()).isEqualTo(origMethod.getCodeBlock());
}
}
}
}
Aggregations