Skip to content

Commit

Permalink
Do not create components for abstract classes
Browse files Browse the repository at this point in the history
  • Loading branch information
FloBoJa committed Feb 6, 2024
1 parent 71b7971 commit 384df0c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class JaxRSRules implements Rule {

if(identifier.toString.endsWith("Test")) return;

if(isAbstraction(unit)) return;

// technology based and general recognition
if (isConverter) {
detectDefault(blackboard, unit)
Expand Down Expand Up @@ -90,10 +92,13 @@ class JaxRSRules implements Rule {
detectDefault(blackboard, unit)
}

for (iface : getAllInterfaces(unit)) {
pcmDetector.detectProvidedInterfaceWeakly(identifier, iface.resolveBinding)
for (m : getMethods(iface)) {
pcmDetector.detectProvidedOperationWeakly(identifier, iface.resolveBinding, m)
for (parent : getAllAbstractParents(unit)) {
System.out.println(identifier.name + ": " + parent)
val parentBinding = parent.resolveBinding
pcmDetector.detectProvidedInterfaceWeakly(identifier, parentBinding)
for (m : getMethods(parent)) {
System.out.println("\t " + m.name)
pcmDetector.detectProvidedOperationWeakly(identifier, parentBinding, m)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SpringRules implements Rule {

if(identifier.toString.endsWith("Test")) return;

pcmDetector.detectComponent(identifier)
if(!isAbstraction(unit) || isClient || isRepository) pcmDetector.detectComponent(identifier)

if (isComponent) {
getConstructors(unit).stream.filter[c|isMethodAnnotatedWithName(c, "Autowired")].flatMap [ c |
Expand Down Expand Up @@ -215,13 +215,13 @@ class SpringRules implements Rule {
}
}

for (iface : getAllInterfaces(unit)) {
val ifaceBinding = iface.resolveBinding
for (parent : getAllAbstractParents(unit)) {
val parentBinding = parent.resolveBinding
// Hide Repository interface implementations, they tend to connect composites in unrepresentative ways
if (ifaceBinding !== null && !ifaceBinding.name.endsWith("Repository")) {
pcmDetector.detectProvidedInterfaceWeakly(identifier, ifaceBinding)
for (m : getMethods(iface)) {
pcmDetector.detectProvidedOperationWeakly(identifier, ifaceBinding, m)
if (parentBinding !== null && !parentBinding.name.endsWith("Repository")) {
pcmDetector.detectProvidedInterfaceWeakly(identifier, parentBinding)
for (m : getMethods(parent)) {
pcmDetector.detectProvidedOperationWeakly(identifier, parentBinding, m)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public synchronized void addWeakly(final OperationInterface iface) {
}

public synchronized void strengthenIfPresent(final OperationInterface iface) {
if (this.weakProvisions.contains(iface)) {
this.weakProvisions.remove(iface);
this.provisions.add(iface);
List<OperationInterface> remainingProvisions = new LinkedList<>();
for (final OperationInterface provision : this.weakProvisions) {
final boolean partlyProvided = provision.isPartOf(iface);
final boolean entirelyProvided = iface.isPartOf(provision);
if (partlyProvided || entirelyProvided) {
this.provisions.add(provision);
} else {
remainingProvisions.add(provision);
}
}
this.weakProvisions.clear();
this.weakProvisions.addAll(remainingProvisions);
}

public synchronized boolean containsRelated(final OperationInterface requirement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.util.IModifierConstants;

/**
* This class is used as a supporting library for writing rules for Retriever. It contains numerous
Expand Down Expand Up @@ -61,7 +62,8 @@ public static boolean isAbstraction(final CompilationUnit unit) {
final List<AbstractTypeDeclaration> types = cast(unit.types(), AbstractTypeDeclaration.class);

for (final AbstractTypeDeclaration abstType : types) {
if ((abstType instanceof TypeDeclaration) && ((TypeDeclaration) abstType).isInterface()) {
if (abstType instanceof TypeDeclaration typeDecl && (typeDecl.isInterface() || typeDecl.modifiers()
.contains(Modifier.ABSTRACT))) {
return true;
}
}
Expand Down Expand Up @@ -320,17 +322,31 @@ public static boolean isUnitAnEnum(final CompilationUnit unit) {
return false;
}

public static List<Type> getAllInterfaces(final CompilationUnit unit) {
/**
* Returns everything listed after "implements" and "extends" in the type declaration that is
* either abstract or an interface.
*/
public static List<Type> getAllAbstractParents(final CompilationUnit unit) {
final List<Type> interfaces = new ArrayList<>();

final List<AbstractTypeDeclaration> types = cast(unit.types(), AbstractTypeDeclaration.class);

for (final AbstractTypeDeclaration abstType : types) {
if (abstType instanceof TypeDeclaration) {
final TypeDeclaration type = (TypeDeclaration) abstType;

if (abstType instanceof TypeDeclaration type) {
final List<Type> interfaceTypes = cast(type.superInterfaceTypes(), Type.class);
interfaces.addAll(interfaceTypes);

Type superclassType = type.getSuperclassType();
if (superclassType == null) {
continue;
}
ITypeBinding superclassBinding = superclassType.resolveBinding();
if (superclassBinding == null) {
continue;
}
if ((superclassBinding.getModifiers() & IModifierConstants.ACC_ABSTRACT) != 0) {
interfaces.add(superclassType);
}
}
}

Expand Down

0 comments on commit 384df0c

Please sign in to comment.