Tristan
Tristan

Reputation: 1378

Calculating Efferent Coupling in Java

I need to calculate the Efferent Coupling (Coupling Between Objects) of a Java program from the source file.

I'm already extracting the Abstract Syntax Tree with jdt in Eclipse, but I'm not sure if it's possible to directly extract class dependencies from another class.

I can't use any metric pluggin.

Thanks for your help.

Upvotes: 0

Views: 600

Answers (1)

christoph.keimel
christoph.keimel

Reputation: 1240

You can use an ASTVisitor to check the relevant nodes in your AST. You can then use resolveBinding() or resolveTypeBinding() to extract the dependencies. (For this to work you need to turn on "resolveBindings" when you parse.)

I haven't tested this, but this example should give you an idea:

public static IType[] findDependencies(ASTNode node) {
    final Set<IType> result = new HashSet<IType>();
    node.accept(new ASTVisitor() {
        @Override
        public boolean visit(SimpleName node) {
            ITypeBinding typeBinding = node.resolveTypeBinding();
            if (typeBinding == null)
                return false;
            IJavaElement element = typeBinding.getJavaElement();
            if (element != null && element instanceof IType) {
                result.add((IType)element);
            }
            return false;
        }
    });
    return result.toArray(new IType[result.size()]);
}

Upvotes: 1

Related Questions