Skip to content

Commit

Permalink
properly merged versions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Jun 17, 2020
2 parents 3588813 + 60868e0 commit 76e24b5
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>javaplugin-model</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
<name>CobiGen Javaplugin Model</name>
<packaging>jar</packaging>
<description>CobiGen - Java Plug-in Model</description>
Expand Down
8 changes: 4 additions & 4 deletions cobigen/cobigen-javaplugin-parent/cobigen-javaplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>javaplugin</artifactId>
<name>CobiGen - Java Plug-in</name>
<version>2.2.0</version>
<version>2.2.1</version>
<packaging>jar</packaging>
<description>CobiGen - Java Plug-in</description>

Expand All @@ -27,7 +27,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core-api</artifactId>
<version>5.3.0</version>
<version>6.1.0</version>
</dependency>

<dependency>
Expand All @@ -39,13 +39,13 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core-test</artifactId>
<version>6.0.0</version>
<version>6.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core</artifactId>
<version>6.0.0</version>
<version>6.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devonfw.cobigen.javaplugin.merger;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -63,18 +64,22 @@ public String getType() {
public String merge(File base, String patch, String targetCharset) throws MergeException {

ModifyableJavaClass baseClass;
try {
baseClass = (ModifyableJavaClass) JavaParserUtil
.getFirstJavaClass(new InputStreamReader(new FileInputStream(base), targetCharset));
String lineDelimiter;
try (FileInputStream stream = new FileInputStream(base);
BufferedInputStream bis = new BufferedInputStream(stream);
InputStreamReader reader = new InputStreamReader(bis, targetCharset)) {
lineDelimiter = determineLineDelimiter(bis, reader);
baseClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
} catch (IOException e) {
throw new MergeException(base, "Cannot read base file.", e);
} catch (ParseException e) {
throw new MergeException(base, "The syntax of the base file is invalid. Error in line: " + e.getLine()
+ " / column: " + e.getColumn() + ": " + e.getMessage(), e);
}
ModifyableJavaClass patchClass;
try {
patchClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(new StringReader(patch));
try (StringReader reader = new StringReader(patch)) {

patchClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
} catch (ParseException e) {
throw new MergeException(base, "The syntax of the generated patch is invalid. Error in line: " + e.getLine()
+ " / column: " + e.getColumn() + ": " + e.getMessage(), e);
Expand All @@ -87,20 +92,70 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
}

ModifyableJavaClass mergedClass = merge(baseClass, patchClass);
return consolidateLineEndings(mergedClass.getSource().getCodeBlock());
return consolidateLineEndings(mergedClass.getSource().getCodeBlock(), lineDelimiter);
}

/**
* @param bis
* The {@link BufferedInputStream} containing the input file
* @param reader
* The {@link InputStreamReader} iterating over the Stream
* @return The line delimiter corresponding to the input file
* @throws IOException
* If an exception occurs while processing the {@link BufferedInputStream} or the
* {@link InputStreamReader}
*/
private String determineLineDelimiter(BufferedInputStream bis, InputStreamReader reader) throws IOException {

bis.mark(0);
try {
while (reader.ready()) {
int nextChar = reader.read();
if (nextChar == '\r') {
nextChar = reader.read();
if (nextChar == '\n') {
return "\r\n";
}
return "\r";
} else if (nextChar == '\n') {
return "\n";
}
}
return null;
} finally {
emptyReader(reader);
bis.reset();
}
}

/**
* @param reader
* The {@link InputStreamReader} that is to be emptied
* @throws IOException
* If an exception occurs while processing the {@link InputStreamReader}
*/
private void emptyReader(InputStreamReader reader) throws IOException {
while (reader.ready()) {
reader.read();
}

}

/**
* Consolidates all line endings to the System default
*
* @param codeBlock
* which should be consolidate
* @param lineDelimiter
* the line delimiter of the file or null if none
* @return the consolidated code block
* @author mbrunnli (04.06.2013)
*/
private String consolidateLineEndings(String codeBlock) {

return codeBlock.replaceAll("\r\n|\r|\n", System.getProperty("line.separator"));
private String consolidateLineEndings(String codeBlock, String lineDelimiter) {
if (lineDelimiter != null) {
return codeBlock.replaceAll("\r\n|\r|\n", lineDelimiter);
}
return codeBlock;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public ModelWriter writeConstructor(JavaConstructor constructor) {
}

buffer.write(" {");
buffer.newline();
if (constructor.getSourceCode() != null) {
buffer.write(constructor.getSourceCode());
}
Expand Down Expand Up @@ -404,6 +403,9 @@ public ModelWriter writeAnnotation(JavaAnnotation annotation) {
buffer.write("{");
for (int i = 0; i < annotations.toArray().length; i++) {
if (a[i] instanceof JavaAnnotation) {
if (i > 0) {
buffer.write(", ");
}
writeAnnotation((JavaAnnotation) a[i]);
} else {
if (i > 0) {
Expand Down Expand Up @@ -477,7 +479,9 @@ protected void commentHeader(JavaAnnotatedElement entity) {
}
if (entity.getAnnotations() != null) {
for (JavaAnnotation annotation : entity.getAnnotations()) {
writeAnnotation(annotation);
if (entity.getAnnotations().get(entity.getAnnotations().size() - 1) != null) {
writeAnnotation(annotation);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
//simple annotation types which are still available at runtime
@Retention(RetentionPolicy.RUNTIME)
public @interface MyFieldAnnotation {
byte b();
byte b() default 0;

short s();
short s() default 0;

int i();
int i() default 0;

long l();
long l() default 0l;

float f();
float f() default 0;

double d();
double d() default 0.0;

char c();
char c() default '0';

String str();
String str() default "0";

boolean bool();
boolean bool() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

//simple annotation types which are still available at runtime
@Retention(RetentionPolicy.RUNTIME)
public @interface MyMethodAnnotation {

boolean bool();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.devonfw.cobigen.javaplugin.unittest.inputreader.testdata;

import java.beans.BeanProperty;

public class TestClassWithAnnotations {

private boolean boolvalue;
Expand All @@ -10,7 +8,7 @@ public class TestClassWithAnnotations {
* Returns the field 'boolvalue'
* @return value of boolvalue
*/
@BeanProperty(bound = true)
@MyFieldAnnotation(bool = true)
public boolean isBoolvalue() {
return boolvalue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ public void testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces() throws I
}

/**
* Tests issue <a href=https://github.com/oasp/cobigen/issues/39>#39</a>: inheritance relation
* should be merged also if the base class (natively) extends java.lang.Object
* Tests issue <a href=https://github.com/devonfw/cobigen/issues/39>#39</a>: inheritance relation should
* be merged also if the base class (natively) extends java.lang.Object
* @throws IOException
* test fails
* @throws MergeException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,26 @@ public void testCorrectNameOutputForOwnAnnotation() throws Exception {
String reprintedClass = target.toString();
assertThat(reprintedClass).contains("{\"abc\", \"cde\"}");
}

/**
* Tests the output of the CustomModelWriter regarding an own Annotation with multiple grouped annotations
* and the focus on correct comma placement.
*
* See https://github.com/devonfw/cobigen/issues/1070
* @throws Exception
* test fails
*/
@Test
public void testCorrectSyntaxOutputForArraysCommaPlacement() throws Exception {
File file = new File(testFileRootPath + "ArraySyntax.java");
CustomModelWriter target = new CustomModelWriter();
JavaClass parsedClass = JavaParserUtil.getFirstJavaClass(new FileReader(file));
target.writeClass(parsedClass);

String reprintedClass = target.toString();
Pattern p1 = Pattern.compile(
"@[A-Za-z]+\\(\\{.+\\{.+\\s*.+\\}.+\\{.+((\\(.+\\(.+\\).+\\)\\,.+)|(\\(.+\\(.+\\).+\\).+\\,.+))+(\\(.+\\(.+\\).+\\)[^\\,]+){1,1}\\}\\).+\\}\\)\\s*public.+",
Pattern.DOTALL);
assertThat(reprintedClass).matches(p1);
}
}
2 changes: 1 addition & 1 deletion documentation/master-cobigen.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This document contains the documentation of the CobiGen core module as well as a
---

* CobiGen v6.1.1
* CobiGen - Java Plug-in v2.2.0
* CobiGen - Java Plug-in v2.2.1
* CobiGen - XML Plug-in v4.2.0
* CobiGen - TypeScript Plug-in v2.4.3
* CobiGen - Property Plug-in v2.1.0
Expand Down

0 comments on commit 76e24b5

Please sign in to comment.