Skip to content

Commit

Permalink
Simplify PCMDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
FloBoJa committed Jan 19, 2024
1 parent d52e3f1 commit 204c818
Showing 1 changed file with 40 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,25 @@ private static String getFullUnitName(final CompUnitOrName unit) {
// On the other hand, it is still fit as a unique identifier,
// since types cannot be declared multiple times.

final List<String> names = getFullUnitNames(unit);
if (!names.isEmpty()) {
return names.get(0);
}
return null;
}

private static List<String> getFullUnitNames(final CompUnitOrName unit) {
if (!unit.isUnit()) {
return List.of(unit.name());
return unit.name();
}

final List<String> names = new ArrayList<>();
for (final Object type : unit.compilationUnit()
.get()
.types()) {
if (type instanceof AbstractTypeDeclaration) {
names.add(getFullTypeName((AbstractTypeDeclaration) type));
String fullTypeName = ((AbstractTypeDeclaration) type).getName()
.getFullyQualifiedName();
names.add(fullTypeName);
}
}
if (!names.isEmpty()) {
return names.get(0);
}

return names;
}

private static String getFullTypeName(final AbstractTypeDeclaration type) {
return type.getName()
.getFullyQualifiedName();
return null;
}

public void detectComponent(final CompUnitOrName unit) {
Expand All @@ -99,7 +91,7 @@ public void detectRequiredInterface(final CompUnitOrName unit, final InterfaceNa
this.detectRequiredInterface(unit, interfaceName, false);
}

public void detectRequiredInterface(final CompUnitOrName unit, final InterfaceName interfaceName,
private void detectRequiredInterface(final CompUnitOrName unit, final InterfaceName interfaceName,
final boolean compositeRequired) {
if (this.components.get(unit) == null) {
this.components.put(unit, new ComponentBuilder(unit));
Expand Down Expand Up @@ -132,11 +124,6 @@ private void detectRequiredInterface(final CompUnitOrName unit, final FieldDecla
}

public void detectRequiredInterface(final CompUnitOrName unit, final SingleVariableDeclaration parameter) {
this.detectRequiredInterface(unit, parameter, false);
}

private void detectRequiredInterface(final CompUnitOrName unit, final SingleVariableDeclaration parameter,
final boolean compositeRequired) {
if (this.components.get(unit) == null) {
this.components.put(unit, new ComponentBuilder(unit));
}
Expand All @@ -149,7 +136,7 @@ private void detectRequiredInterface(final CompUnitOrName unit, final SingleVari
final ITypeBinding type = parameterBinding.getType();
final EntireInterface iface = new EntireInterface(type,
new JavaInterfaceName(NameConverter.toPCMIdentifier(type)));
this.detectRequiredInterface(unit, compositeRequired, false, iface);
this.detectRequiredInterface(unit, false, false, iface);
}

private void detectRequiredInterface(final CompUnitOrName unit, final boolean compositeRequired,
Expand Down Expand Up @@ -187,45 +174,22 @@ private void detectRequiredInterface(final CompUnitOrName unit, final boolean co
}
}

public void detectProvidedInterface(final CompUnitOrName unit, final ITypeBinding iface) {
this.detectProvidedInterface(unit, iface, false, false);
}

public void detectProvidedInterfaceWeakly(final CompUnitOrName unit, final ITypeBinding iface) {
this.detectProvidedInterface(unit, iface, false, true);
}

private void detectProvidedInterface(final CompUnitOrName unit, final ITypeBinding iface,
final boolean compositeProvided, final boolean detectWeakly) {
if (iface == null) {
LOG.warn("Unresolved type binding detected in " + getFullUnitName(unit) + "!");
return;
}
final OperationInterface provision = new EntireInterface(iface,
new JavaInterfaceName(NameConverter.toPCMIdentifier(iface)));
this.detectProvidedInterface(unit, provision, compositeProvided, detectWeakly);
}

public void detectProvidedOperation(final CompUnitOrName unit, final IMethodBinding method) {
detectProvidedOperation(unit, method, false);
this.detectProvidedInterface(unit, provision, false, true);
}

public void detectProvidedOperationWeakly(final CompUnitOrName unit, final IMethodBinding method) {
detectProvidedOperation(unit, method, true);
}

private void detectProvidedOperation(final CompUnitOrName unit, final IMethodBinding method,
final boolean detectWeakly) {
if (method == null) {
LOG.warn("Unresolved method binding detected in " + getFullUnitName(unit) + "!");
return;
}
this.detectProvidedOperation(unit, method.getDeclaringClass(), method, detectWeakly);
}

public void detectProvidedOperation(final CompUnitOrName unit, final ITypeBinding declaringIface,
final IMethodBinding method) {
this.detectProvidedOperation(unit, declaringIface, method, false);
this.detectProvidedOperation(unit, method.getDeclaringClass(), method, true);
}

public void detectProvidedOperationWeakly(final CompUnitOrName unit, final ITypeBinding declaringIface,
Expand All @@ -239,16 +203,7 @@ private void detectProvidedOperation(final CompUnitOrName unit, final ITypeBindi
LOG.warn("Unresolved type binding detected in " + getFullUnitName(unit) + "!");
return;
}
this.detectProvidedOperation(unit, NameConverter.toPCMIdentifier(declaringIface), method, false, detectWeakly);
}

public void detectProvidedOperation(final CompUnitOrName unit, final String declaringIface,
final IMethodBinding method) {
this.detectProvidedOperation(unit, declaringIface, method, false, false);
}

private void detectProvidedOperation(final CompUnitOrName unit, final String declaringIface,
final IMethodBinding method, final boolean compositeProvided, final boolean detectWeakly) {
String operationName;
if (method == null) {
LOG.warn("Unresolved method binding detected in " + getFullUnitName(unit) + "!");
Expand All @@ -257,8 +212,9 @@ private void detectProvidedOperation(final CompUnitOrName unit, final String dec
operationName = method.getName();
}

this.detectProvidedOperation(unit, method, new JavaOperationName(declaringIface, operationName),
compositeProvided, detectWeakly);
this.detectProvidedOperation(unit, method,
new JavaOperationName(NameConverter.toPCMIdentifier(declaringIface), operationName), false,
detectWeakly);
}

public void detectProvidedOperation(final CompUnitOrName unit, final IMethodBinding method,
Expand All @@ -277,35 +233,28 @@ private void detectProvidedOperation(final CompUnitOrName unit, final IMethodBin

private void detectProvidedInterface(final CompUnitOrName unit, final OperationInterface iface,
final boolean compositeProvided, final boolean detectWeakly) {
this.detectProvidedInterface(unit, compositeProvided, detectWeakly, List.of(iface));
}

private void detectProvidedInterface(final CompUnitOrName unit, final boolean compositeProvided,
final boolean detectWeakly, final Collection<OperationInterface> ifaces) {
for (final OperationInterface iface : ifaces) {
final boolean isRequired = this.compositeRequirements.containsRelated(iface) || this.components.values()
final boolean isRequired = this.compositeRequirements.containsRelated(iface) || this.components.values()
.stream()
.anyMatch(component -> component.requirements()
.containsRelated(iface));
if (!isRequired && detectWeakly) {
this.components.get(unit)
.provisions()
.addWeakly(iface);
if (compositeProvided) {
this.compositeProvisions.addWeakly(iface);
}
} else {
this.components.get(unit)
.provisions()
.add(iface);
this.components.values()
.stream()
.anyMatch(component -> component.requirements()
.containsRelated(iface));
if (!isRequired && detectWeakly) {
this.components.get(unit)
.provisions()
.addWeakly(iface);
if (compositeProvided) {
this.compositeProvisions.addWeakly(iface);
}
} else {
this.components.get(unit)
.provisions()
.add(iface);
this.components.values()
.stream()
.forEach(component -> component.requirements()
.strengthenIfPresent(iface));
this.compositeRequirements.strengthenIfPresent(iface);
if (compositeProvided) {
this.compositeProvisions.add(iface);
}
.forEach(component -> component.requirements()
.strengthenIfPresent(iface));
this.compositeRequirements.strengthenIfPresent(iface);
if (compositeProvided) {
this.compositeProvisions.add(iface);
}
}
}
Expand All @@ -314,48 +263,22 @@ public void detectPartOfComposite(final CompUnitOrName unit, final String compos
if (this.components.get(unit) == null) {
this.components.put(unit, new ComponentBuilder(unit));
}
this.getComposite(compositeName)
if (!this.composites.containsKey(compositeName)) {
this.composites.put(compositeName, new CompositeBuilder(compositeName));
}
this.composites.get(compositeName)
.addPart(this.components.get(unit));
}

public void detectCompositeRequiredInterface(final CompUnitOrName unit, final InterfaceName interfaceName) {
this.detectRequiredInterface(unit, interfaceName, true);
}

public void detectCompositeRequiredInterface(final CompUnitOrName unit, final FieldDeclaration field) {
this.detectRequiredInterface(unit, field, true, false);
}

public void detectCompositeRequiredInterface(final CompUnitOrName unit, final SingleVariableDeclaration parameter) {
this.detectRequiredInterface(unit, parameter, true);
}

public void detectCompositeProvidedOperation(final CompUnitOrName unit, final IMethodBinding method) {
this.detectCompositeProvidedOperation(unit, method.getDeclaringClass(), method);
}

public void detectCompositeProvidedOperation(final CompUnitOrName unit, final ITypeBinding declaringIface,
final IMethodBinding method) {
this.detectCompositeProvidedOperation(unit, NameConverter.toPCMIdentifier(declaringIface), method, false);
}

private void detectCompositeProvidedOperation(final CompUnitOrName unit, final String declaringIface,
final IMethodBinding method, final boolean detectWeakly) {
this.detectProvidedOperation(unit, declaringIface, method, true, detectWeakly);
}

public void detectCompositeProvidedOperation(final CompUnitOrName unit, final IMethodBinding method,
final OperationName name) {
this.detectProvidedOperation(unit, method, name, true, false);
}

private CompositeBuilder getComposite(final String name) {
if (!this.composites.containsKey(name)) {
this.composites.put(name, new CompositeBuilder(name));
}
return this.composites.get(name);
}

public Set<CompUnitOrName> getCompilationUnits() {
return this.components.keySet();
}
Expand Down

0 comments on commit 204c818

Please sign in to comment.