static private <T> Artifact getFieldArtifact(Class<T> clazz, T obj, String artifactName) { Optional<Field> field = Arrays.stream(clazz.getDeclaredFields()) .filter(f -> { boolean nameMatches = f.getName().equals(artifactName); boolean propertyMatches = false; JsonProperty property = f.getDeclaredAnnotation(JsonProperty.class); if (property != null) { propertyMatches = property.value().equals(artifactName); } return nameMatches || propertyMatches; }) .findFirst(); try { Artifact result = (Artifact) field .orElseThrow(() -> new NoKnownArtifact(artifactName)) .get(obj); if (result == null && !artifactName.equals("defaultArtifact")) { result = getFieldArtifact(clazz, obj, "defaultArtifact"); } if (result == null) { result = new Artifact(); } return result; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (NullPointerException e) { throw new RuntimeException("Versioned artifact " + artifactName + " is not listed in the BOM"); } }
@Override public BillOfMaterials readBom(String version) throws IOException { if (!Versions.isBranch(version)) { throw new IllegalArgumentException("Version must be a branch in the git profile reader"); } String branch = Versions.fromBranch(version); BillOfMaterials.Artifact artifact = new BillOfMaterials.Artifact(); artifact.setCommit(branch); artifact.setVersion(version); BillOfMaterials.Services services = new BillOfMaterials.Services(); services.setDefaultArtifact(artifact); BillOfMaterials.Dependencies dependencies = new BillOfMaterials.Dependencies(); dependencies.setDefaultArtifact(artifact); BillOfMaterials bom = new BillOfMaterials(); bom.setDependencies(dependencies); bom.setVersion(version); bom.setServices(services); return bom; }
String getArtifactCommit(String artifactName) { return getFieldArtifact(Services.class, this, artifactName).getCommit(); }
String getArtifactCommit(String artifactName) { return getFieldArtifact(Dependencies.class, this, artifactName).getCommit(); }
ArtifactSources getArtifactSources(String artifactName) { return getFieldArtifact(Services.class, this, artifactName).getArtifactSources(); } }
ArtifactSources getArtifactSources(String artifactName) { return getFieldArtifact(Dependencies.class, this, artifactName).getArtifactSources(); } }
String getArtifactVersion(String artifactName) { return getFieldArtifact(Services.class, this, artifactName).getVersion(); }
String getArtifactVersion(String artifactName) { return getFieldArtifact(Dependencies.class, this, artifactName).getVersion(); }