org.eclipse.recommenders.tests.jdt.AstUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.recommenders.tests.jdt.AstUtils.java

Source

/**
 * Copyright (c) 2010, 2011 Darmstadt University of Technology.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marcel Bruch - initial API and implementation.
 */
package org.eclipse.recommenders.tests.jdt;

import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.recommenders.utils.Tuple;

import com.google.common.collect.Sets;

public class AstUtils {
    public static String MARKER = "$";
    public static String MARKER_ESCAPE = "\\$";

    public static CompilationUnit createAst(final String content) {
        final ASTParser p = ASTParser.newParser(AST.JLS3);
        p.setKind(ASTParser.K_COMPILATION_UNIT);
        p.setSource(content.toCharArray());
        final CompilationUnit cu = (CompilationUnit) p.createAST(null);
        return cu;
    }

    public ASTNode findNode(final CompilationUnit cu, final int origSourcePosition) {
        final ASTNode res = NodeFinder.perform(cu, origSourcePosition, 1);
        return res;
    }

    public static Tuple<CompilationUnit, Set<Integer>> createAstWithMarkers(final String content) {
        final Set<Integer> markers = Sets.newTreeSet();

        int pos = 0;
        final StringBuilder sb = new StringBuilder(content);
        while ((pos = sb.indexOf(MARKER, pos)) != -1) {
            sb.delete(pos, pos + 1);
            markers.add(pos);
        }

        final CompilationUnit cu = createAst(sb.toString());
        // final IJavaElement javaElement = cu.getJavaElement();
        // final ITypeRoot typeRoot = cu.getTypeRoot();
        // final IType findPrimaryType = typeRoot.findPrimaryType();
        return Tuple.newTuple(cu, markers);
    }

}