Eclipse JDT: как найти JDK MethodInvocation и ClassInstanceCreation

Я использую Eclipse JDT ASTVisitor для поиска вызовов методов Java и создания экземпляров классов из исходных файлов Java.

Теперь я могу найти все это. Но я не могу судить, являются ли эти вызовы методов и создания экземпляров классов из библиотеки JDK или нет.

Итак, как я могу получить вызовы методов библиотеки JDK (например, InputStream.read()) и экземпляр класса (например, new String())?


Ниже приведен мой код.

JdtAstUtil.java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;

public class JdtAstUtil {
    /**
     * get compilation unit of source code
     * @param javaFilePath
     * @return CompilationUnit
     */
    public static CompilationUnit getCompilationUnit(String javaFilePath){
        byte[] input = null;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));
            input = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(input);
            bufferedInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ASTParser astParser = ASTParser.newParser(AST.JLS4);
        astParser.setSource(new String(input).toCharArray());
        astParser.setKind(ASTParser.K_COMPILATION_UNIT);
        astParser.setEnvironment(null, null, null, true);
        astParser.setResolveBindings(true);
        astParser.setBindingsRecovery(true);
        astParser.setUnitName("any_name");
        CompilationUnit result = (CompilationUnit) (astParser.createAST(null));
        return result;
    }
}  

DemoVisitor.java

import org.eclipse.jdt.core.dom.*;

public class DemoVisitor extends ASTVisitor {

    @Override
    public boolean visit(MethodInvocation node) {
        System.out.println("MethodInvocation:\t" + node.toString());
        System.out.println("\tExpression: " + node.getExpression());
        Expression expression = node.getExpression();
        if(expression != null) {
            ITypeBinding  typeBinding = expression.resolveTypeBinding();
            if (typeBinding != null) {
                System.out.println("\tType: " + typeBinding.getName());
            }
        }
        System.out.println("\tName: " + node.getName());
        // System.out.println("\t" + node.resolveMethodBinding());
        return true;
    }

    public void endVisit(ClassInstanceCreation node) {
        System.out.println("ClassInstanceCreation:\t" + node.toString());
        System.out.println("\tType: " + node.getType().toString());
    }
}  

ДемоВизиторТест.java

import org.eclipse.jdt.core.dom.CompilationUnit;

public class DemoVisitorTest {

    public DemoVisitorTest(String path) {
        CompilationUnit comp = JdtAstUtil.getCompilationUnit(path);
        DemoVisitor visitor = new DemoVisitor();
        comp.accept(visitor);
    }
    public static void main(String args[]) {
        DemoVisitorTest test = new DemoVisitorTest
                ("/home/luckcul/developSpace/test/test.java");
    }
}  

Я использовал приведенные выше коды для анализа следующего файла test.java.

test.java

package com.mtihc.minecraft.myhelppages;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class HelpCommandExecutor implements CommandExecutor {

    /***
     * Copies bytes from a large (over 2GB) InputStream to an OutputStream.
     * This method uses the provided buffer, so there is no need to use a
     * BufferedInputStream.
     * @param input the InputStream to read from
    * */
    public static long copyLarge(final InputStream input,
                                 final OutputStream output, final byte[] buffer) throws IOException {
        long count = 0;
        int n;
        CommandExecutor t = new CommandExecutor(new String());
        while (EOF != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        t.test();
            count += n;
        }
        return count;
    }
}

Наконец, я получил эти:

ClassInstanceCreation:  new String()
    Type: String
ClassInstanceCreation:  new CommandExecutor(new String())
    Type: CommandExecutor
MethodInvocation:   input.read(buffer)
    Expression: input
    Type: InputStream
    Name: read
MethodInvocation:   output.write(buffer,0,n)
    Expression: output
    Type: OutputStream
    Name: write
MethodInvocation:   t.test()
    Expression: t
    Type: CommandExecutor
    Name: test

В этом примере я хочу получить только new String(), InputStream.read(), OutputStream.write() из библиотеки JDK.

Как я могу изменить свои коды, чтобы решить свои проблемы?


person luckcul    schedule 22.09.2017    source источник


Ответы (1)


Вы можете использовать Java-модель JDT, чтобы узнать о классе, объявляющем вызываемый метод. Базовый (непроверенный) набросок стратегии:

ITypeBinding declaringType = invocation.resolveMethodBinding().getDeclaringClass();
IJavaProject javaProject = JavaCore.create(iProject); // assumes you have an IProject
IType type = javaProject.findType(declaringType.getQualifiedName());
IJavaElement root = type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
IClasspathEntry cpEntry = ((IPackageFragmentRoot) root).getRawClasspathEntry();

Для типов JDK cpEntry должно быть от getEntryKind() до CPE_CONTAINER, а его getPath() должно начинаться с "org.eclipse.jdt.launching.JRE_CONTAINER".

person Stephan Herrmann    schedule 31.10.2017
comment
Я использую ' StringqualifiedName = typeBinding.getQualifiedName(); ', чтобы получить полное имя класса, например 'java.lang.Long'. Затем я проверяю, начинается ли это имя с «java.lang», «java.io» и т. д. (все это имя библиотеки jdk). Таким образом, могу ли я получить правильное суждение о принадлежности к библиотеке JDK? - person luckcul; 01.11.2017
comment
Переход только по квалифицированному имени — это эвристика, которая может приблизиться к ожидаемому результату. Остаются некоторые неясности: классы в пакетах javax.* могут быть из JDK или сторонних производителей, а также все внутренние классы в пространствах имен com.sun и т.п. не обнаруживаются правильно таким образом. Если такой мягкой классификации достаточно, все в порядке, в противном случае мой ответ подскажет, как получить точную классификацию. - person Stephan Herrmann; 02.11.2017