Skip to content

Commit

Permalink
- changed to Java 17 Switch Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
wide4head authored and HannesWell committed Apr 30, 2023
1 parent 59a2a32 commit 25e36e9
Show file tree
Hide file tree
Showing 27 changed files with 417 additions and 732 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,10 @@ 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
}
// 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,22 @@ 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;
}
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 +208,22 @@ 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,16 @@ 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;
}
}

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,7 @@ private String[] collectMissingTags(IApiAnnotations api, List<TagElement> tags,
missing.add(tag.getCompleteTag(type, member));
}
}
break;
default:
break;
}
}
return missing.toArray(new String[missing.size()]);
}
Expand Down Expand Up @@ -414,27 +413,22 @@ 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;
}
}
return null;
Expand Down Expand Up @@ -674,19 +668,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 +718,7 @@ private static int getRestrictions(final IJavaProject project, final Element ele
}
}
}
break;
}
default:
break;
}
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void annotateElementAttributes(IApiAnnotations description, Element elem
@Override
public void endVisitElement(IElementDescriptor element, IApiAnnotations description) {
switch (element.getElementType()) {
case IElementDescriptor.PACKAGE: {
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
Expand All @@ -130,14 +130,10 @@ public void endVisitElement(IElementDescriptor element, IApiAnnotations descript
fComponent.appendChild(fPackage);
}
fPackage = null;
break;
}
case IElementDescriptor.TYPE: {
case IElementDescriptor.TYPE -> {
fTypeStack.pop();
break;
}
default:
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ 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;
}
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,13 @@ void visitType(ManifestNode node, ApiDescriptionVisitor visitor) {

@Override
protected boolean isInsertOnResolve(IElementDescriptor elementDescriptor) {
switch (elementDescriptor.getElementType()) {
case IElementDescriptor.METHOD:
case IElementDescriptor.FIELD:
return false;
case IElementDescriptor.TYPE:
// no need to insert member types
return ((IReferenceTypeDescriptor) elementDescriptor).getEnclosingType() == null;
default:
return true;
}
return switch (elementDescriptor.getElementType())
{
case IElementDescriptor.METHOD, IElementDescriptor.FIELD -> false;
// no need to insert member types
case IElementDescriptor.TYPE -> ((IReferenceTypeDescriptor) elementDescriptor).getEnclosingType() == null;
default -> true;
};
}

@Override
Expand All @@ -451,21 +448,21 @@ protected ManifestNode createNode(ManifestNode parentNode, IElementDescriptor el
for (IPackageFragmentRoot root : roots) {
IClasspathEntry entry = root.getRawClasspathEntry();
switch (entry.getEntryKind()) {
case IClasspathEntry.CPE_SOURCE:
case IClasspathEntry.CPE_LIBRARY:
case IClasspathEntry.CPE_SOURCE, IClasspathEntry.CPE_LIBRARY -> {
IPackageFragment fragment = root.getPackageFragment(pkg.getName());
if (fragment.exists()) {
fragments.add(fragment);
}
break;
default:
}
default -> {
if (!root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
// class file folder
fragment = root.getPackageFragment(pkg.getName());
IPackageFragment fragment = root.getPackageFragment(pkg.getName());
if (fragment.exists()) {
fragments.add(fragment);
}
}
}
}
}
if (fragments.isEmpty()) {
Expand Down Expand Up @@ -620,14 +617,12 @@ private void logPackafesRefresh() {
}

private IElementDescriptor getElementDescriptor(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.PACKAGE_FRAGMENT:
return Factory.packageDescriptor(element.getElementName());
case IJavaElement.TYPE:
return Factory.typeDescriptor(((IType) element).getFullyQualifiedName('$'));
default:
return null;
}
return switch (element.getElementType())
{
case IJavaElement.PACKAGE_FRAGMENT -> Factory.packageDescriptor(element.getElementName());
case IJavaElement.TYPE -> Factory.typeDescriptor(((IType) element).getFullyQualifiedName('$'));
default -> null;
};
}

/**
Expand Down
Loading

0 comments on commit 25e36e9

Please sign in to comment.