Example usage for org.eclipse.jdt.core.dom MemberValuePair getParent

List of usage examples for org.eclipse.jdt.core.dom MemberValuePair getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MemberValuePair getParent.

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the root node.

Usage

From source file:org.evosuite.eclipse.popup.actions.TestGenerationJob.java

License:Open Source License

@Override
protected IStatus run(final IProgressMonitor monitor) {
    Boolean disabled = System.getProperty("evosuite.disable") != null; //  && System.getProperty("evosuite.disable").equals("1")
    if (disabled) {
        System.out.println("TestGenerationJob: The EvoSuite plugin is disabled :(");
        return Status.OK_STATUS;
    }/*from w  w  w  .  j a  va  2s  .  c o m*/

    final String suiteFileName = getSuiteFileName(suiteClass);
    final IFile fileSuite = ResourcesPlugin.getWorkspace().getRoot()
            .getFileForLocation(new Path(suiteFileName));
    if (fileSuite != null && fileSuite.exists()) {
        // Open test suite in editor
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                IWorkbenchPage page = iw.getActivePage();
                try {
                    IDE.openEditor(page, fileSuite, true);
                } catch (PartInitException e1) {
                    System.out.println("Could not open test suite");
                    e1.printStackTrace();
                }
            }
        });

        // Generated tests should be checked by tester?
        Boolean checkMarkers = System.getProperty("evosuite.markers.enforce") != null;
        if (checkMarkers) {

            String fileContents = readFileToString(suiteFileName);

            ASTParser parser = ASTParser.newParser(AST.JLS8);
            parser.setKind(ASTParser.K_COMPILATION_UNIT);
            parser.setStatementsRecovery(true);

            @SuppressWarnings("unchecked")
            Map<String, String> COMPILER_OPTIONS = new HashMap<String, String>(JavaCore.getOptions());
            COMPILER_OPTIONS.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
            COMPILER_OPTIONS.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
            COMPILER_OPTIONS.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);

            parser.setUnitName(suiteClass);
            String[] encodings = { ENCODING };
            String[] classpaths = { classPath };
            String[] sources = { new File(suiteFileName).getParent() };
            parser.setEnvironment(classpaths, sources, encodings, true);
            parser.setSource(fileContents.toCharArray());

            CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
            final List<String> uncheckedMethods = new ArrayList<String>();
            compilationUnit.accept(new ASTVisitor() {
                @Override
                public boolean visit(MemberValuePair node) {
                    if (node.getName().toString().equals("checked")
                            && !((BooleanLiteral) node.getValue()).booleanValue()) {
                        NormalAnnotation ann = (NormalAnnotation) node.getParent();
                        MethodDeclaration method = (MethodDeclaration) ann.getParent();
                        uncheckedMethods.add(method.getName().toString());
                        return false;
                    }
                    return true;
                }
            });
            if (uncheckedMethods.size() > 0) {
                Display.getDefault().syncExec(new Runnable() {
                    @Override
                    public void run() {
                        MessageDialog dialog = new MessageDialog(shell,
                                "JUnit test suite contains unit tests that need to be checked", null, // image
                                "The JUnit test suite " + suiteClass
                                        + " contains test cases that need to be checked:\n"
                                        + uncheckedMethods.toString(),
                                MessageDialog.OK, new String[] { "Ok" }, 0);
                        dialog.open();
                    }
                });
                return Status.OK_STATUS;
            }
        } else
            System.out.println("Not checking markers.");
    } else {
        System.out.println("File " + suiteFileName + " does not exist");
        // TODO: Dialog
        //         Display.getDefault().syncExec(new Runnable() {
        //            @Override
        //            public void run() {
        //               MessageDialog dialog = new MessageDialog(
        //                     shell,
        //                     "Error during test generation",
        //                     null, // image
        //                     "EvoSuite failed to generate tests for class"
        //                     + suiteClass,
        //                     MessageDialog.OK, new String[] { "Ok" }, 0);
        //               dialog.open();
        //            }               
        //         });
        //         return Status.CANCEL_STATUS;
    }

    setThread(new Thread());
    running = true;

    clearMarkersTarget();

    String oldTgr = getOldTestGenerationResult();
    lastTest = oldTgr;

    ArrayList<TestGenerationResult> results = runEvoSuite(monitor);
    writeMarkersTarget(results);
    //uncomment after experiment
    if (writeAllMarkers)
        writeMarkersTestSuite();

    try {
        target.getProject().refreshLocal(IProject.DEPTH_INFINITE, null);
        //         if ("true".equals(target.getProject().getPersistentProperty(
        //               EvoSuitePropertyPage.REPORT_PROP_KEY))) {
        //            syncWithUi(target);
        //         };

        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
                try {
                    final IFile generatedSuite = ResourcesPlugin.getWorkspace().getRoot()
                            .getFileForLocation(new Path(suiteFileName));
                    ICompilationUnit cu = JavaCore.createCompilationUnitFrom(generatedSuite);
                    IWorkbenchWindow iw = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
                    IWorkbenchPage page = iw.getActivePage();
                    IEditorPart part = IDE.openEditor(page, generatedSuite, true);
                    if (Activator.organizeImports()) {
                        OrganizeImportsAction a = new OrganizeImportsAction(part.getSite());
                        a.run(cu);
                        cu.commitWorkingCopy(true, null);
                        cu.save(null, true);
                    }
                } catch (PartInitException e1) {
                    System.out.println("Could not open test suite to organize imports");
                    e1.printStackTrace();
                } catch (JavaModelException e) {
                    System.out.println("Something went wrong while saving test suite after organizing imports");
                    e.printStackTrace();
                }
                ;
            }
        });
    } catch (CoreException e) {
        System.out.println("Dear me");
        e.printStackTrace();
    }
    Activator.CURRENT_WRITING_FILE = null;
    running = false;
    monitor.done();
    done(ASYNC_FINISH);
    Activator.FILE_QUEUE.update();
    return Status.OK_STATUS;
}

From source file:org.gw4e.eclipse.facade.JDTManager.java

License:Open Source License

/**
 * @param cu//  w  ww  .j a  v a  2s  .co  m
 * @return
 */
public static String findPathGeneratorInGraphWalkerAnnotation(ICompilationUnit cu) {
    CompilationUnit ast = parse(cu);
    Map<String, String> ret = new HashMap<String, String>();
    ast.accept(new ASTVisitor() {
        public boolean visit(MemberValuePair node) {
            String name = node.getName().getFullyQualifiedName();
            if ("value".equals(name) && node.getParent() != null
                    && node.getParent() instanceof NormalAnnotation) {
                IAnnotationBinding annoBinding = ((NormalAnnotation) node.getParent())
                        .resolveAnnotationBinding();
                String qname = annoBinding.getAnnotationType().getQualifiedName();
                if (GraphWalker.class.getName().equals(qname)) {
                    StringLiteral sl = (StringLiteral) node.getValue();
                    ret.put("ret", sl.getLiteralValue());
                }
            }
            return true;
        }
    });
    return ret.get("ret");
}

From source file:org.gw4e.eclipse.facade.JDTManager.java

License:Open Source License

/**
 * @param cu/*  ww w  .j a v  a 2 s .  co m*/
 * @param annotationName
 * @return
 * @throws JavaModelException
 */
public static AnnotationParsing resolveAnnotation(ICompilationUnit cu, Class inputClass,
        final String attribut) {

    CompilationUnit ast = parse(cu);
    AnnotationParsing ret = new AnnotationParsing();
    String annotationName = inputClass.getName();
    ast.accept(new ASTVisitor() {
        public boolean visit(MemberValuePair node) {
            String name = node.getName().getFullyQualifiedName();
            if (attribut.equals(name) && node.getParent() != null
                    && node.getParent() instanceof NormalAnnotation) {
                IAnnotationBinding annoBinding = ((NormalAnnotation) node.getParent())
                        .resolveAnnotationBinding();
                String qname = annoBinding.getAnnotationType().getQualifiedName();
                if (inputClass.getName().equals(qname)) {
                    int start = node.getStartPosition();
                    int end = start + node.getLength();
                    int lineNumber = ast.getLineNumber(start);
                    Location location = new Location(lineNumber, start, end);
                    ret.setLocation(location);
                }
            }
            return true;
        }

        public final boolean visit(final TypeDeclaration node) {
            List<?> modifiers = (List<?>) node.getStructuralProperty(TypeDeclaration.MODIFIERS2_PROPERTY);
            for (Object modifier : modifiers) {
                if (modifier instanceof org.eclipse.jdt.core.dom.Annotation) {
                    IAnnotationBinding annotationBinding = ((org.eclipse.jdt.core.dom.Annotation) modifier)
                            .resolveAnnotationBinding();

                    if (annotationBinding != null) {
                        final String qualifiedName = annotationBinding.getAnnotationType().getQualifiedName();
                        if (annotationName.equalsIgnoreCase(qualifiedName))
                            ret.add(annotationBinding);

                    }
                }
            }
            return true;
        }
    });
    return ret;
}

From source file:org.spoofax.interpreter.adapter.ecj.ECJFactory.java

License:LGPL

private MemberValuePair asMemberValuePair(IStrategoTerm term) {
    MemberValuePair x = ((WrappedMemberValuePair) term).getWrappee();
    return x.getParent() == null && x.getAST() == ast ? x : (MemberValuePair) ASTNode.copySubtree(ast, x);
}