Skip to content

Commit

Permalink
Migrate to Java 17 switch (eclipse-pde#584)
Browse files Browse the repository at this point in the history
- changed to Java 17 Switch Expression
- replace switch-statements by if/else-statements where suitable

Co-authored-by: Hannes Wellmann <[email protected]>
  • Loading branch information
wide4head and HannesWell committed May 1, 2023
1 parent 59a2a32 commit b1a4e1c
Show file tree
Hide file tree
Showing 27 changed files with 484 additions and 808 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ public String toString() {
* @return the textual representation of an {@link IApiAccess}
*/
public static String getAccessText(int access) {
switch (access) {
case IApiAccess.NORMAL:
return "NORMAL"; //$NON-NLS-1$
case IApiAccess.FRIEND:
return "FRIEND"; //$NON-NLS-1$
default:
break;
}
return "<UNKNOWN ACCESS LEVEL>"; //$NON-NLS-1$
return switch (access)
{
case IApiAccess.NORMAL -> "NORMAL"; //$NON-NLS-1$
case IApiAccess.FRIEND -> "FRIEND"; //$NON-NLS-1$
default -> "<UNKNOWN ACCESS LEVEL>"; //$NON-NLS-1$
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,11 @@ private IApiBaseline setBaseline(File baselinePath) throws CoreException {
IStatus resolutionStatus = definition.resolve(new NullProgressMonitor());
switch (resolutionStatus.getSeverity())
{
case IStatus.WARNING:
case IStatus.WARNING ->
System.out.println("WARNING resolving target platform: " + resolutionStatus.getMessage()); //$NON-NLS-1$
break;
case IStatus.ERROR:
case IStatus.ERROR ->
throw new CoreException(resolutionStatus);
default: // Nothing
default -> { /*Nothing*/ }
}
// remove ".target"
String baselineName = baselineFileName.substring(0, baselineFileName.lastIndexOf('.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,23 @@ public String toString() {
String type = null;
String name = null;
switch (element.getElementType()) {
case IElementDescriptor.FIELD: {
case IElementDescriptor.FIELD -> {
type = "Field"; //$NON-NLS-1$
name = ((IMemberDescriptor) element).getName();
break;
}
case IElementDescriptor.METHOD: {
case IElementDescriptor.METHOD -> {
type = "Method"; //$NON-NLS-1$
name = ((IMemberDescriptor) element).getName();
break;
}
case IElementDescriptor.PACKAGE: {
case IElementDescriptor.PACKAGE -> {
type = "Package"; //$NON-NLS-1$
name = ((IPackageDescriptor) element).getName();
break;
}
case IElementDescriptor.TYPE: {
case IElementDescriptor.TYPE -> {
type = "Type"; //$NON-NLS-1$
name = ((IMemberDescriptor) element).getName();
break;
}
default:
break;
default -> { /**/ }
}
StringBuilder buffer = new StringBuilder();
buffer.append(type == null ? "Unknown" : type).append(" Node: ").append(name == null ? "Unknown Name" : name); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Expand Down Expand Up @@ -214,25 +209,23 @@ void persistXML(Document document, Element parentElement) {
return;
}
switch (element.getElementType()) {
case IElementDescriptor.METHOD: {
case IElementDescriptor.METHOD -> {
IMethodDescriptor md = (IMethodDescriptor) element;
Element method = document.createElement(IApiXmlConstants.ELEMENT_METHOD);
method.setAttribute(IApiXmlConstants.ATTR_NAME, md.getName());
method.setAttribute(IApiXmlConstants.ATTR_SIGNATURE, md.getSignature());
persistAnnotations(method);
parentElement.appendChild(method);
break;
}
case IElementDescriptor.FIELD: {
case IElementDescriptor.FIELD -> {
IFieldDescriptor fd = (IFieldDescriptor) element;
Element field = document.createElement(IApiXmlConstants.ELEMENT_FIELD);
field.setAttribute(IApiXmlConstants.ATTR_NAME, fd.getName());
persistAnnotations(field);
parentElement.appendChild(field);
break;
}
default:
break;
default -> { /**/ }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,17 @@ synchronized void projectClasspathChanged(IJavaProject project) {
*/
void flushElementCache(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.COMPILATION_UNIT: {
case IJavaElement.COMPILATION_UNIT -> {
ICompilationUnit unit = (ICompilationUnit) element;
IType type = unit.findPrimaryType();
if (type != null) {
ApiModelCache.getCache().removeElementInfo(ApiBaselineManager.WORKSPACE_API_BASELINE_ID, element.getJavaProject().getElementName(), type.getFullyQualifiedName(), IApiElement.TYPE);
}
break;
}
case IJavaElement.JAVA_PROJECT: {
case IJavaElement.JAVA_PROJECT -> {
ApiModelCache.getCache().removeElementInfo(ApiBaselineManager.WORKSPACE_API_BASELINE_ID, element.getElementName(), null, IApiElement.COMPONENT);
break;
}
default:
break;
default -> { /**/ }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,21 @@ static class DescriptionVisitor extends ApiDescriptionVisitor {

@Override
public boolean visitElement(IElementDescriptor element, IApiAnnotations description) {
switch (element.getElementType()) {
case IElementDescriptor.PACKAGE: {
return true;
return switch (element.getElementType())
{
case IElementDescriptor.PACKAGE -> {
yield true;
}
case IElementDescriptor.TYPE: {
case IElementDescriptor.TYPE -> {
members.clear();
members.add(element);
return true;
yield true;
}
default: {
default -> {
members.add(element);
yield false;
}
}
return false;
};
}

@Override
Expand Down Expand Up @@ -342,15 +343,15 @@ private String[] collectMissingTags(IApiAnnotations api, List<TagElement> tags,
ArrayList<String> missing = new ArrayList<>();
JavadocTagManager jtm = ApiPlugin.getJavadocTagManager();
switch (member) {
case IApiJavadocTag.MEMBER_FIELD:
case IApiJavadocTag.MEMBER_FIELD -> {
if (RestrictionModifiers.isReferenceRestriction(res)) {
if (!containsRestrictionTag(tags, "@noreference")) { //$NON-NLS-1$
IApiJavadocTag tag = jtm.getTag(IApiJavadocTag.NO_REFERENCE_TAG_ID);
missing.add(tag.getCompleteTag(type, member));
}
}
break;
case IApiJavadocTag.MEMBER_METHOD:
}
case IApiJavadocTag.MEMBER_METHOD -> {
if (RestrictionModifiers.isReferenceRestriction(res)) {
if (!containsRestrictionTag(tags, "@noreference")) { //$NON-NLS-1$
IApiJavadocTag tag = jtm.getTag(IApiJavadocTag.NO_REFERENCE_TAG_ID);
Expand All @@ -363,8 +364,8 @@ private String[] collectMissingTags(IApiAnnotations api, List<TagElement> tags,
missing.add(tag.getCompleteTag(type, member));
}
}
break;
case IApiJavadocTag.MEMBER_NONE:
}
case IApiJavadocTag.MEMBER_NONE -> {
if (RestrictionModifiers.isImplementRestriction(res)) {
if (!containsRestrictionTag(tags, "@noimplement")) { //$NON-NLS-1$
IApiJavadocTag tag = jtm.getTag(IApiJavadocTag.NO_IMPLEMENT_TAG_ID);
Expand All @@ -383,9 +384,8 @@ private String[] collectMissingTags(IApiAnnotations api, List<TagElement> tags,
missing.add(tag.getCompleteTag(type, member));
}
}
break;
default:
break;
}
default -> { /**/ }
}
return missing.toArray(new String[missing.size()]);
}
Expand Down Expand Up @@ -414,27 +414,23 @@ private boolean containsRestrictionTag(List<TagElement> tags, String tag) {
private IElementDescriptor findDescriptorByName(String name, String signature) {
for (IElementDescriptor desc : apis) {
switch (desc.getElementType()) {
case IElementDescriptor.TYPE: {
case IElementDescriptor.TYPE -> {
if (((IReferenceTypeDescriptor) desc).getName().equals(name)) {
return desc;
}
break;
}
case IElementDescriptor.METHOD: {
case IElementDescriptor.METHOD -> {
IMethodDescriptor method = (IMethodDescriptor) desc;
if (method.getName().equals(name) && method.getSignature().equals(signature)) {
return desc;
}
break;
}
case IElementDescriptor.FIELD: {
case IElementDescriptor.FIELD -> {
if (((IFieldDescriptor) desc).getName().equals(name)) {
return desc;
}
break;
}
default:
break;
default -> { /**/ }
}
}
return null;
Expand Down Expand Up @@ -674,19 +670,17 @@ private static int getRestrictions(final IJavaProject project, final Element ele
res = Integer.parseInt(element.getAttribute(IApiXmlConstants.ATTR_RESTRICTIONS));
} else {
switch (descriptor.getElementType()) {
case IElementDescriptor.FIELD: {
case IElementDescriptor.FIELD -> {
res = annotateRestriction(element, IApiXmlConstants.ATTR_REFERENCE, RestrictionModifiers.NO_REFERENCE, res);
break;
}
case IElementDescriptor.METHOD: {
case IElementDescriptor.METHOD -> {
IMethodDescriptor method = (IMethodDescriptor) descriptor;
res = annotateRestriction(element, IApiXmlConstants.ATTR_REFERENCE, RestrictionModifiers.NO_REFERENCE, res);
if (!method.isConstructor()) {
res = annotateRestriction(element, IApiXmlConstants.ATTR_OVERRIDE, RestrictionModifiers.NO_OVERRIDE, res);
}
break;
}
case IElementDescriptor.TYPE: {
case IElementDescriptor.TYPE -> {
IReferenceTypeDescriptor rtype = (IReferenceTypeDescriptor) descriptor;
res = annotateRestriction(element, IApiXmlConstants.ATTR_IMPLEMENT, RestrictionModifiers.NO_IMPLEMENT, res);
if (earlierversion && RestrictionModifiers.isImplementRestriction(res)) {
Expand Down Expand Up @@ -726,10 +720,8 @@ private static int getRestrictions(final IJavaProject project, final Element ele
}
}
}
break;
}
default:
break;
default -> { /**/ }
}
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,19 @@ private void annotateElementAttributes(IApiAnnotations description, Element elem

@Override
public void endVisitElement(IElementDescriptor element, IApiAnnotations description) {
switch (element.getElementType()) {
case IElementDescriptor.PACKAGE: {
// A null package indicates there was an override for the
// package in a different context.
// Package rules are stored in the manifest, not the API
// description file.
// No need to add empty packages.
if (fPackage != null && fPackage.hasChildNodes()) {
fComponent.appendChild(fPackage);
}
fPackage = null;
break;
int elementType = element.getElementType();
if (elementType == IElementDescriptor.PACKAGE) {
// A null package indicates there was an override for the
// package in a different context.
// Package rules are stored in the manifest, not the API
// description file.
// No need to add empty packages.
if (fPackage != null && fPackage.hasChildNodes()) {
fComponent.appendChild(fPackage);
}
case IElementDescriptor.TYPE: {
fTypeStack.pop();
break;
}
default:
break;
fPackage = null;
} else if (elementType == IElementDescriptor.TYPE) {
fTypeStack.pop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ public boolean visitElement(IElementDescriptor element, IApiAnnotations descript
String signature = null;
String name = null;
switch (element.getElementType()) {
case IElementDescriptor.TYPE:
case IElementDescriptor.TYPE -> {
signature = ((IReferenceTypeDescriptor) element).getSignature();
break;
case IElementDescriptor.METHOD:
}
case IElementDescriptor.METHOD -> {
signature = ((IMethodDescriptor) element).getSignature();
name = ((IMethodDescriptor) element).getName();
break;
case IElementDescriptor.FIELD:
}
case IElementDescriptor.FIELD -> {
name = ((IFieldDescriptor) element).getName();
break;
default:
break;
}
default -> { /**/ }
}
if (signature != null) {
fCrc.update(signature.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ public NonApiProjectDescription(IJavaProject project) {

@Override
protected boolean isInsertOnResolve(IElementDescriptor elementDescriptor) {
switch (elementDescriptor.getElementType()) {
case IElementDescriptor.PACKAGE:
return true;
default:
return false;
}
return elementDescriptor.getElementType() == IElementDescriptor.PACKAGE;
}

}
Loading

0 comments on commit b1a4e1c

Please sign in to comment.