Java Code Examples for org.eclipse.jdt.core.compiler.IProblem#getSourceStart()

The following examples show how to use org.eclipse.jdt.core.compiler.IProblem#getSourceStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaASTUtils.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns <code>true</code> if any of the problems fall within the
 * {@link ASTNode}'s source range.
 */
public static boolean hasErrors(ASTNode node, IProblem[] problems) {
  int startPosition = node.getStartPosition();
  int endPosition = startPosition + node.getLength() - 1;

  for (IProblem problem : problems) {
    if (!problem.isError()) {
      // Skip any problem that is not an error
      continue;
    }
    if (problem.getSourceStart() >= startPosition
        && problem.getSourceEnd() <= endPosition) {
      return true;
    }
  }

  return false;
}
 
Example 2
Source File: CompilationUnitDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected Position createPositionFromProblem(IProblem problem) {
	int start= problem.getSourceStart();
	int end= problem.getSourceEnd();

	if (start == -1 && end == -1)
		return new Position(0);

	if (start == -1)
		return new Position(end);

	if (end == -1)
		return new Position(start);

	int length= end - start + 1;
	if (length < 0)
		return null;

	return new Position(start, length);
}
 
Example 3
Source File: CompilationUnit.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the list of messages reported by the compiler during the parsing
 * or the type checking of this compilation unit. This list might be a subset of
 * errors detected and reported by a Java compiler.
 * <p>
 * This list of messages is suitable for simple clients that do little
 * more than log the messages or display them to the user. Clients that
 * need further details should call <code>getProblems</code> to get
 * compiler problem objects.
 * </p>
 *
 * @return the list of messages, possibly empty
 * @see #getProblems()
 * @see ASTParser
 */
public Message[] getMessages() {
	if (this.messages == null) {
		int problemLength = this.problems.length;
		if (problemLength == 0) {
			this.messages = EMPTY_MESSAGES;
		} else {
			this.messages = new Message[problemLength];
			for (int i = 0; i < problemLength; i++) {
				IProblem problem = this.problems[i];
				int start = problem.getSourceStart();
				int end = problem.getSourceEnd();
				this.messages[i] = new Message(problem.getMessage(), start, end - start + 1);
			}
		}
	}
	return this.messages;
}
 
Example 4
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IProblem[] getProblems(ASTNode node, int scope, int severity) {
	ASTNode root= node.getRoot();
	if (!(root instanceof CompilationUnit))
		return EMPTY_PROBLEMS;
	IProblem[] problems= ((CompilationUnit)root).getProblems();
	if (root == node)
		return problems;
	final int iterations= computeIterations(scope);
	List<IProblem> result= new ArrayList<IProblem>(5);
	for (int i= 0; i < problems.length; i++) {
		IProblem problem= problems[i];
		boolean consider= false;
		if ((severity & PROBLEMS) == PROBLEMS)
			consider= true;
		else if ((severity & WARNING) != 0)
			consider= problem.isWarning();
		else if ((severity & ERROR) != 0)
			consider= problem.isError();
		if (consider) {
			ASTNode temp= node;
			int count= iterations;
			do {
				int nodeOffset= temp.getStartPosition();
				int problemOffset= problem.getSourceStart();
				if (nodeOffset <= problemOffset && problemOffset < nodeOffset + temp.getLength()) {
					result.add(problem);
					count= 0;
				} else {
					count--;
				}
			} while ((temp= temp.getParent()) != null && count > 0);
		}
	}
	return result.toArray(new IProblem[result.size()]);
}
 
Example 5
Source File: LinkedNodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int getNameNodeProblemKind(IProblem[] problems, SimpleName nameNode) {
	int nameOffset= nameNode.getStartPosition();
	int nameInclEnd= nameOffset + nameNode.getLength() - 1;

	for (int i= 0; i < problems.length; i++) {
		IProblem curr= problems[i];
		if (curr.getSourceStart() == nameOffset && curr.getSourceEnd() == nameInclEnd) {
			int kind= getProblemKind(curr);
			if (kind != 0) {
				return kind;
			}
		}
	}
	return 0;
}
 
Example 6
Source File: LinkedNodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static SimpleName[] findByProblems(ASTNode parent, SimpleName nameNode) {
	ArrayList<SimpleName> res= new ArrayList<SimpleName>();

	ASTNode astRoot = parent.getRoot();
	if (!(astRoot instanceof CompilationUnit)) {
		return null;
	}

	IProblem[] problems= ((CompilationUnit) astRoot).getProblems();
	int nameNodeKind= getNameNodeProblemKind(problems, nameNode);
	if (nameNodeKind == 0) { // no problem on node
		return null;
	}

	int bodyStart= parent.getStartPosition();
	int bodyEnd= bodyStart + parent.getLength();

	String name= nameNode.getIdentifier();

	for (int i= 0; i < problems.length; i++) {
		IProblem curr= problems[i];
		int probStart= curr.getSourceStart();
		int probEnd= curr.getSourceEnd() + 1;

		if (probStart > bodyStart && probEnd < bodyEnd) {
			int currKind= getProblemKind(curr);
			if ((nameNodeKind & currKind) != 0) {
				ASTNode node= NodeFinder.perform(parent, probStart, (probEnd - probStart));
				if (node instanceof SimpleName && name.equals(((SimpleName) node).getIdentifier())) {
					res.add((SimpleName) node);
				}
			}
		}
	}
	return res.toArray(new SimpleName[res.size()]);
}
 
Example 7
Source File: ProblemHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasProblem(IProblem[] problems, IProblemLocation location) {
	for (int i= 0; i < problems.length; i++) {
		IProblem problem= problems[i];
		if (problem.getID() == location.getProblemId() && problem.getSourceStart() == location.getOffset())
			return true;
	}
	return false;
}
 
Example 8
Source File: ProblemLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ProblemLocation(IProblem problem) {
	fId= problem.getID();
	fArguments= problem.getArguments();
	fOffset= problem.getSourceStart();
	fLength= problem.getSourceEnd() - fOffset + 1;
	fIsError= problem.isError();
	fMarkerType= problem instanceof CategorizedProblem ? ((CategorizedProblem) problem).getMarkerType() : IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER;
}
 
Example 9
Source File: CorrectionMarkerResolutionGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasProblem(IProblem[] problems, IProblemLocation location) {
	for (int i= 0; i < problems.length; i++) {
		IProblem problem= problems[i];
		if (problem.getID() == location.getProblemId() && problem.getSourceStart() == location.getOffset())
			return true;
	}
	return false;
}
 
Example 10
Source File: JdtCompiler.java    From jetbrick-template-1x with Apache License 2.0 5 votes vote down vote up
@Override
protected void generateJavaClass(JavaSource source) throws IOException {
    INameEnvironment env = new NameEnvironment(source);
    IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    CompilerOptions options = getCompilerOptions();
    CompilerRequestor requestor = new CompilerRequestor();
    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());

    Compiler compiler = new Compiler(env, policy, options, requestor, problemFactory);
    compiler.compile(new ICompilationUnit[] { new CompilationUnit(source) });

    if (requestor.hasErrors()) {
        String sourceCode = source.getSourceCode();
        String[] sourceCodeLines = sourceCode.split("(\r\n|\r|\n)", -1);
        StringBuilder sb = new StringBuilder();
        sb.append("Compilation failed.");
        sb.append('\n');
        for (IProblem p : requestor.getErrors()) {
            sb.append(p.getMessage()).append('\n');
            int start = p.getSourceStart();
            int column = start; // default
            for (int i = start; i >= 0; i--) {
                char c = sourceCode.charAt(i);
                if (c == '\n' || c == '\r') {
                    column = start - i;
                    break;
                }
            }
            sb.append(StringUtils.getPrettyError(sourceCodeLines, p.getSourceLineNumber(), column, p.getSourceStart(), p.getSourceEnd(), 3));
        }
        sb.append(requestor.getErrors().length);
        sb.append(" error(s)\n");
        throw new CompileErrorException(sb.toString());
    }

    requestor.save(source.getOutputdir());
}
 
Example 11
Source File: Preprocessor.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
public CompilerProblem buildCompilerProblem(IProblem problem) throws TextTransform.LockException {
	// ECJ gives us the full path to the file, we just want the filename
	String filename = new File(new String(problem.getOriginatingFileName())).getName();
	
	// Check to see if main file or not
	if (context.getSketchMainFilename().equals(filename)) {
		return buildCompilerProblem(
				new TextTransform.Range(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1),
				problem.isError(), problem.getMessage(), false);
	} else {
		// Check all java files
		for (SketchCode sketchFile : context.getSketchFiles()) {
			if (sketchFile.isJava() && sketchFile.getFilename().equals(filename)) {
				int absStart = problem.getSourceStart() - sketchFile.javaImportHeaderOffset;
				int line = sketchFile.lineForOffset(absStart);
				int start = absStart - sketchFile.offsetForLine(line);
				
				return new CompilerProblem(sketchFile, line, start,
						problem.getSourceEnd() - problem.getSourceStart() + 1,
						problem.isError(), problem.getMessage());
			}
		}
		
		// If we can't find it
		System.err.println("Unable to find java file: " + filename);
		return new CompilerProblem(context.getSketchFiles().get(0), 0, 0, 1,
				problem.isError(), problem.getMessage());
	}
}
 
Example 12
Source File: JRJdtCompiler.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void acceptResult(CompilationResult result) 
{
	String className = ((CompilationUnit) result.getCompilationUnit()).className;
	
	int classIdx;
	for (classIdx = 0; classIdx < units.length; ++classIdx)
	{
		if (className.equals(units[classIdx].getName()))
		{
			break;
		}
	}
	
	if (result.hasErrors()) 
	{
		//IProblem[] problems = result.getErrors();
		IProblem[] problems = getJavaCompilationErrors(result);
		
		unitResults[classIdx].problems = problems;

		String sourceCode = units[classIdx].getSourceCode();
		
		for (int i = 0; i < problems.length; i++) 
		{
			IProblem problem = problems[i];

			if (IProblem.UndefinedMethod == problem.getID())
			{
				if (
					problem.getSourceStart() >= 0
					&& problem.getSourceEnd() >= 0
					)
				{									
					String methodName = 
						sourceCode.substring(
							problem.getSourceStart(),
							problem.getSourceEnd() + 1
							);
					
					Method method = FunctionsUtil.getInstance(jasperReportsContext).getMethod4Function(methodName);
					if (method != null)
					{
						unitResults[classIdx].addMissingMethod(method);
						//continue;
					}
				}
			}
		}
	}
	else
	{
		ClassFile[] resultClassFiles = result.getClassFiles();
		for (int i = 0; i < resultClassFiles.length; i++) 
		{
			units[classIdx].setCompileData(resultClassFiles[i].getBytes());
		}
	}
}
 
Example 13
Source File: JRJdtCompiler.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 
 */
public String getFormattedProblems() 
{
	StringBuilder problemBuilder = new StringBuilder();
	
	for (int u = 0; u < units.length; u++) 
	{
		String sourceCode = units[u].getSourceCode();
		
		IProblem[] problems = unitResults[u].problems;
		
		if (problems != null && problems.length > 0)
		{
			for (int i = 0; i < problems.length; i++) 
			{
				IProblem problem = problems[i];
	
				problemBuilder.append(i + 1);
				problemBuilder.append(". ");
				problemBuilder.append(problem.getMessage());
	
				if (
					problem.getSourceStart() >= 0
					&& problem.getSourceEnd() >= 0
					)
				{									
					int problemStartIndex = sourceCode.lastIndexOf("\n", problem.getSourceStart()) + 1;
					int problemEndIndex = sourceCode.indexOf("\n", problem.getSourceEnd());
					if (problemEndIndex < 0)
					{
						problemEndIndex = sourceCode.length();
					}
					
					problemBuilder.append("\n");
					problemBuilder.append(
						sourceCode.substring(
							problemStartIndex,
							problemEndIndex
							)
						);
					problemBuilder.append("\n");
					for(int j = problemStartIndex; j < problem.getSourceStart(); j++)
					{
						problemBuilder.append(" ");
					}
					if (problem.getSourceStart() == problem.getSourceEnd())
					{
						problemBuilder.append("^");
					}
					else
					{
						problemBuilder.append("<");
						for(int j = problem.getSourceStart() + 1; j < problem.getSourceEnd(); j++)
						{
							problemBuilder.append("-");
						}
						problemBuilder.append(">");
					}
	
					problemBuilder.append("\n");
				}
			}
			
			problemBuilder.append(problems.length);
			problemBuilder.append(" errors\n");
		}
	}
	
	return problemBuilder.length() > 0 ? problemBuilder.toString() : null;
}
 
Example 14
Source File: SourceRangeFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static ISourceRange create(IProblem problem) {
	return new SourceRange(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
}
 
Example 15
Source File: SourceRange.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public SourceRange(IProblem problem) {
	this(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
}
 
Example 16
Source File: SourceRangeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static ISourceRange create(IProblem problem) {
	return new SourceRange(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
}