Skip to content

Commit

Permalink
small improvements in annotation processing, logging and NPE handling
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <[email protected]>
  • Loading branch information
lukasj committed Sep 11, 2024
1 parent 1b44275 commit 2933409
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
exports org.eclipse.persistence.internal.localization to
org.eclipse.persistence.dbws,
org.eclipse.persistence.jpa,
org.eclipse.persistence.jpa.modelgen,
org.eclipse.persistence.jpars.server,
org.eclipse.persistence.moxy,
org.eclipse.persistence.oracle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,66 +838,74 @@ protected String getThreadString(Thread thread) {
/**
* Print the prefix string representing EclipseLink logging
*/

//Bug3135111 Prefix strings are not translated until the first time they are used.
protected void printPrefixString(int level, String category) {
try {
switch (level) {
this.getWriter().write(getPrefixString(level, category));
} catch (IOException exception) {
throw ValidationException.logIOError(exception);
}
}

/**
* Get the prefix string for the EclipseLink logging
*/
protected String getPrefixString(int level, String category) {
StringBuilder sb = new StringBuilder();
switch (level) {
case SEVERE:
if (SEVERE_PREFIX == null) {
SEVERE_PREFIX = LoggingLocalization.buildMessage("toplink_severe");
}
this.getWriter().write(SEVERE_PREFIX);
sb.append(SEVERE_PREFIX);
break;
case WARNING:
if (WARNING_PREFIX == null) {
WARNING_PREFIX = LoggingLocalization.buildMessage("toplink_warning");
}
this.getWriter().write(WARNING_PREFIX);
sb.append(WARNING_PREFIX);
break;
case INFO:
if (INFO_PREFIX == null) {
INFO_PREFIX = LoggingLocalization.buildMessage("toplink_info");
}
this.getWriter().write(INFO_PREFIX);
sb.append(INFO_PREFIX);
break;
case CONFIG:
if (CONFIG_PREFIX == null) {
CONFIG_PREFIX = LoggingLocalization.buildMessage("toplink_config");
}
this.getWriter().write(CONFIG_PREFIX);
sb.append(CONFIG_PREFIX);
break;
case FINE:
if (FINE_PREFIX == null) {
FINE_PREFIX = LoggingLocalization.buildMessage("toplink_fine");
}
this.getWriter().write(FINE_PREFIX);
sb.append(FINE_PREFIX);
break;
case FINER:
if (FINER_PREFIX == null) {
FINER_PREFIX = LoggingLocalization.buildMessage("toplink_finer");
}
this.getWriter().write(FINER_PREFIX);
sb.append(FINER_PREFIX);
break;
case FINEST:
if (FINEST_PREFIX == null) {
FINEST_PREFIX = LoggingLocalization.buildMessage("toplink_finest");
}
this.getWriter().write(FINEST_PREFIX);
sb.append(FINEST_PREFIX);
break;
default:
if (TOPLINK_PREFIX == null) {
TOPLINK_PREFIX = LoggingLocalization.buildMessage("toplink");
}
this.getWriter().write(TOPLINK_PREFIX);
}
if (category != null) {
this.getWriter().write(category);
this.getWriter().write(": ");
}
} catch (IOException exception) {
throw ValidationException.logIOError(exception);
sb.append(TOPLINK_PREFIX);
}
if (category != null) {
sb.append(category);
sb.append(": ");
}
return sb.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ private Attributes processNamedGraphs(MetadataProject project, ClassAccessor acc
for (NamedEntityGraphMetadata namedEntityGraph : project.getNamedEntityGraphs(accessor)) {
String name = namedEntityGraph.getName();
// if name is not present, default to the entity name
if (name == null) {
if (name == null || name.isEmpty()) {
if (accessor.isEntityAccessor()) {
name = ((EntityAccessor) accessor).getEntityName();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.tools.Diagnostic;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.localization.LoggingLocalization;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.logging.SessionLogEntry;
Expand Down Expand Up @@ -202,64 +201,6 @@ private void initOrUpdateLevels(Map<String, String> settings) {
}
}

private CharSequence getPrefixString(int level, String category) {
StringBuilder sb = new StringBuilder();
switch (level) {
case SEVERE:
if (SEVERE_PREFIX == null) {
SEVERE_PREFIX = LoggingLocalization.buildMessage("toplink_severe");
}
sb.append(SEVERE_PREFIX);
break;
case WARNING:
if (WARNING_PREFIX == null) {
WARNING_PREFIX = LoggingLocalization.buildMessage("toplink_warning");
}
sb.append(WARNING_PREFIX);
break;
case INFO:
if (INFO_PREFIX == null) {
INFO_PREFIX = LoggingLocalization.buildMessage("toplink_info");
}
sb.append(INFO_PREFIX);
break;
case CONFIG:
if (CONFIG_PREFIX == null) {
CONFIG_PREFIX = LoggingLocalization.buildMessage("toplink_config");
}
sb.append(CONFIG_PREFIX);
break;
case FINE:
if (FINE_PREFIX == null) {
FINE_PREFIX = LoggingLocalization.buildMessage("toplink_fine");
}
sb.append(FINE_PREFIX);
break;
case FINER:
if (FINER_PREFIX == null) {
FINER_PREFIX = LoggingLocalization.buildMessage("toplink_finer");
}
sb.append(FINER_PREFIX);
break;
case FINEST:
if (FINEST_PREFIX == null) {
FINEST_PREFIX = LoggingLocalization.buildMessage("toplink_finest");
}
sb.append(FINEST_PREFIX);
break;
default:
if (TOPLINK_PREFIX == null) {
TOPLINK_PREFIX = LoggingLocalization.buildMessage("toplink");
}
sb.append(TOPLINK_PREFIX);
}
if (category != null) {
sb.append(category);
sb.append(": ");
}
return sb;
}

private Diagnostic.Kind translateLevelToKind(int level) {
return switch (level) {
case SEVERE, WARNING -> Diagnostic.Kind.WARNING;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* 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 @@ -38,6 +38,7 @@
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
Expand Down Expand Up @@ -169,7 +170,6 @@ public MetadataClass getMetadataClass(Element element) {
metadataClass = new MetadataClass(MetadataMirrorFactory.this, name);
addMetadataClass(metadataClass);
element.accept(elementVisitor, metadataClass);
addMetadataClass(metadataClass);
} else {
// Only thing going to get through at this point are
// TypeParameterElements (presumably generic ones). Look
Expand All @@ -180,7 +180,6 @@ public MetadataClass getMetadataClass(Element element) {
metadataClass = new MetadataClass(MetadataMirrorFactory.this, name);
addMetadataClass(metadataClass);
element.accept(elementVisitor, metadataClass);
addMetadataClass(metadataClass);
}
} else {
// Array types etc ...
Expand Down Expand Up @@ -281,14 +280,14 @@ public Map<Element, MetadataClass> getRoundElements() {
}

/**
* INTENAL:
* INTERNAL:
*/
public boolean isRoundElement(Element element) {
return roundElements.containsKey(element);
}

/**
* INTENAL:
* INTERNAL:
*/
public boolean isRoundElement(MetadataClass cls) {
return roundMetadataClasses.contains(cls);
Expand All @@ -302,9 +301,12 @@ boolean isProcessed(Element name) {
return processedElements.contains(name);
}

/**
* INTERNAL:
*/
@Override
public boolean isInterface(MetadataClass metadataClass) {
TypeElement element = processingEnv.getElementUtils().getTypeElement(metadataClass.getName());
return element != null && element.getKind() == ElementKind.INTERFACE;
}

@Override
public void resolveGenericTypes(MetadataClass child, List<String> genericTypes, MetadataClass parent, MetadataDescriptor descriptor) {
// Our metadata factory does not and can not resolve the types since
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* 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 @@ -378,8 +378,8 @@ public String getQualifiedCanonicalName(String qualifiedName) {
*/
public String getPersistenceUnitProperty(final String name) {
Object objVal = persistenceUnitProperties.get(name);
if (objVal instanceof String) {
return (String) objVal;
if (objVal instanceof String s) {
return s;
} else {
return objVal != null ? objVal.toString() : null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* 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 All @@ -17,14 +17,17 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractAnnotationValueVisitor8;
import javax.lang.model.util.AbstractAnnotationValueVisitor14;
import javax.lang.model.util.Elements;

import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotation;

Expand All @@ -34,11 +37,16 @@
* @author Guy Pelletier
* @since EclipseLink 1.2
*/
public class AnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor8<Object, Object> {
public class AnnotationValueVisitor<R, P> extends AbstractAnnotationValueVisitor14<Object, Object> {

private final Elements elementUtils;

/**
* INTERNAL:
*/
public AnnotationValueVisitor() {}
public AnnotationValueVisitor(ProcessingEnvironment processingEnv) {
elementUtils = processingEnv.getElementUtils();
}

/**
* INTERNAL:
Expand All @@ -52,11 +60,9 @@ public Object visitAnnotation(AnnotationMirror annotationMirror, Object arg1) {
annotation.setName(annotationMirror.getAnnotationType().toString());

// Process the values.
Set<? extends ExecutableElement> keys = annotationMirror.getElementValues().keySet();

for (ExecutableElement annotationElement : keys) {
AnnotationValue annotationValue = annotationMirror.getElementValues().get(annotationElement);
String attribute = annotationElement.getSimpleName().toString();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementUtils.getElementValuesWithDefaults(annotationMirror).entrySet()) {
String attribute = entry.getKey().getSimpleName().toString();
AnnotationValue annotationValue = entry.getValue();
Object attributeValue = annotationValue.accept(this, arg1);
annotation.addAttribute(attribute, attributeValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* 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 @@ -31,11 +31,14 @@
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractElementVisitor14;
import javax.lang.model.util.AbstractElementVisitor8;
import javax.tools.Diagnostic;

Expand All @@ -55,7 +58,7 @@
* @author Guy Pelletier
* @since EclipseLink 1.2
*/
public class ElementVisitor<R, P> extends AbstractElementVisitor8<MetadataAnnotatedElement, MetadataClass> {
public class ElementVisitor<R, P> extends AbstractElementVisitor14<MetadataAnnotatedElement, MetadataClass> {
private ProcessingEnvironment processingEnv;
private TypeVisitor<MetadataAnnotatedElement, MetadataAnnotatedElement> typeVisitor;

Expand All @@ -75,7 +78,7 @@ public ElementVisitor(ProcessingEnvironment processingEnv) {
* and build complete MetadataAnnotation from the mirrors.
*/
protected void buildMetadataAnnotations(MetadataAnnotatedElement annotatedElement, List<? extends AnnotationMirror> annotationMirrors) {
AnnotationValueVisitor<Object, Object> visitor = new AnnotationValueVisitor<>();
AnnotationValueVisitor<Object, Object> visitor = new AnnotationValueVisitor<>(processingEnv);

for (AnnotationMirror annotationMirror : annotationMirrors) {
String annotation = annotationMirror.getAnnotationType().toString() ;
Expand Down Expand Up @@ -303,6 +306,25 @@ public MetadataField visitVariable(VariableElement variableElement, MetadataClas
@Override
public MetadataAnnotatedElement visitUnknown(Element e, MetadataClass metadataClass) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unsupported element", e);
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unsupported element in " + metadataClass.getName() + ": " + e.toString(), e);
return null;
}

@Override
public MetadataAnnotatedElement visitRecordComponent(RecordComponentElement recordElement, MetadataClass metadataClass) {
MetadataLogger logger = metadataClass.getMetadataFactory().getLogger();
logger.getSession().getSessionLog().log(SessionLog.FINE, SessionLog.PROCESSOR,
"ElementVisitor Record NOT IMPLEMENTED : {0}",
new Object[] {recordElement}, false);
return null;
}

@Override
public MetadataAnnotatedElement visitModule(ModuleElement moduleElement, MetadataClass metadataClass) {
MetadataLogger logger = metadataClass.getMetadataFactory().getLogger();
logger.getSession().getSessionLog().log(SessionLog.FINE, SessionLog.PROCESSOR,
"ElementVisitor Module NOT IMPLEMENTED : {0}",
new Object[] {moduleElement}, false);
return null;
}
}
Loading

0 comments on commit 2933409

Please sign in to comment.