From d2f0299e0da9dba60a4ca8d14501e171fdb7a3a2 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Wed, 16 Oct 2024 20:36:54 +0200 Subject: [PATCH] Use records for inner data carrier classes in TargetDefinitionFile --- .../targetplatform/TargetDefinitionFile.java | 286 ++---------------- 1 file changed, 28 insertions(+), 258 deletions(-) diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java index e4bd84a9db..383456df63 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java @@ -151,90 +151,26 @@ public String getVersion() { } - private static class TargetRef implements TargetDefinition.TargetReferenceLocation { - - private String uri; - - public TargetRef(String uri) { - this.uri = uri; - } - + private record TargetReference(String getUri) implements TargetDefinition.TargetReferenceLocation { @Override public String getTypeDescription() { return "Target"; } - - @Override - public String getUri() { - return uri; - } - } - private static final class OSGIRepositoryLocation implements TargetDefinition.RepositoryLocation { - - private String uri; - private Collection requirements; - - public OSGIRepositoryLocation(String uri, Collection requirements) { - this.uri = uri; - this.requirements = requirements; - } - - @Override - public String getUri() { - return uri; - } - - @Override - public Collection getRequirements() { - return requirements; - } - + private record OSGIRepositoryLocation(String getUri, Collection getRequirements) + implements TargetDefinition.RepositoryLocation { } - private static class MavenLocation implements TargetDefinition.MavenGAVLocation { - - private final Collection includeDependencyScopes; - private final MissingManifestStrategy manifestStrategy; - private final boolean includeSource; - private final Collection instructions; - private final Collection roots; - private final DependencyDepth dependencyDepth; - private final Collection repositoryReferences; - private final Element featureTemplate; - private String label; - - public MavenLocation(Collection roots, Collection includeDependencyScopes, - MissingManifestStrategy manifestStrategy, boolean includeSource, - Collection instructions, DependencyDepth dependencyDepth, - Collection repositoryReferences, Element featureTemplate, - String label) { - this.roots = roots; - this.includeDependencyScopes = includeDependencyScopes; - this.manifestStrategy = manifestStrategy; - this.includeSource = includeSource; - this.instructions = instructions; - this.dependencyDepth = dependencyDepth; - this.repositoryReferences = repositoryReferences; - this.label = label; - this.featureTemplate = featureTemplate == null ? null : (Element) featureTemplate.cloneNode(true); - } - - @Override - public Collection getIncludeDependencyScopes() { - return includeDependencyScopes; - } - - @Override - public MissingManifestStrategy getMissingManifestStrategy() { - return manifestStrategy; - } + private record MavenLocation(Collection getRoots, Collection getIncludeDependencyScopes, + MissingManifestStrategy getMissingManifestStrategy, boolean includeSource, + Collection getInstructions, DependencyDepth getIncludeDependencyDepth, + Collection getRepositoryReferences, Element getFeatureTemplate, + String label) implements TargetDefinition.MavenGAVLocation { - @Override - public boolean includeSource() { - return includeSource; - } + MavenLocation { + getFeatureTemplate = getFeatureTemplate == null ? null : (Element) getFeatureTemplate.cloneNode(true); + } @Override public String toString() { @@ -249,36 +185,12 @@ public String toString() { return builder.toString(); } - @Override - public Collection getInstructions() { - return instructions; - } - - @Override - public Collection getRoots() { - return roots; - } - - @Override - public Collection getRepositoryReferences() { - return repositoryReferences; - } - - @Override - public Element getFeatureTemplate() { - return featureTemplate; - } - - @Override - public DependencyDepth getIncludeDependencyDepth() { - return dependencyDepth; - } - @Override public String getLabel() { if (label != null && !label.isBlank()) { return label; } + Element featureTemplate = getFeatureTemplate(); if (featureTemplate != null) { String featureLabel = featureTemplate.getAttribute("label"); if (featureLabel != null && !featureLabel.isBlank()) { @@ -289,6 +201,7 @@ public String getLabel() { return featureId; } } + Collection roots = getRoots(); if (roots.size() == 1) { MavenDependency dependency = roots.iterator().next(); return MessageFormat.format("{0}:{1} ({2})", dependency.getGroupId(), dependency.getArtifactId(), @@ -300,24 +213,8 @@ public String getLabel() { } - private static final class MavenDependencyRoot implements MavenDependency { - - private final Set globalExcludes; - private final String groupId; - private final String artifactId; - private final String version; - private final String classifier; - private final String type; - - public MavenDependencyRoot(String groupId, String artifactId, String version, String classifier, String type, - Set globalExcludes) { - this.groupId = groupId; - this.artifactId = artifactId; - this.version = version; - this.classifier = classifier; - this.type = type; - this.globalExcludes = globalExcludes; - } + private record MavenDependencyRoot(String getGroupId, String getArtifactId, String getVersion, String getClassifier, + String getArtifactType, Set globalExcludes) implements MavenDependency { private static String getKey(IArtifactFacade artifact) { if (artifact == null) { @@ -332,31 +229,6 @@ private static String getKey(IArtifactFacade artifact) { return key; } - @Override - public String getGroupId() { - return groupId; - } - - @Override - public String getArtifactId() { - return artifactId; - } - - @Override - public String getVersion() { - return version; - } - - @Override - public String getArtifactType() { - return type; - } - - @Override - public String getClassifier() { - return classifier; - } - @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -406,126 +278,24 @@ private static Element getChild(Element element, String tagName) { return list.get(0); } - private static class IULocation implements TargetDefinition.InstallableUnitLocation { - - private final List units; - private final List repositories; - private final IncludeMode includeMode; - private final boolean includeAllEnvironments; - private final boolean includeSource; - private final boolean includeConfigurePhase; - private final FollowRepositoryReferences followRepositoryReferences; - - IULocation(List units, List repositories, IncludeMode includeMode, - boolean includeAllEnvironments, boolean includeSource, boolean includeConfigurePhase, - FollowRepositoryReferences followRepositoryReferences) { - this.units = units; - this.repositories = repositories; - this.includeMode = includeMode; - this.includeAllEnvironments = includeAllEnvironments; - this.includeSource = includeSource; - this.includeConfigurePhase = includeConfigurePhase; - this.followRepositoryReferences = followRepositoryReferences; - } - - @Override - public List getUnits() { - return units; - } - - @Override - public List getRepositories() { - return repositories; - } - - @Override - public IncludeMode getIncludeMode() { - return includeMode; - } - - @Override - public boolean includeAllEnvironments() { - return includeAllEnvironments; - } - - @Override - public boolean includeSource() { - return includeSource; - } - - @Override - public boolean includeConfigurePhase() { - return includeConfigurePhase; - } - - @Override - public FollowRepositoryReferences followRepositoryReferences() { - return followRepositoryReferences; - } - } - - private static class OtherLocation implements Location { - private final String description; - - OtherLocation(String description) { - this.description = description; - } - - @Override - public String getTypeDescription() { - return description; - } - } - - private static final class Repository implements TargetDefinition.Repository { - - private final String id; - private final String uri; - - Repository(String id, String uri) { - this.id = id; - this.uri = uri; - } - - @Override - public String getId() { - // this is Maven specific, used to match credentials and mirrors - return id; - } - - @Override - public String getLocation() { - return uri; - } - - } - - private static class Unit implements TargetDefinition.Unit { - - private String id; - private String version; - - Unit(String id, String version) { - this.id = id; - this.version = version; - } + private record IULocation(List getUnits, List getRepositories, IncludeMode getIncludeMode, + boolean includeAllEnvironments, boolean includeSource, boolean includeConfigurePhase, + FollowRepositoryReferences followRepositoryReferences) implements TargetDefinition.InstallableUnitLocation { + } - @Override - public String getId() { - return id; - } + private record OtherLocation(String getTypeDescription) implements Location { + } - @Override - public String getVersion() { - return version; - } + private record Repository(String getId, String getLocation) implements TargetDefinition.Repository { + // the id is Maven specific, used to match credentials and mirrors + } + private record Unit(String getId, String getVersion) implements TargetDefinition.Unit { @Override public String toString() { - return "Unit [id=" + id + ", version=" + version + "]"; + return "Unit [id=" + getId() + ", version=" + getVersion() + "]"; } - - } + } private TargetDefinitionFile(Document document, String origin) throws TargetDefinitionSyntaxException { this.origin = origin; @@ -684,7 +454,7 @@ private static List parseLocations(Element } else if (MavenGAVLocation.TYPE.equals(type)) { locations.add(parseMavenLocation(locationDom)); } else if ("Target".equals(type)) { - locations.add(new TargetRef(locationDom.getAttribute("uri"))); + locations.add(new TargetReference(locationDom.getAttribute("uri"))); } else if (TargetDefinition.RepositoryLocation.TYPE.equals(type)) { locations.add(parseRepositoryLocation(locationDom)); } else {