Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use annotations of the patchfile, if javamerge_override is set. #1490

Merged
merged 6 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.devonfw.cobigen.api.exception.MergeException;
import com.devonfw.cobigen.api.extension.Merger;
import com.devonfw.cobigen.api.util.StringUtil;
import com.devonfw.cobigen.api.util.SystemUtil;
import com.devonfw.cobigen.javaplugin.inputreader.JavaParserUtil;
import com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass;
import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaConstructor;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaInitializer;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.impl.DefaultJavaField;
import com.thoughtworks.qdox.model.impl.DefaultJavaMethod;
import com.thoughtworks.qdox.parser.ParseException;

/**
Expand Down Expand Up @@ -112,6 +118,7 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
*/
private ModifyableJavaClass merge(ModifyableJavaClass baseClass, ModifyableJavaClass patchClass) {

baseClass.setAnnotations(mergeAnnotation(baseClass.getAnnotations(), patchClass.getAnnotations()));
mergeImports(baseClass, patchClass);
mergeFields(baseClass, patchClass);
mergeInnerClasses(baseClass, patchClass);
Expand All @@ -120,6 +127,24 @@ private ModifyableJavaClass merge(ModifyableJavaClass baseClass, ModifyableJavaC
return baseClass;
}

/**
LarsReinken marked this conversation as resolved.
Show resolved Hide resolved
* @param baseAnnotations {@link JavaClass} a List of annotations originate from the base class
* @param patchAnnotations {@link JavaClass} a List of annotations originate from the patch class
*/
private List<JavaAnnotation> mergeAnnotation(List<JavaAnnotation> baseAnnotations,
List<JavaAnnotation> patchAnnotations) {

List<JavaAnnotation> bAnnotations = this.patchOverrides ? patchAnnotations : baseAnnotations;
List<JavaAnnotation> pAnnotations = this.patchOverrides ? baseAnnotations : patchAnnotations;

Set<String> annotationNames = bAnnotations.stream().map(a -> a.getType().getName()).collect(Collectors.toSet());

List<JavaAnnotation> annotationsToMerge = pAnnotations.stream()
.filter(a -> !annotationNames.contains(a.getType().getName())).collect(Collectors.toList());

return Stream.of(bAnnotations, annotationsToMerge).flatMap(a -> a.stream()).collect(Collectors.toList());
}

/**
* Merges all super types of the two class sources
*
Expand Down Expand Up @@ -231,9 +256,11 @@ private void mergeFields(ModifyableJavaClass baseClass, ModifyableJavaClass patc
if (baseField == null) {
baseClass.addField(patchField);
} else {
((DefaultJavaField) baseField)
.setAnnotations(new ArrayList<>(mergeAnnotation(baseField.getAnnotations(), patchField.getAnnotations())));
if (this.patchOverrides) {
baseClass.replace(baseField, patchField);
} // else do not override
}
}
}
}
Expand Down Expand Up @@ -288,11 +315,14 @@ private void mergeMethods(ModifyableJavaClass baseClass, ModifyableJavaClass pat
if (baseMethod == null) {
baseClass.addMethod(patchMethod);
} else {
((DefaultJavaMethod) baseMethod)
.setAnnotations(mergeAnnotation(baseMethod.getAnnotations(), patchMethod.getAnnotations()));
if (this.patchOverrides) {
baseClass.replace(baseMethod, patchMethod);
} // else do not override
}
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,86 @@ private JavaSource getMergedSource(File baseFile, File patchFile, boolean overri
return getFirstJavaClass(new StringReader(mergedContents)).getSource();
}

/**
* Tests merging the class annotation into the baseFile
*
* @throws IOException shouldn't happen
* @throws MergeException shouldn't happen either
*/
@Test
public void testMergeClassAnnotation() throws IOException, MergeException {

File baseFile = new File(testFileRootPath + "BaseFile_ClassAnnotation.java");
File patchFile = new File(testFileRootPath + "PatchFile_ClassAnnotation.java");

// with override
String mergedContents = new JavaMerger("", true).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Entity");
assertThat(mergedContents).contains("@javax.persistence.Table(name=\"Visitor\")");

// without override
mergedContents = new JavaMerger("", false).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Entity");
assertThat(mergedContents).contains("@javax.persistence.Table(name=\"Visits\")");
}

/**
* Tests merging a property annotation into the baseFile
*
* @throws IOException shouldn't happen
* @throws MergeException shouldn't happen either
*/
@Test
public void testMergePropertyAnnotation() throws IOException, MergeException {

File baseFile = new File(testFileRootPath + "BaseFile_PropertyAnnotation.java");
File patchFile = new File(testFileRootPath + "PatchFile_PropertyAnnotation.java");

// with override
String mergedContents = new JavaMerger("", false).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Column(name=\"USER\")");
assertThat(mergedContents).contains("@Column(name=\"NAME\")");

// without override

mergedContents = new JavaMerger("", true).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Column(name= \"USERNAME\")");
assertThat(mergedContents).contains("@Column(name=\"NAME\")");

}

/**
* Tests merging a Method annotation into the baseFile
*
* @throws IOException shouldn't happen
* @throws MergeException shouldn't happen either
*/
@Test
public void testMergeMethodAnnotation() throws IOException, MergeException {

File baseFile = new File(testFileRootPath + "BaseFile_MethodAnnotation.java");
File patchFile = new File(testFileRootPath + "PatchFile_MethodAnnotation.java");

// with override
String mergedContents = new JavaMerger("", true).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Column(name=\"USERNAME\")");
assertThat(mergedContents).contains("@Column(name=\"USER\")");

// without override
mergedContents = new JavaMerger("", false).merge(baseFile,
FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");

assertThat(mergedContents).contains("@Column(name=\"NAME\")");
assertThat(mergedContents).contains("@Column(name=\"USER\")");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.devonfw.application.jtqj.general.dataaccess.api;

import com.devonfw.application.jtqj.general.common.api.Visitor;

@javax.persistence.Table(name = "Visits")
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

private String username;

private String name;

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.devonfw;

/**
* Data access object for Visitor entities
*/
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

private String username;

private String name;

@Column(name = "NAME")
public String getUsername() {
return this.username;
}

public String getName() {
return this.Name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.devonfw.application.jtqj.general.dataaccess.api;

import javax.persistence.Entity;
import com.devonfw.application.jtqj.general.common.api.Visitor;

/**
* Data access object for Visitor entities
*/
@Entity
@javax.persistence.Table(name = "Visitor")
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

@Column(name = "USER")
private String username;

private String name;

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.devonfw.application.jtqj.general.dataaccess.api;

import javax.persistence.Entity;

import com.devonfw.application.jtqj.general.common.api.Visitor;

/**
* Data access object for Visitor entities
*/
@Entity
@javax.persistence.Table(name = "Visitor")
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devonfw;

/**
* Data access object for Visitor entities
*/
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

private String username;

private String name;

@Column(name = "USERNAME")
public String getUsername() {
return this.username;
}

@Column(name = "USER")
public String getName() {
return this.Name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devonfw.application.jtqj.general.dataaccess.api;

import javax.persistence.Entity;

import com.devonfw.application.jtqj.general.common.api.Visitor;

/**
* Data access object for Visitor entities
*/
@Entity
@javax.persistence.Table(name = "Visitor")
public class VisitorEntity extends ApplicationPersistenceEntity implements Visitor {

private static final long serialVersionUID = 1L;

@Column(name = "USERNAME")
private String username;


@Column(name = "NAME")
private String name;
}