View | Details | Raw Unified | Return to bug 88265 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/AttachSourceTests.java (+14 lines)
Lines 1470-1473 Link Here
1470
	attachSource(root, null, null); // detach source
1470
	attachSource(root, null, null); // detach source
1471
	root.close();
1471
	root.close();
1472
}
1472
}
1473
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=88265
1474
// Test to ensure availability and correctness of API SourceRange
1475
public void test88265 () {
1476
	org.eclipse.jdt.core.SourceRange one = new org.eclipse.jdt.core.SourceRange(10, 7);
1477
	org.eclipse.jdt.core.SourceRange two = new org.eclipse.jdt.core.SourceRange(9, 13);
1478
	assertTrue(two.getOffset() == 9);
1479
	assertTrue(two.getLength() == 13);
1480
	assertFalse(one.equals(two));
1481
	SourceRange three = new org.eclipse.jdt.core.SourceRange(10, 7);
1482
	assertTrue(one.equals(three));
1483
	assertTrue(SourceRange.isAvailable(one));
1484
	assertFalse(SourceRange.isAvailable(null));
1485
	assertFalse(SourceRange.isAvailable(new SourceRange(-1, 0)));
1486
}
1473
}
1487
}
(-)src/org/eclipse/jdt/core/tests/dom/ASTNodeFinderTest.java (-1 / +1 lines)
Lines 15-25 Link Here
15
15
16
import org.eclipse.jdt.core.ICompilationUnit;
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.JavaModelException;
17
import org.eclipse.jdt.core.JavaModelException;
18
import org.eclipse.jdt.core.SourceRange;
18
import org.eclipse.jdt.core.compiler.CharOperation;
19
import org.eclipse.jdt.core.compiler.CharOperation;
19
import org.eclipse.jdt.core.dom.AST;
20
import org.eclipse.jdt.core.dom.AST;
20
import org.eclipse.jdt.core.dom.ASTNode;
21
import org.eclipse.jdt.core.dom.ASTNode;
21
import org.eclipse.jdt.core.dom.NodeFinder;
22
import org.eclipse.jdt.core.dom.NodeFinder;
22
import org.eclipse.jdt.internal.core.SourceRange;
23
23
24
public class ASTNodeFinderTest extends ConverterTestSetup {
24
public class ASTNodeFinderTest extends ConverterTestSetup {
25
25
(-)model/org/eclipse/jdt/internal/core/SourceRefElementInfo.java (+1 lines)
Lines 11-16 Link Here
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import org.eclipse.jdt.core.ISourceRange;
13
import org.eclipse.jdt.core.ISourceRange;
14
import org.eclipse.jdt.core.SourceRange;
14
15
15
/**
16
/**
16
 * Element info for ISourceReference elements.
17
 * Element info for ISourceReference elements.
(-)model/org/eclipse/jdt/internal/core/SourceRange.java (-62 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.core;
12
13
import org.eclipse.jdt.core.ISourceRange;
14
15
/**
16
 * @see ISourceRange
17
 */
18
public class SourceRange implements ISourceRange {
19
20
protected int offset, length;
21
22
public SourceRange(int offset, int length) {
23
	this.offset = offset;
24
	this.length = length;
25
}
26
/*
27
 * @see Object#equals(Object)
28
 */
29
public boolean equals(Object obj) {
30
	if (!(obj instanceof ISourceRange))
31
        return false;
32
	ISourceRange sourceRange = (ISourceRange) obj;
33
    return sourceRange.getOffset() == this.offset && sourceRange.getLength() == this.length;
34
}
35
/**
36
 * @see ISourceRange
37
 */
38
public int getLength() {
39
	return this.length;
40
}
41
/**
42
 * @see ISourceRange
43
 */
44
public int getOffset() {
45
	return this.offset;
46
}
47
/*
48
 * @see Object#hashCode()
49
 */
50
public int hashCode() {
51
    return this.length ^ this.offset;
52
}
53
public String toString() {
54
	StringBuffer buffer = new StringBuffer();
55
	buffer.append("[offset="); //$NON-NLS-1$
56
	buffer.append(this.offset);
57
	buffer.append(", length="); //$NON-NLS-1$
58
	buffer.append(this.length);
59
	buffer.append("]"); //$NON-NLS-1$
60
	return buffer.toString();
61
}
62
}
(-)model/org/eclipse/jdt/internal/core/ImportContainer.java (-6 lines)
Lines 11-22 Link Here
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import org.eclipse.jdt.core.*;
13
import org.eclipse.jdt.core.*;
14
import org.eclipse.jdt.core.IImportContainer;
15
import org.eclipse.jdt.core.IImportDeclaration;
16
import org.eclipse.jdt.core.IJavaElement;
17
import org.eclipse.jdt.core.ISourceRange;
18
import org.eclipse.jdt.core.ISourceReference;
19
import org.eclipse.jdt.core.JavaModelException;
20
import org.eclipse.jdt.internal.core.util.MementoTokenizer;
14
import org.eclipse.jdt.internal.core.util.MementoTokenizer;
21
15
22
/**
16
/**
(-)model/org/eclipse/jdt/internal/core/SourceMapper.java (+1 lines)
Lines 47-52 Link Here
47
import org.eclipse.jdt.core.JavaCore;
47
import org.eclipse.jdt.core.JavaCore;
48
import org.eclipse.jdt.core.JavaModelException;
48
import org.eclipse.jdt.core.JavaModelException;
49
import org.eclipse.jdt.core.Signature;
49
import org.eclipse.jdt.core.Signature;
50
import org.eclipse.jdt.core.SourceRange;
50
import org.eclipse.jdt.core.compiler.CategorizedProblem;
51
import org.eclipse.jdt.core.compiler.CategorizedProblem;
51
import org.eclipse.jdt.core.compiler.CharOperation;
52
import org.eclipse.jdt.core.compiler.CharOperation;
52
import org.eclipse.jdt.internal.compiler.IProblemFactory;
53
import org.eclipse.jdt.internal.compiler.IProblemFactory;
(-)model/org/eclipse/jdt/internal/core/ClassFile.java (-2 / +2 lines)
Lines 445-452 Link Here
445
445
446
				SourceRange range = mapper.getSourceRange(classFile.getType());
446
				SourceRange range = mapper.getSourceRange(classFile.getType());
447
				if (range == SourceMapper.UNKNOWN_RANGE) continue;
447
				if (range == SourceMapper.UNKNOWN_RANGE) continue;
448
				int newStart = range.offset;
448
				int newStart = range.getOffset();
449
				int newEnd = newStart + range.length - 1;
449
				int newEnd = newStart + range.getLength() - 1;
450
				if(newStart > start && newEnd < end
450
				if(newStart > start && newEnd < end
451
						&& newStart <= position && newEnd >= position) {
451
						&& newStart <= position && newEnd >= position) {
452
					type = classFile.getType();
452
					type = classFile.getType();
(-)model/org/eclipse/jdt/internal/core/Annotation.java (+1 lines)
Lines 16-21 Link Here
16
import org.eclipse.jdt.core.IMemberValuePair;
16
import org.eclipse.jdt.core.IMemberValuePair;
17
import org.eclipse.jdt.core.ISourceRange;
17
import org.eclipse.jdt.core.ISourceRange;
18
import org.eclipse.jdt.core.JavaModelException;
18
import org.eclipse.jdt.core.JavaModelException;
19
import org.eclipse.jdt.core.SourceRange;
19
import org.eclipse.jdt.internal.compiler.env.IBinaryAnnotation;
20
import org.eclipse.jdt.internal.compiler.env.IBinaryAnnotation;
20
import org.eclipse.jdt.internal.compiler.env.IBinaryElementValuePair;
21
import org.eclipse.jdt.internal.compiler.env.IBinaryElementValuePair;
21
import org.eclipse.jdt.internal.core.util.Util;
22
import org.eclipse.jdt.internal.core.util.Util;
(-)model/org/eclipse/jdt/internal/core/TypeParameter.java (-3 lines)
Lines 11-19 Link Here
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import org.eclipse.jdt.core.*;
13
import org.eclipse.jdt.core.*;
14
import org.eclipse.jdt.core.IMember;
15
import org.eclipse.jdt.core.ITypeParameter;
16
import org.eclipse.jdt.core.JavaModelException;
17
import org.eclipse.jdt.core.compiler.CharOperation;
14
import org.eclipse.jdt.core.compiler.CharOperation;
18
15
19
public class TypeParameter extends SourceRefElement implements ITypeParameter {
16
public class TypeParameter extends SourceRefElement implements ITypeParameter {
(-)model/org/eclipse/jdt/internal/core/CompilationUnitElementInfo.java (+1 lines)
Lines 11-16 Link Here
11
package org.eclipse.jdt.internal.core;
11
package org.eclipse.jdt.internal.core;
12
12
13
import org.eclipse.jdt.core.ISourceRange;
13
import org.eclipse.jdt.core.ISourceRange;
14
import org.eclipse.jdt.core.SourceRange;
14
15
15
public class CompilationUnitElementInfo extends OpenableElementInfo {
16
public class CompilationUnitElementInfo extends OpenableElementInfo {
16
17
(-)model/org/eclipse/jdt/core/ISourceRange.java (-2 / +4 lines)
Lines 28-37 Link Here
28
int getLength();
28
int getLength();
29
/**
29
/**
30
 * Returns the 0-based index of the first character of the source code for this element,
30
 * Returns the 0-based index of the first character of the source code for this element,
31
 * relative to the source buffer in which this element is contained.
31
 * relative to the source buffer in which this element is contained. However, if the element
32
 * has no associated source code, an implementation may return -1. 
32
 *
33
 *
33
 * @return the 0-based index of the first character of the source code for this element,
34
 * @return the 0-based index of the first character of the source code for this element,
34
 * relative to the source buffer in which this element is contained
35
 * relative to the source buffer in which this element is contained. However, if the element
36
 * has no associated source code, an implementation may return -1. 
35
 */
37
 */
36
int getOffset();
38
int getOffset();
37
}
39
}
(-)model/org/eclipse/jdt/core/SourceRange.java (+79 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.core;
12
13
14
/**
15
 * A source range defines an element's source coordinates relative to
16
 * its source buffer.
17
 * @see ISourceRange
18
 * @since 3.6
19
 */
20
public class SourceRange implements ISourceRange {
21
22
	private int offset, length;
23
24
	public SourceRange(int offset, int length) {
25
		this.offset = offset;
26
		this.length = length;
27
	}
28
	/*
29
	 * @see Object#equals(Object)
30
	 */
31
	public boolean equals(Object obj) {
32
		if (!(obj instanceof ISourceRange))
33
			return false;
34
		ISourceRange sourceRange = (ISourceRange) obj;
35
		return sourceRange.getOffset() == this.offset && sourceRange.getLength() == this.length;
36
	}
37
	/**
38
	 * @see ISourceRange
39
	 */
40
	public int getLength() {
41
		return this.length;
42
	}
43
	/**
44
	 * @see ISourceRange
45
	 */
46
	public int getOffset() {
47
		return this.offset;
48
	}
49
	/*
50
	 * @see Object#hashCode()
51
	 */
52
	public int hashCode() {
53
		return this.length ^ this.offset;
54
	}
55
56
	/**
57
	 * Helper method that answers whether a valid source range is available
58
	 * in the given ISourceRange. When an element has no associated source
59
	 * code, Java Model APIs may return either <code>null</code> or a range of
60
	 * [-1, 0] to indicate an invalid range. This utility method can be used
61
	 * to detect that case.
62
	 *
63
	 * @param range a source range, can be <code>null</code>
64
	 * @return <code>true</code> iff range is not null and range.getOffset() is not -1
65
	 */
66
	public static boolean isAvailable(ISourceRange range) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=130161
67
		return range != null && range.getOffset() != -1;
68
	}
69
70
	public String toString() {
71
		StringBuffer buffer = new StringBuffer();
72
		buffer.append("[offset="); //$NON-NLS-1$
73
		buffer.append(this.offset);
74
		buffer.append(", length="); //$NON-NLS-1$
75
		buffer.append(this.length);
76
		buffer.append("]"); //$NON-NLS-1$
77
		return buffer.toString();
78
	}
79
}

Return to bug 88265