2

I'm currently refactoring a project used by two application-projects and facing following problem:

The old project should be split up into three new projects A, B and C, because its growing and getting confusing.

So project A is implementing classes used by application 1, project B is implementing some other classes used by application 2 and rest of the classes which are used by both applications 1 and 2 should be implemented in project C.

https://i.stack.imgur.com/KIcQo.png

I already began the refactoring, so the structure is existing and working. Now I want to find out, if there are any classes left in project C, which are only used by application 1 or application 2

Because there are about 500 Java classes I would like to use a script to see whether a Java class is implemented by project A, B or both. In eclipse manually I can select the classname of every java class and go to "references --> workspace" and check the results but I hope someone has a better idea :-)

Thank you in advance!

Bohne
  • 4,029
  • 1
  • 16
  • 22

1 Answers1

1

I now wrote a powershellscript which finds any java-files in project c. Then it searches every java-file of project a and b for the name of the java classes of project c and sorts out, which was not found in each project.

I have to say that this will not cover any inheritance...so this is just a hint of classes to search for.

Wierd solution but I think it works, heres the code:

# all classes used in the project C
$classNames = New-Object System.Collections.ArrayList
Get-ChildItem "path-to-project-C" -Recurse -Filter *.java | `
Sort-Object -Descending | `
Foreach-Object {
    $className = $_.name.Split(".")
    #[void] to prevent .add-function to pass the added index into console
    [void]$classNames.add($className[0])
}

# all classes of project C used in project A
$projectA_classes = New-Object System.Collections.ArrayList
Get-ChildItem "path-to-project-A" -Recurse -Filter *.java | `
Foreach-Object {
    $path = $_.FullName
    foreach ($class in $classNames){
        if(Select-String -Path $path -pattern $class -quiet) {
            [void]$projectA_classes.add($class)
        }
    }
}

write-host $projectA_classes.count classes used by project A

# all classes of project C used in project B
$projectB_classes = New-Object System.Collections.ArrayList
Get-ChildItem "path-to-project-B" -Recurse -Filter *.java | `
Foreach-Object {
    $path = $_.FullName
    foreach ($class in $classNames){
        if(Select-String -Path $path -pattern $class -quiet) {
            [void]$projectB_classes.add($class)
        }
    }
}

write-host $projectB_classes.count classes used by daemon project

#find classes not used by porject A
$projectB_classesNotUsedByProjectA = New-Object System.Collections.ArrayList
foreach ($class in $projectB_classes) {
    if(!($projectA_classes.contains($class))) {
        if(!($projectB_classesNotUsedByProjectA.contains($class))) {
            [void]$projectB_classesNotUsedByProjectA.add($class);
        }
    }
}

#find classes not used by porject B
$projectA_classesNotUsedByProjectB = New-Object System.Collections.ArrayList
foreach ($class in $projectA_classes) {
    if(!($projectB_classes.contains($class))) {
        if(!($projectA_classesNotUsedByProjectB.contains($class))) {
            [void]$projectA_classesNotUsedByProjectB.add($class);
        }

    }
}

write-host $projectB_classesNotUsedByProjectA.count classes not used by project A
write-host $projectA_classesNotUsedByProjectB.count classes not used by project B

write-host Classes not used by project A: $projectB_classesNotUsedByProjectA
write-host Classes not used by project B: $projectA_classesNotUsedByProjectB

I won't choose this as the correct answer because I hope there is a better solution.

Bohne
  • 4,029
  • 1
  • 16
  • 22