View | Details | Raw Unified | Return to bug 366007
Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AbstractBindingLabelsTest.java (+25 lines)
Lines 10-24 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.ui.tests.core;
11
package org.eclipse.jdt.ui.tests.core;
12
12
13
import java.io.File;
14
import java.io.IOException;
13
import java.util.regex.Matcher;
15
import java.util.regex.Matcher;
14
import java.util.regex.Pattern;
16
import java.util.regex.Pattern;
15
17
18
import org.osgi.framework.Bundle;
19
16
import org.eclipse.jdt.testplugin.JavaProjectHelper;
20
import org.eclipse.jdt.testplugin.JavaProjectHelper;
21
22
import org.eclipse.core.runtime.FileLocator;
23
import org.eclipse.core.runtime.Path;
24
import org.eclipse.core.runtime.Platform;
17
25
18
import org.eclipse.jface.preference.IPreferenceStore;
26
import org.eclipse.jface.preference.IPreferenceStore;
19
27
20
import org.eclipse.jdt.core.IJavaElement;
28
import org.eclipse.jdt.core.IJavaElement;
21
import org.eclipse.jdt.core.IJavaProject;
29
import org.eclipse.jdt.core.IJavaProject;
30
import org.eclipse.jdt.core.JavaCore;
31
import org.eclipse.jdt.core.JavaModelException;
22
import org.eclipse.jdt.core.dom.AST;
32
import org.eclipse.jdt.core.dom.AST;
23
import org.eclipse.jdt.core.dom.ASTParser;
33
import org.eclipse.jdt.core.dom.ASTParser;
24
import org.eclipse.jdt.core.dom.IBinding;
34
import org.eclipse.jdt.core.dom.IBinding;
Lines 35-40 Link Here
35
45
36
	protected IJavaProject fJProject1;
46
	protected IJavaProject fJProject1;
37
	protected boolean fHaveSource= true;
47
	protected boolean fHaveSource= true;
48
	private String ANNOTATION_JAR_PATH;
38
49
39
	protected void setUp() throws Exception {
50
	protected void setUp() throws Exception {
40
		fJProject1= ProjectTestSetup.getProject();
51
		fJProject1= ProjectTestSetup.getProject();
Lines 45-53 Link Here
45
56
46
57
47
	protected void tearDown() throws Exception {
58
	protected void tearDown() throws Exception {
59
		fJProject1.setOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.DISABLED);
48
		JavaProjectHelper.clear(fJProject1, ProjectTestSetup.getDefaultClasspath());
60
		JavaProjectHelper.clear(fJProject1, ProjectTestSetup.getDefaultClasspath());
49
	}
61
	}
50
62
63
	protected void setupForNullAnnotations(String version) throws IOException, JavaModelException {
64
		fJProject1.setOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
65
		if (this.ANNOTATION_JAR_PATH == null) {
66
			Bundle[] bundles= Platform.getBundles("org.eclipse.jdt.annotation", version);
67
			File bundleFile= FileLocator.getBundleFile(bundles[0]);
68
			if (bundleFile.isDirectory())
69
				this.ANNOTATION_JAR_PATH= bundleFile.getPath() + "/bin";
70
			else
71
				this.ANNOTATION_JAR_PATH= bundleFile.getPath();
72
		}
73
		JavaProjectHelper.addLibrary(fJProject1, new Path(ANNOTATION_JAR_PATH));
74
	}
75
51
	protected String getBindingLabel(IJavaElement elem, long flags) {
76
	protected String getBindingLabel(IJavaElement elem, long flags) {
52
		ASTParser parser= ASTParser.newParser(AST.JLS8);
77
		ASTParser parser= ASTParser.newParser(AST.JLS8);
53
		parser.setResolveBindings(true);
78
		parser.setResolveBindings(true);
(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/BindingLabelsTest.java (-1 / +32 lines)
Lines 732-738 Link Here
732
		lab= getBindingLabel(foo2, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_POST_QUALIFIED | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
732
		lab= getBindingLabel(foo2, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_POST_QUALIFIED | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
733
		assertLinkMatch(lab, "{{org.test.LambdaTests.{...}|Local}} toStringL - {{org}}.{{test}}.{{LambdaTests}}.{...}");
733
		assertLinkMatch(lab, "{{org.test.LambdaTests.{...}|Local}} toStringL - {{org}}.{{test}}.{{LambdaTests}}.{...}");
734
		
734
		
735
// can't select the constructor, only the type (label computation works fine once we find the binding) 
735
// can't select the constructor, only the type (label computation works fine once we find the binding)
736
//		IJavaElement ctor= cu.codeSelect(content.indexOf("Local()"), 0)[0];
736
//		IJavaElement ctor= cu.codeSelect(content.indexOf("Local()"), 0)[0];
737
//		lab= getBindingLabel(ctor, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_FULLY_QUALIFIED);
737
//		lab= getBindingLabel(ctor, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_FULLY_QUALIFIED);
738
//		assertLinkMatch(lab, "{{org.test.LambdaTests}}.{...}.{{org.test.LambdaTests.{...}.|Local}}.Local()");
738
//		assertLinkMatch(lab, "{{org.test.LambdaTests}}.{...}.{{org.test.LambdaTests.{...}.|Local}}.Local()");
Lines 768-771 Link Here
768
		lab= getBindingLabel(foo2, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_POST_QUALIFIED | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
768
		lab= getBindingLabel(foo2, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_POST_QUALIFIED | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
769
		assertLinkMatch(lab, "{{org.test.LambdaTests.{...}|Local}} toStringL - {{org}}.{{test}}.{{LambdaTests}}.{...}");
769
		assertLinkMatch(lab, "{{org.test.LambdaTests.{...}|Local}} toStringL - {{org}}.{{test}}.{{LambdaTests}}.{...}");
770
	}
770
	}
771
772
	public void testNonNullByDefault() throws Exception {
773
		setupForNullAnnotations("[1.1.0,2.0.0)"); // tests run at 1.5, need the "old" null annotations
774
		IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
775
		
776
		IPackageFragment pack1= sourceFolder.createPackageFragment("org.test", false, null);
777
		StringBuffer buf= new StringBuffer();
778
		buf.append("package org.test;\n");
779
		buf.append("import org.eclipse.jdt.annotation.*;\n");
780
		buf.append("\n");
781
		buf.append("@interface Ann {}\n");
782
		buf.append("@NonNullByDefault;\n");
783
		buf.append("public class Default {\n");
784
		buf.append("    public String test(@Nullable Integer t, int i, @Ann Object o) {\n");
785
		buf.append("        return \"\";\n");
786
		buf.append("    }\n");
787
		buf.append("    String s = test(1,2,new Object());\n");
788
		buf.append("}\n");
789
		String content= buf.toString();
790
		ICompilationUnit cu= pack1.createCompilationUnit("Default.java", content, false, null);
791
792
		IJavaElement test= cu.codeSelect(content.indexOf("test(1"), 4)[0];
793
		long flags1= JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_FULLY_QUALIFIED | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_NAMES;
794
		String lab= getBindingLabel(test, flags1);
795
		assertLinkMatch(lab, "{{java.lang|String}} {{org.test.Default}}.test({{java.lang|Integer}} t, int i, {{java.lang|Object}} o)");
796
797
		lab= getBindingLabel(test, flags1 | JavaElementLabels.M_PARAMETER_ANNOTATIONS);
798
		assertLinkMatch(lab, "@{{org.eclipse.jdt.annotation|NonNull}} {{java.lang|String}} {{org.test.Default}}.test("
799
				+ "@{{org.eclipse.jdt.annotation|Nullable}} {{java.lang|Integer}} t, int i, @{{org.test|Ann}} @{{org.eclipse.jdt.annotation|NonNull}} {{java.lang|Object}} o)");
800
801
	}
771
}
802
}
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/viewsupport/BindingLinkedLabelComposer.java (-3 / +27 lines)
Lines 20-27 Link Here
20
20
21
import org.eclipse.jdt.core.Flags;
21
import org.eclipse.jdt.core.Flags;
22
import org.eclipse.jdt.core.IJavaElement;
22
import org.eclipse.jdt.core.IJavaElement;
23
import org.eclipse.jdt.core.IJavaProject;
23
import org.eclipse.jdt.core.IMethod;
24
import org.eclipse.jdt.core.IMethod;
24
import org.eclipse.jdt.core.IType;
25
import org.eclipse.jdt.core.IType;
26
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaModelException;
27
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.dom.IAnnotationBinding;
28
import org.eclipse.jdt.core.dom.IAnnotationBinding;
27
import org.eclipse.jdt.core.dom.IBinding;
29
import org.eclipse.jdt.core.dom.IBinding;
Lines 101-107 Link Here
101
			case IBinding.ANNOTATION:
103
			case IBinding.ANNOTATION:
102
			case IBinding.MEMBER_VALUE_PAIR:
104
			case IBinding.MEMBER_VALUE_PAIR:
103
				// not used for hovers
105
				// not used for hovers
104
		}		
106
		}
105
	}
107
	}
106
108
107
	private void appendMethodBindingLabel(IMethodBinding method, long flags) {
109
	private void appendMethodBindingLabel(IMethodBinding method, long flags) {
Lines 140-145 Link Here
140
		
142
		
141
		// return type
143
		// return type
142
		if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && !method.isConstructor() && !isInitializer) {
144
		if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && !method.isConstructor() && !isInitializer) {
145
			if (getFlag(flags, JavaElementLabels.M_PARAMETER_ANNOTATIONS)) { // for consistency
146
				appendMethodDeclarationNullAnnotation(method.getAnnotations(), flags);
147
			}
143
			appendTypeBindingLabel(method.getReturnType(), flags);
148
			appendTypeBindingLabel(method.getReturnType(), flags);
144
			fBuffer.append(' ');
149
			fBuffer.append(' ');
145
		}
150
		}
Lines 297-302 Link Here
297
		}
302
		}
298
	}
303
	}
299
304
305
	private void appendMethodDeclarationNullAnnotation(IAnnotationBinding[] annotations, long flags) {
306
		IJavaProject javaProject= fEnclosingElement.getJavaProject();
307
		if (javaProject != null && JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
308
			String nonNullName= javaProject.getOption(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, true);
309
			String nullableName= javaProject.getOption(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, true);
310
			for (IAnnotationBinding annotation : annotations) {
311
				ITypeBinding annotationType= annotation.getAnnotationType();
312
				if (annotationType != null) {
313
					String annotationName= annotationType.getQualifiedName();
314
					if (annotationName.equals(nonNullName) || annotationName.equals(nullableName)) {
315
						appendAnnotationLabel(annotation, flags);
316
						fBuffer.append(' ');
317
						return;
318
					}
319
				}
320
			}
321
		}
322
	}
323
300
	private void appendVariableLabel(IVariableBinding variable, long flags) {
324
	private void appendVariableLabel(IVariableBinding variable, long flags) {
301
		long qualificationFlags = flags & (QUALIFIER_FLAGS | JavaElementLabels.ALL_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED);
325
		long qualificationFlags = flags & (QUALIFIER_FLAGS | JavaElementLabels.ALL_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED);
302
		if (fIsFromSource) {
326
		if (fIsFromSource) {
Lines 391-397 Link Here
391
			if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED))
415
			if (getFlag(flags, JavaElementLabels.T_FULLY_QUALIFIED))
392
				qualificationFlags |= JavaElementLabels.ALL_FULLY_QUALIFIED;
416
				qualificationFlags |= JavaElementLabels.ALL_FULLY_QUALIFIED;
393
			else if (getFlag(flags, JavaElementLabels.T_CONTAINER_QUALIFIED))
417
			else if (getFlag(flags, JavaElementLabels.T_CONTAINER_QUALIFIED))
394
				qualificationFlags |= (JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED); 
418
				qualificationFlags |= (JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED);
395
			appendBindingLabel(declaringMember, qualificationFlags);
419
			appendBindingLabel(declaringMember, qualificationFlags);
396
			fBuffer.append('.');
420
			fBuffer.append('.');
397
			flags &= ~(JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED); // qualification is done
421
			flags &= ~(JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED); // qualification is done
Lines 550-556 Link Here
550
				appendTypeBindingLabel((ITypeBinding) value, flags);
574
				appendTypeBindingLabel((ITypeBinding) value, flags);
551
				fBuffer.append(".class"); //$NON-NLS-1$
575
				fBuffer.append(".class"); //$NON-NLS-1$
552
			} else if (value instanceof String) {
576
			} else if (value instanceof String) {
553
				fBuffer.append(htmlEscape(ASTNodes.getEscapedStringLiteral((String) value))); 
577
				fBuffer.append(htmlEscape(ASTNodes.getEscapedStringLiteral((String) value)));
554
			} else if (value instanceof IVariableBinding) {
578
			} else if (value instanceof IVariableBinding) {
555
				appendVariableLabel((IVariableBinding) value, flags);
579
				appendVariableLabel((IVariableBinding) value, flags);
556
			} else if (value instanceof IAnnotationBinding) {
580
			} else if (value instanceof IAnnotationBinding) {

Return to bug 366007