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

PersistenceMapKeyDiagnosticsCollector: Move body of the for loops over methods and fields into a common method. #1037

Merged
merged 3 commits into from
Oct 29, 2024
Merged
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation, Ankush Sharma and others.
* Copyright (c) 2020, 2024 IBM Corporation, Ankush Sharma and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -37,81 +37,56 @@ protected String getDiagnosticSource() {
@Override
public void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics) {
if (unit != null) {
PsiClass[] alltypes;
PsiAnnotation[] allAnnotations;

alltypes = unit.getClasses();
PsiClass[] alltypes = unit.getClasses();
PsiMethod[] methods;
PsiField[] fields;

for (PsiClass type : alltypes) {
methods = type.getMethods();
for (PsiMethod method : methods) {
List<PsiAnnotation> mapKeyJoinCols = new ArrayList<PsiAnnotation>();
boolean hasMapKeyAnnotation = false;
boolean hasMapKeyClassAnnotation = false;
allAnnotations = method.getAnnotations();
for (PsiAnnotation annotation : allAnnotations) {
String matchedAnnotation = getMatchedJavaElementName(type, annotation.getQualifiedName(),
PersistenceConstants.SET_OF_PERSISTENCE_ANNOTATIONS);
if (matchedAnnotation != null) {
if (PersistenceConstants.MAPKEY.equals(matchedAnnotation))
hasMapKeyAnnotation = true;
else if (PersistenceConstants.MAPKEYCLASS.equals(matchedAnnotation))
hasMapKeyClassAnnotation = true;
else if (PersistenceConstants.MAPKEYJOINCOLUMN.equals(matchedAnnotation)) {
mapKeyJoinCols.add(annotation);
}
}
}
if (hasMapKeyAnnotation && hasMapKeyClassAnnotation) {
// A single field cannot have the same
diagnostics.add(createDiagnostic(method, unit,
Messages.getMessage("MapKeyAnnotationsNotOnSameField"),
PersistenceConstants.DIAGNOSTIC_CODE_INVALID_ANNOTATION, null,
DiagnosticSeverity.Error));
}
// If we have multiple MapKeyJoinColumn annotations on a single method we must
// ensure each has a name and referencedColumnName
if (mapKeyJoinCols.size() > 1) {
validateMapKeyJoinColumnAnnotations(mapKeyJoinCols, method, unit, diagnostics);
}
collectDiagnostics(unit, diagnostics, type, method);
}

// Go through each field to ensure they do not have both MapKey and MapKeyColumn
// Annotations
fields = type.getFields();
for (PsiField field : fields) {
List<PsiAnnotation> mapKeyJoinCols = new ArrayList<PsiAnnotation>();
boolean hasMapKeyAnnotation = false;
boolean hasMapKeyClassAnnotation = false;
allAnnotations = field.getAnnotations();
for (PsiAnnotation annotation : allAnnotations) {
String matchedAnnotation = getMatchedJavaElementName(type, annotation.getQualifiedName(),
PersistenceConstants.SET_OF_PERSISTENCE_ANNOTATIONS);
if (matchedAnnotation != null) {
if (PersistenceConstants.MAPKEY.equals(matchedAnnotation))
hasMapKeyAnnotation = true;
else if (PersistenceConstants.MAPKEYCLASS.equals(matchedAnnotation))
hasMapKeyClassAnnotation = true;
else if (PersistenceConstants.MAPKEYJOINCOLUMN.equals(matchedAnnotation)) {
mapKeyJoinCols.add(annotation);
}
}
}
if (hasMapKeyAnnotation && hasMapKeyClassAnnotation) {
// A single field cannot have the same
diagnostics.add(createDiagnostic(field, unit,
Messages.getMessage("MapKeyAnnotationsNotOnSameField"),
PersistenceConstants.DIAGNOSTIC_CODE_INVALID_ANNOTATION, null,
DiagnosticSeverity.Error));
}
if (mapKeyJoinCols.size() > 1) {
validateMapKeyJoinColumnAnnotations(mapKeyJoinCols, field, unit, diagnostics);
}
collectDiagnostics(unit, diagnostics, type, field);
}
}
}
}

private void collectDiagnostics(PsiJavaFile unit, List<Diagnostic> diagnostics,
PsiClass type, PsiJvmModifiersOwner fieldOrProperty) {
TrevCraw marked this conversation as resolved.
Show resolved Hide resolved
List<PsiAnnotation> mapKeyJoinCols = new ArrayList<PsiAnnotation>();
boolean hasMapKeyAnnotation = false;
boolean hasMapKeyClassAnnotation = false;
PsiAnnotation[] allAnnotations = fieldOrProperty.getAnnotations();
for (PsiAnnotation annotation : allAnnotations) {
String matchedAnnotation = getMatchedJavaElementName(type, annotation.getQualifiedName(),
PersistenceConstants.SET_OF_PERSISTENCE_ANNOTATIONS);
if (matchedAnnotation != null) {
if (PersistenceConstants.MAPKEY.equals(matchedAnnotation))
hasMapKeyAnnotation = true;
else if (PersistenceConstants.MAPKEYCLASS.equals(matchedAnnotation))
hasMapKeyClassAnnotation = true;
else if (PersistenceConstants.MAPKEYJOINCOLUMN.equals(matchedAnnotation)) {
mapKeyJoinCols.add(annotation);
}
}
}
if (hasMapKeyAnnotation && hasMapKeyClassAnnotation) {
// A single field or property cannot have the same
TrevCraw marked this conversation as resolved.
Show resolved Hide resolved
diagnostics.add(createDiagnostic(fieldOrProperty, unit,
Messages.getMessage("MapKeyAnnotationsNotOnSameField"),
PersistenceConstants.DIAGNOSTIC_CODE_INVALID_ANNOTATION, null,
DiagnosticSeverity.Error));
}
// If we have multiple MapKeyJoinColumn annotations on a single field or property we must
// ensure each has a name and referencedColumnName
if (mapKeyJoinCols.size() > 1) {
validateMapKeyJoinColumnAnnotations(mapKeyJoinCols, fieldOrProperty, unit, diagnostics);
}
}

private void validateMapKeyJoinColumnAnnotations(List<PsiAnnotation> annotations, PsiElement element,
Expand All @@ -131,4 +106,5 @@ private void validateMapKeyJoinColumnAnnotations(List<PsiAnnotation> annotations
PersistenceConstants.DIAGNOSTIC_CODE_MISSING_ATTRIBUTES, null, DiagnosticSeverity.Error));
}
});
}}
}
}
Loading