SoftwareEngineering/Java/JDT

Example

ExampleVisitor

package org.codereign;

import java.nio.file.Path;

import org.codereign.jdt.Status;
import org.codereign.jdt.StatusBuilder;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class ExampleVisitor extends ASTVisitor {

    private Path path;

    private CompilationUnit unit;

    public ExampleVisitor(final Path path) {
        this.path = path;
    }

    @SuppressWarnings("unchecked")
    private <T> T findByParentNode(ASTNode node, Class<T> clazz) {
        T instance = null;

        ASTNode parentNode = node.getParent();
        while (parentNode != null) {
            if (clazz.isAssignableFrom(parentNode.getClass())) {
                instance = (T) parentNode;
                break;
            }

            parentNode = parentNode.getParent();
        }

        return instance;
    }

    @Override
    public void preVisit(ASTNode node) {
        if (node instanceof CompilationUnit) {
            this.unit = (CompilationUnit) node;
        }
    }

    @Override
    public boolean visit(TypeDeclaration node) {
        StatusBuilder builder = new StatusBuilder(path, unit, node).withTypeDeclaration(node);

        for (Object element : node.modifiers()) {
            if (element instanceof MarkerAnnotation) {
                MarkerAnnotation annotation = (MarkerAnnotation) element;

                Status status = builder.build();
                status.setTitle("クラスのアノテーション");
                status.setDescription(annotation + " は使用禁止アノテーションです。");
                System.out.println(status);

                status = builder.build();
                status.setCurrentNode(annotation);
                status.setTitle("クラスのアノテーション");
                status.setDescription(annotation + " は使用禁止アノテーションです。");
                System.out.println(status);

                status = builder.build();
                status.setTitle("クラスのアノテーション");
                status.setDescription(annotation + " は使用禁止アノテーションです。");
                System.out.println(status);
            }
        }
        return super.visit(node);
    }

    @Override
    public boolean visit(MethodDeclaration node) {
        final TypeDeclaration typeDeclaration = findByParentNode(node, TypeDeclaration.class);

        StatusBuilder builder = new StatusBuilder(path, unit, node).withTypeDeclaration(typeDeclaration)
                .withMethodDeclaration(node);

        Status status = null;

        status = builder.build();
        status.setLineNumber(1);
        status.setTitle("title");
        status.setDescription("description");
        System.out.println(status);

        status = builder.build();
        status.setTitle("title");
        status.setDescription("description");
        System.out.println(status);

        status = builder.build();
        status.setCurrentNode(node);
        status.setTitle("title");
        status.setDescription("description");
        System.out.println(status);

        return super.visit(node);
    }

}

Status

package org.codereign.jdt;

import java.nio.file.Path;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class Status {

    private Path path;
    private CompilationUnit unit;
    private TypeDeclaration typeDeclaration;
    private MethodDeclaration methodDeclaration;
    private ASTNode currentNode;

    private int lineNumber = 1;
    private String title;
    private String description;

    public Status(StatusBuilder builder) {
        this.path = builder.getPath();
        this.unit = builder.getUnit();
        this.typeDeclaration = builder.getTypeDeclaration();
        this.methodDeclaration = builder.getMethodDeclaration();
        this.currentNode = builder.getCurrentNode();
        this.lineNumber = this.unit.getLineNumber(this.currentNode.getStartPosition());
    }

    public void setCurrentNode(ASTNode currentNode) {
        this.currentNode = currentNode;
        this.lineNumber = this.unit.getLineNumber(this.currentNode.getStartPosition());
    }

    public void setLineNumber(int lineNumber) {
        if (0 < lineNumber) {
            this.lineNumber = lineNumber;
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        final String fileName = path.getFileName().toString();
        final String packageName = unit.getPackage().getName().toString();
        final String className = (typeDeclaration != null) ? typeDeclaration.getName().toString() : "";
        final String methodName = (methodDeclaration != null) ? methodDeclaration.getName().toString() : "";

        return packageName + "." + className + "." + methodName + "(" + fileName + ":" + lineNumber + ") : " + title
                + ":" + description;
    }

}

StatusBuilder

package org.codereign.jdt;

import java.nio.file.Path;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class StatusBuilder {

    private Path path;
    private CompilationUnit unit;
    private TypeDeclaration typeDeclaration;
    private MethodDeclaration methodDeclaration;
    private ASTNode currentNode;

    public StatusBuilder(Path path, CompilationUnit unit, ASTNode currentNode) {
        this.setPath(path);
        this.setUnit(unit);
        this.setCurrentNode(currentNode);
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    public CompilationUnit getUnit() {
        return unit;
    }

    public void setUnit(CompilationUnit unit) {
        this.unit = unit;
    }

    public ASTNode getCurrentNode() {
        return currentNode;
    }

    public void setCurrentNode(ASTNode currentNode) {
        this.currentNode = currentNode;
    }

    public TypeDeclaration getTypeDeclaration() {
        return typeDeclaration;
    }

    public void setTypeDeclaration(TypeDeclaration typeDeclaration) {
        this.typeDeclaration = typeDeclaration;
    }

    public MethodDeclaration getMethodDeclaration() {
        return methodDeclaration;
    }

    public void setMethodDeclaration(MethodDeclaration methodDeclaration) {
        this.methodDeclaration = methodDeclaration;
    }

    public StatusBuilder withTypeDeclaration(TypeDeclaration typeDeclaration) {
        this.typeDeclaration = typeDeclaration;
        return this;
    }

    public StatusBuilder withMethodDeclaration(MethodDeclaration methodDeclaration) {
        this.methodDeclaration = methodDeclaration;
        return this;
    }

    public Status build() {
        return new Status(this);
    }

}

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-12-05 (木) 01:18:02 (7d)