Example usage for org.eclipse.jdt.internal.compiler.problem DefaultProblem DefaultProblem

List of usage examples for org.eclipse.jdt.internal.compiler.problem DefaultProblem DefaultProblem

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.problem DefaultProblem DefaultProblem.

Prototype

public DefaultProblem(char[] originatingFileName, String message, int id, String[] stringArguments,
            int severity, int startPosition, int endPosition, int line, int column) 

Source Link

Usage

From source file:at.bestsolution.efxclipse.tooling.fxml.compile.FxmlAnnotationCompilationParticipant.java

License:Open Source License

/**
 * @param context//from  ww w.j  a  va 2 s  . c  o  m
 */
private List<CategorizedProblem> checkCU(final ICompilationUnit unit,
        final Collection<CategorizedProblem> existingProblems) {
    List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>();
    if (existingProblems != null) {
        problems.addAll(existingProblems);
    }
    List<String> fxmlMethods = new ArrayList<String>();
    try {
        IJavaProject project = unit.getJavaProject();
        for (IType type : unit.getTypes()) {
            for (IMethod method : type.getMethods()) {
                for (IAnnotation a : method.getAnnotations()) {
                    if ("FXML".equals(a.getElementName())) { ////$NON-NLS-1$
                        if (fxmlMethods.contains(method.getElementName())) {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method name is not unique: " //$NON-NLS-1$
                                            + method.getElementName(),
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        fxmlMethods.add(method.getElementName());

                        switch (method.getNumberOfParameters()) {
                        case 0:
                            break;
                        case 1: {
                            ILocalVariable pType = method.getParameters()[0];
                            String[][] resolvedType = type
                                    .resolveType(Signature.toString(pType.getTypeSignature()));
                            IType parameterType = null;
                            if (resolvedType != null) {
                                parameterType = project.findType(resolvedType[0][0] + "." + resolvedType[0][1]); //$NON-NLS-1$
                            }
                            if (resolvedType == null || !Util.assignable(parameterType,
                                    project.findType("javafx.event.Event"))) { ////$NON-NLS-1$
                                DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                        "Parameter '" //$NON-NLS-1$
                                                + pType.getElementName()
                                                + "' is not assignable to javafx.event.Event", //$NON-NLS-1$
                                        IProblem.ExternalProblemNotFixable, new String[0],
                                        ProblemSeverities.Warning, pType.getSourceRange().getOffset(),
                                        pType.getSourceRange().getOffset() + pType.getSourceRange().getLength(),
                                        getMethodLineNumber(type, method), 0);
                                problems.add(problem);
                            }

                        }
                            break;
                        default: {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method must have 0 or exactly 1 argument", //$NON-NLS-1$
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return problems;
}

From source file:jmockit.assist.MockASTVisitor.java

License:Open Source License

private void addMarker(final ASTNode node, final String msg, final boolean isError) {
    try {//from  w w w.ja  va2s .c  o  m
        int start = node.getStartPosition();
        int endChar = start + node.getLength();
        int line = cu.getLineNumber(start);
        int col = cu.getColumnNumber(start);
        int id = IProblem.TypeRelated;
        String filePath = icunit.getPath().toOSString();
        String[] args = new String[] {};

        int severity = isError ? ProblemSeverities.Error : ProblemSeverities.Warning;

        CategorizedProblem problem = new DefaultProblem(filePath.toCharArray(), msg, id, args, severity, start,
                endChar, line, col) {
            @Override
            public String getMarkerType() {
                return JMockitCompilationParticipant.MARKER;
            }
        };

        probs.add(problem);
    } catch (Exception e) {
        Activator.log(e);
    }
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ScriptFolderCompilationParticipant.java

License:Open Source License

/**
 * @param buildContext/*  w w  w.  j  av  a 2 s  .  c o m*/
 * @return
 */
private CategorizedProblem[] createProblem(BuildContext buildContext) {
    DefaultProblem problem = new DefaultProblem(buildContext.getFile().getFullPath().toOSString().toCharArray(),
            "Error compiling Groovy project.  Either the Groovy-JDT patch is not installed or JavaBuilder is not being used.",
            0, new String[0], ProblemSeverities.Error, 0, 0, 1, 0);
    return new CategorizedProblem[] { problem };
}

From source file:org.eclipse.ajdt.internal.core.parserbridge.AJCompilationUnitDeclarationWrapper.java

License:Open Source License

public CompilationResult compilationResult() {
    CompilationResult cr = new CompilationResult(cUnit, delegate.compilationResult.unitIndex,
            delegate.compilationResult.totalUnitsKnown, 500);
    cr.lineSeparatorPositions = delegate.compilationResult.lineSeparatorPositions;
    cr.problemCount = delegate.compilationResult.problemCount;
    cr.compiledTypes = delegate.compilationResult.compiledTypes;
    cr.hasBeenAccepted = delegate.compilationResult.hasBeenAccepted;
    cr.qualifiedReferences = delegate.compilationResult.qualifiedReferences;
    cr.simpleNameReferences = delegate.compilationResult.simpleNameReferences;
    if (delegate.compilationResult.problems != null) {
        cr.problems = new CategorizedProblem[delegate.compilationResult.problems.length];
        for (int i = 0; i < delegate.compilationResult.problems.length; i++) {
            org.aspectj.org.eclipse.jdt.core.compiler.IProblem ajprob = delegate.compilationResult.problems[i];
            if (ajprob != null) {
                cr.problems[i] = new DefaultProblem(ajprob.getOriginatingFileName(), ajprob.getMessage(),
                        ajprob.getID(), ajprob.getArguments(),
                        ajprob.isWarning() ? ProblemSeverities.Error : ProblemSeverities.Warning,
                        ajprob.getSourceStart(), ajprob.getSourceEnd(), ajprob.getSourceLineNumber(), 0); // unknown column
            }/*from  w w w .j  a v  a  2 s.  c om*/
        }
    } else {
        cr.problems = new CategorizedProblem[0];
    }
    cr.taskCount = delegate.compilationResult.taskCount;
    return cr;
}

From source file:org.eclipse.ajdt.internal.ui.editor.CompilationUnitAnnotationModelWrapper.java

License:Open Source License

public void beginReporting() {
    if (delegate != null) {
        ((IProblemRequestor) delegate).beginReporting();

        IJavaProject project = unit.getJavaProject();

        AJCompilationUnitStructureRequestor requestor = new AJCompilationUnitStructureRequestor(unit,
                new AJCompilationUnitInfo(), new HashMap());
        JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = ((CompilationUnit) unit)
                .getPerWorkingCopyInfo();
        boolean computeProblems = JavaProject.hasJavaNature(project.getProject()) && perWorkingCopyInfo != null
                && perWorkingCopyInfo.isActive();
        IProblemFactory problemFactory = new DefaultProblemFactory();
        Map options = project.getOptions(true);
        IBuffer buffer;/*from   ww  w .j  a  va  2s  .  c om*/
        try {
            buffer = unit.getBuffer();

            final char[] contents = buffer == null ? null : buffer.getCharacters();

            AJSourceElementParser parser = new AJSourceElementParser(requestor, problemFactory,
                    new CompilerOptions(options), true/*report local declarations*/, false);
            parser.reportOnlyOneSyntaxError = !computeProblems;

            parser.scanner.source = contents;
            requestor.setParser(parser);

            CompilationUnitDeclaration unitDec = parser.parseCompilationUnit(
                    new org.aspectj.org.eclipse.jdt.internal.compiler.env.ICompilationUnit() {
                        public char[] getContents() {
                            return contents;
                        }

                        public char[] getMainTypeName() {
                            return ((CompilationUnit) unit).getMainTypeName();
                        }

                        public char[][] getPackageName() {
                            return ((CompilationUnit) unit).getPackageName();
                        }

                        public char[] getFileName() {
                            return ((CompilationUnit) unit).getFileName();
                        }

                        public boolean ignoreOptionalProblems() {
                            return false;
                        }
                    }, true /*full parse to find local elements*/);
            org.aspectj.org.eclipse.jdt.core.compiler.IProblem[] problems = unitDec.compilationResult.problems;
            if (problems != null) {
                for (int i = 0; i < problems.length; i++) {
                    org.aspectj.org.eclipse.jdt.core.compiler.IProblem problem = problems[i];
                    if (problem == null)
                        continue;
                    ((IProblemRequestor) delegate)
                            .acceptProblem(
                                    new DefaultProblem(problem.getOriginatingFileName(), problem.getMessage(),
                                            problem.getID(), problem.getArguments(),
                                            problem.isError() ? ProblemSeverities.Error
                                                    : ProblemSeverities.Warning,
                                            problem.getSourceStart(), problem.getSourceEnd(),
                                            problem.getSourceLineNumber(), 0)); // unknown column
                }
            }
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.fx.ide.fxml.compile.FxmlAnnotationCompilationParticipant.java

License:Open Source License

/**
 * @param context//from w ww .j  ava 2 s.c  o  m
 */
private static List<CategorizedProblem> checkCU(final ICompilationUnit unit,
        final Collection<CategorizedProblem> existingProblems) {
    List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>();
    if (existingProblems != null) {
        problems.addAll(existingProblems);
    }
    List<String> fxmlMethods = new ArrayList<String>();
    try {
        IJavaProject project = unit.getJavaProject();
        for (IType type : unit.getTypes()) {
            for (IMethod method : type.getMethods()) {
                for (IAnnotation a : method.getAnnotations()) {
                    if ("FXML".equals(a.getElementName())) { ////$NON-NLS-1$
                        if (fxmlMethods.contains(method.getElementName())) {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method name is not unique: " //$NON-NLS-1$
                                            + method.getElementName(),
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        fxmlMethods.add(method.getElementName());

                        switch (method.getNumberOfParameters()) {
                        case 0:
                            break;
                        case 1: {
                            ILocalVariable pType = method.getParameters()[0];
                            String[][] resolvedType = type
                                    .resolveType(Signature.toString(pType.getTypeSignature()));
                            IType parameterType = null;
                            if (resolvedType != null) {
                                parameterType = project.findType(resolvedType[0][0] + "." + resolvedType[0][1]); //$NON-NLS-1$
                            }
                            if (resolvedType == null || !Util.assignable(parameterType,
                                    project.findType("javafx.event.Event"))) { ////$NON-NLS-1$
                                DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                        "Parameter '" //$NON-NLS-1$
                                                + pType.getElementName()
                                                + "' is not assignable to javafx.event.Event", //$NON-NLS-1$
                                        IProblem.ExternalProblemNotFixable, new String[0],
                                        ProblemSeverities.Warning, pType.getSourceRange().getOffset(),
                                        pType.getSourceRange().getOffset() + pType.getSourceRange().getLength(),
                                        getMethodLineNumber(type, method), 0);
                                problems.add(problem);
                            }

                        }
                            break;
                        default: {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method must have 0 or exactly 1 argument", //$NON-NLS-1$
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return problems;
}

From source file:quarkninja.mode.xqmode.ErrorCheckerService.java

License:Open Source License

/**
 * The name cannot get any simpler, can it?
 *//* w  ww  . j  a  v a 2 s . c  o  m*/
private void compileCheck() {

    // Currently (Sept, 2012) I'm using Java's reflection api to load the
    // CompilationChecker class(from CompilationChecker.jar) that houses the
    // Eclispe JDT compiler and call its getErrorsAsObj method to obtain
    // errors. This way, I'm able to add the paths of contributed libraries
    // to the classpath of CompilationChecker, dynamically.

    try {

        // NOTE TO SELF: If classpath contains null Strings
        // URLClassLoader gets angry. Drops NPE bombs.

        // If imports have changed, reload classes with new classpath.
        if (loadCompClass) {

            if (classpathJars.size() > 0)
                System.out.println("XQMode: Loading contributed libraries referenced by import statements.");
            File f = new File(editor.getBase().getSketchbookFolder().getAbsolutePath() + File.separator
                    + "modes" + File.separator + "XQMode" + File.separator + "mode");

            FileFilter fileFilter = new FileFilter() {
                public boolean accept(File file) {
                    return (file.getName().endsWith(".jar") && !file.getName().startsWith("XQMode"));
                }
            };

            File[] jarFiles = f.listFiles(fileFilter);
            for (File jarFile : jarFiles) {
                classpathJars.add(jarFile.toURI().toURL());
            }

            // for (int i = 0; i < tempPath.length; i++) {
            // classpathJars.add(new URL(tempPath[i]));
            // }

            classpath = new URL[classpathJars.size()]; // + 1 for
            // Compilation
            // Checker class
            for (int i = 0; i < classpathJars.size(); i++) {
                classpath[i] = classpathJars.get(i);
            }

            // System.out.println("-- " + classpath.length);
            URLClassLoader classLoader = new URLClassLoader(classpath);
            // System.out.println("1.");
            checkerClass = Class.forName("CompilationChecker", true, classLoader);
            // System.out.println("2.");
            compilationChecker = checkerClass.newInstance();
            loadCompClass = false;
        }

        if (compilerSettings == null)
            prepareCompilerSetting();
        Method getErrors = checkerClass.getMethod("getErrorsAsObjArr",
                new Class[] { String.class, String.class, Map.class });
        // Method disp = checkerClass.getMethod("test", (Class<?>[])null);

        Object[][] errorList = (Object[][]) getErrors.invoke(compilationChecker, className, sourceCode,
                compilerSettings);

        if (errorList == null)
            return;

        problems = new DefaultProblem[errorList.length];

        for (int i = 0; i < errorList.length; i++) {

            // for (int j = 0; j < errorList[i].length; j++)
            // System.out.print(errorList[i][j] + ", ");

            problems[i] = new DefaultProblem((char[]) errorList[i][0], (String) errorList[i][1],
                    ((Integer) errorList[i][2]).intValue(), (String[]) errorList[i][3],
                    ((Integer) errorList[i][4]).intValue(), ((Integer) errorList[i][5]).intValue(),
                    ((Integer) errorList[i][6]).intValue(), ((Integer) errorList[i][7]).intValue(), 0);

            // System.out
            // .println("ECS: " + problems[i].getMessage() + ","
            // + problems[i].isError() + ","
            // + problems[i].isWarning());

            IProblem problem = problems[i];

            int a[] = calculateTabIndexAndLineNumber(problem);
            Problem p = new Problem(problem, a[0], a[1]);
            if ((Boolean) errorList[i][8])
                p.setType(Problem.ERROR);
            if ((Boolean) errorList[i][9])
                p.setType(Problem.WARNING);

            if (p.isWarning() && !warningsEnabled)
                continue;
            problemsList.add(p);
        }

    } catch (ClassNotFoundException e) {
        System.err.println("Compiltation Checker files couldn't be found! " + e + " compileCheck() problem.");
        stopThread();
    } catch (MalformedURLException e) {
        System.err.println("Compiltation Checker files couldn't be found! " + e + " compileCheck() problem.");
        stopThread();
    } catch (Exception e) {
        System.err.println("compileCheck() problem." + e);
        e.printStackTrace();
        stopThread();
    } catch (NoClassDefFoundError e) {
        System.err.println(e + " compileCheck() problem. Somebody tried to mess with XQMode files.");
        stopThread();
    }
    // System.out.println("Compilecheck, Done.");
}