Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 397464
Collapse All | Expand All

(-)ui/org/eclipse/jdt/internal/ui/text/javadoc/JavadocContentAccess2.java (-72 / +63 lines)
Lines 30-35 Link Here
30
import java.util.zip.ZipEntry;
30
import java.util.zip.ZipEntry;
31
import java.util.zip.ZipFile;
31
import java.util.zip.ZipFile;
32
32
33
import org.eclipse.core.runtime.Assert;
33
import org.eclipse.core.runtime.CoreException;
34
import org.eclipse.core.runtime.CoreException;
34
import org.eclipse.core.runtime.IPath;
35
import org.eclipse.core.runtime.IPath;
35
import org.eclipse.core.runtime.IStatus;
36
import org.eclipse.core.runtime.IStatus;
Lines 73-79 Link Here
73
import org.eclipse.jdt.core.dom.ASTNode;
74
import org.eclipse.jdt.core.dom.ASTNode;
74
import org.eclipse.jdt.core.dom.ASTParser;
75
import org.eclipse.jdt.core.dom.ASTParser;
75
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
76
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
76
import org.eclipse.jdt.core.dom.Comment;
77
import org.eclipse.jdt.core.dom.CompilationUnit;
77
import org.eclipse.jdt.core.dom.CompilationUnit;
78
import org.eclipse.jdt.core.dom.IBinding;
78
import org.eclipse.jdt.core.dom.IBinding;
79
import org.eclipse.jdt.core.dom.IVariableBinding;
79
import org.eclipse.jdt.core.dom.IVariableBinding;
Lines 83-88 Link Here
83
import org.eclipse.jdt.core.dom.MethodRefParameter;
83
import org.eclipse.jdt.core.dom.MethodRefParameter;
84
import org.eclipse.jdt.core.dom.Name;
84
import org.eclipse.jdt.core.dom.Name;
85
import org.eclipse.jdt.core.dom.NodeFinder;
85
import org.eclipse.jdt.core.dom.NodeFinder;
86
import org.eclipse.jdt.core.dom.PackageDeclaration;
86
import org.eclipse.jdt.core.dom.SimpleName;
87
import org.eclipse.jdt.core.dom.SimpleName;
87
import org.eclipse.jdt.core.dom.TagElement;
88
import org.eclipse.jdt.core.dom.TagElement;
88
import org.eclipse.jdt.core.dom.TextElement;
89
import org.eclipse.jdt.core.dom.TextElement;
Lines 439-447 Link Here
439
		}
440
		}
440
	}
441
	}
441
442
442
	private final IMember fMember;
443
	private final IJavaElement fElement;
444
	
443
	/**
445
	/**
444
	 * The method, or <code>null</code> if {@link #fMember} is not a method where @inheritDoc could work.
446
	 * The method, or <code>null</code> if {@link #fElement} is not a method where @inheritDoc could
447
	 * work.
445
	 */
448
	 */
446
	private final IMethod fMethod;
449
	private final IMethod fMethod;
447
	private final Javadoc fJavadoc;
450
	private final Javadoc fJavadoc;
Lines 456-470 Link Here
456
	private HashMap<String, StringBuffer> fExceptionDescriptions;
459
	private HashMap<String, StringBuffer> fExceptionDescriptions;
457
460
458
	private JavadocContentAccess2(IMethod method, Javadoc javadoc, String source, JavadocLookup lookup) {
461
	private JavadocContentAccess2(IMethod method, Javadoc javadoc, String source, JavadocLookup lookup) {
459
		fMember= method;
462
		Assert.isNotNull(method);
463
		fElement= method;
460
		fMethod= method;
464
		fMethod= method;
461
		fJavadoc= javadoc;
465
		fJavadoc= javadoc;
462
		fSource= source;
466
		fSource= source;
463
		fJavadocLookup= lookup;
467
		fJavadocLookup= lookup;
464
	}
468
	}
465
469
466
	private JavadocContentAccess2(IMember member, Javadoc javadoc, String source) {
470
	private JavadocContentAccess2(IJavaElement element, Javadoc javadoc, String source) {
467
		fMember= member;
471
		Assert.isNotNull(element);
472
		fElement= element;
468
		fMethod= null;
473
		fMethod= null;
469
		fJavadoc= javadoc;
474
		fJavadoc= javadoc;
470
		fSource= source;
475
		fSource= source;
Lines 609-615 Link Here
609
		//Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
614
		//Caveat: Javadoc nodes are not available when Javadoc processing has been disabled!
610
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
615
		//https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
611
616
612
		CompilationUnit root= createAST(element, rawJavadoc);
617
		CompilationUnit root= createCompilationUnit(element, rawJavadoc);
613
		if (root == null)
618
		if (root == null)
614
			return null;
619
			return null;
615
		List<AbstractTypeDeclaration> types= root.types();
620
		List<AbstractTypeDeclaration> types= root.types();
Lines 619-624 Link Here
619
		return type.getJavadoc();
624
		return type.getJavadoc();
620
	}
625
	}
621
626
627
	private static Javadoc getPackageJavadocNode(IJavaElement element, String rawJavadoc) {
628
		CompilationUnit cu= createCompilationUnit(element, rawJavadoc);
629
		if (cu != null) {
630
			PackageDeclaration packDeclr= cu.getPackage();
631
			if (packDeclr != null) {
632
				return packDeclr.getJavadoc();
633
			}
634
		}
635
		return null;
636
	}
622
637
623
	private static String javadoc2HTML(IMember member, String rawJavadoc) {
638
	private static String javadoc2HTML(IMember member, String rawJavadoc) {
624
		Javadoc javadoc= getJavadocNode(member, rawJavadoc);
639
		Javadoc javadoc= getJavadocNode(member, rawJavadoc);
Lines 1066-1072 Link Here
1066
					// should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
1081
					// should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
1067
					Exception exception= new Exception("Illegal ASTNode positions: previousEnd=" + previousEnd //$NON-NLS-1$
1082
					Exception exception= new Exception("Illegal ASTNode positions: previousEnd=" + previousEnd //$NON-NLS-1$
1068
							+ ", childStart=" + childStart //$NON-NLS-1$
1083
							+ ", childStart=" + childStart //$NON-NLS-1$
1069
							+ ", member=" + fMember.getHandleIdentifier() //$NON-NLS-1$
1084
							+ ", element=" + fElement.getHandleIdentifier() //$NON-NLS-1$
1070
							+ ", Javadoc:\n" + fSource); //$NON-NLS-1$
1085
							+ ", Javadoc:\n" + fSource); //$NON-NLS-1$
1071
					JavaPlugin.log(exception);
1086
					JavaPlugin.log(exception);
1072
				} else if (previousEnd != childStart) {
1087
				} else if (previousEnd != childStart) {
Lines 1183-1191 Link Here
1183
		
1198
		
1184
		List<? extends ASTNode> fragments= node.fragments();
1199
		List<? extends ASTNode> fragments= node.fragments();
1185
		try {
1200
		try {
1201
			if (!(fElement instanceof IMember)) {
1202
				return false;
1203
			}
1186
			if (fragments.isEmpty()) {
1204
			if (fragments.isEmpty()) {
1187
				if (fMember instanceof IField && JdtFlags.isStatic(fMember) && JdtFlags.isFinal(fMember)) {
1205
				if (fElement instanceof IField && JdtFlags.isStatic((IField) fElement) && JdtFlags.isFinal((IField) fElement)) {
1188
					IField field= (IField) fMember;
1206
					IField field= (IField) fElement;
1189
					return handleConstantValue(field, false);
1207
					return handleConstantValue(field, false);
1190
				}
1208
				}
1191
			} else if (fragments.size() == 1) {
1209
			} else if (fragments.size() == 1) {
Lines 1194-1200 Link Here
1194
					MemberRef memberRef= (MemberRef) first;
1212
					MemberRef memberRef= (MemberRef) first;
1195
					if (memberRef.getQualifier() == null) {
1213
					if (memberRef.getQualifier() == null) {
1196
						SimpleName name= memberRef.getName();
1214
						SimpleName name= memberRef.getName();
1197
						IType type= fMember instanceof IType ? (IType)fMember : fMember.getDeclaringType();
1215
						IType type= fElement instanceof IType ? (IType) fElement : ((IMember) fElement).getDeclaringType();
1198
						while (type != null) {
1216
						while (type != null) {
1199
							IField field= type.getField(name.getIdentifier());
1217
							IField field= type.getField(name.getIdentifier());
1200
							if (field != null && field.exists()) {
1218
							if (field != null && field.exists()) {
Lines 1271-1283 Link Here
1271
1289
1272
		try {
1290
		try {
1273
			String url= null;
1291
			String url= null;
1274
			if (fMember.isBinary()) {
1292
			if (fElement instanceof IMember && ((IMember) fElement).isBinary()) {
1275
				URL javadocBaseLocation= JavaUI.getJavadocBaseLocation(fMember);
1293
				URL javadocBaseLocation= JavaUI.getJavadocBaseLocation(fElement);
1276
				if (javadocBaseLocation != null) {
1294
				if (javadocBaseLocation != null) {
1277
					url= javadocBaseLocation.toExternalForm();
1295
					url= javadocBaseLocation.toExternalForm();
1278
				}
1296
				}
1279
			} else {
1297
			} else {
1280
				IPackageFragmentRoot srcRoot= JavaModelUtil.getPackageFragmentRoot(fMember);
1298
				IPackageFragmentRoot srcRoot= JavaModelUtil.getPackageFragmentRoot(fElement);
1281
				if (srcRoot != null) {
1299
				if (srcRoot != null) {
1282
					IResource resource= srcRoot.getResource();
1300
					IResource resource= srcRoot.getResource();
1283
					if (resource != null) {
1301
					if (resource != null) {
Lines 1569-1575 Link Here
1569
				fBuf.append("<a href='"); //$NON-NLS-1$
1587
				fBuf.append("<a href='"); //$NON-NLS-1$
1570
				try {
1588
				try {
1571
					String scheme= JavaElementLinks.JAVADOC_SCHEME;
1589
					String scheme= JavaElementLinks.JAVADOC_SCHEME;
1572
					String uri= JavaElementLinks.createURI(scheme, fMember, refTypeName, refMemberName, refMethodParamTypes);
1590
					String uri= JavaElementLinks.createURI(scheme, fElement, refTypeName, refMemberName, refMethodParamTypes);
1573
					fBuf.append(uri);
1591
					fBuf.append(uri);
1574
				} catch (URISyntaxException e) {
1592
				} catch (URISyntaxException e) {
1575
					JavaPlugin.log(e);
1593
					JavaPlugin.log(e);
Lines 1669-1678 Link Here
1669
			String source= typeRoot.getSource();
1687
			String source= typeRoot.getSource();
1670
			//the source can be null for some of the class files
1688
			//the source can be null for some of the class files
1671
			if (source != null) {
1689
			if (source != null) {
1672
				CompilationUnit ast= createAST(typeRoot);
1690
				Javadoc packageJavadocNode= getPackageJavadocNode(packageFragment, source);
1673
				String javadoc= getJavadocFromAST(ast, source);
1691
				if (packageJavadocNode != null) {
1674
				if (javadoc != null)
1692
					IMember member= null;
1675
					return javadoc;
1693
					if (typeRoot instanceof ICompilationUnit && typeRoot.exists()) {
1694
						member= ((ICompilationUnit) typeRoot).getType(typeRoot.getElementName());
1695
					} else if (typeRoot instanceof IClassFile && typeRoot.exists()) {
1696
						member= ((IClassFile) typeRoot).getType();
1697
					}
1698
					return new JavadocContentAccess2(member, packageJavadocNode, source).toHTML();
1699
				}
1676
			}
1700
			}
1677
		}
1701
		}
1678
1702
Lines 1717-1736 Link Here
1717
		return null;
1741
		return null;
1718
	}
1742
	}
1719
1743
1720
	private static String getJavadocFromAST(CompilationUnit ast, String source) {
1721
		List<Comment> commentList= ast.getCommentList();
1722
		if (commentList != null && !commentList.isEmpty()) {
1723
			for (int i= commentList.size() - 1; i >= 0; i--) {
1724
				Comment comment= commentList.get(i);
1725
				if (comment instanceof Javadoc) {
1726
					JavadocContentAccess2 docacc= new JavadocContentAccess2(null, (Javadoc) comment, source);
1727
					return docacc.toHTML();
1728
				}
1729
			}
1730
		}
1731
		return null;
1732
	}
1733
1734
	private static String getHTMLContent(IJarEntryResource jarEntryResource, String encoding) throws CoreException {
1744
	private static String getHTMLContent(IJarEntryResource jarEntryResource, String encoding) throws CoreException {
1735
		InputStream in= jarEntryResource.getContents();
1745
		InputStream in= jarEntryResource.getContents();
1736
		try {
1746
		try {
Lines 1750-1761 Link Here
1750
		String filePath= packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_INFO_JAVA;
1760
		String filePath= packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_INFO_JAVA;
1751
		String contents= getHTMLContentFromAttachedSource(root, filePath);
1761
		String contents= getHTMLContentFromAttachedSource(root, filePath);
1752
		if (contents != null) {
1762
		if (contents != null) {
1753
			ASTParser parser= createASTParser(packageFragment);
1763
			ICompilationUnit cu= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
1754
			parser.setSource(contents.toCharArray());
1764
			IType type= cu.getType(cu.getElementName());
1755
			CompilationUnit ast= (CompilationUnit) parser.createAST(null);
1765
			Javadoc packageJavadocNode= getPackageJavadocNode(type, contents);
1756
			String javadoc= getJavadocFromAST(ast, contents);
1766
			if (packageJavadocNode != null)
1757
			if (javadoc != null)
1767
				return new JavadocContentAccess2(type, packageJavadocNode, contents).toHTML();
1758
				return javadoc;
1768
1759
		}
1769
		}
1760
		filePath= packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_HTML;
1770
		filePath= packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_HTML;
1761
		return getHTMLContentFromAttachedSource(root, filePath);
1771
		return getHTMLContentFromAttachedSource(root, filePath);
Lines 1765-1770 Link Here
1765
		IPath sourceAttachmentPath= root.getSourceAttachmentPath();
1775
		IPath sourceAttachmentPath= root.getSourceAttachmentPath();
1766
		if (sourceAttachmentPath != null) {
1776
		if (sourceAttachmentPath != null) {
1767
			File file= sourceAttachmentPath.toFile();
1777
			File file= sourceAttachmentPath.toFile();
1778
1779
			if (!file.exists()) {
1780
				//the path could be a workspace relative path to a jar or to the source folder
1781
				IWorkspaceRoot wsRoot= ResourcesPlugin.getWorkspace().getRoot();
1782
				IResource res= wsRoot.findMember(sourceAttachmentPath.append(filePath));
1783
				if (res == null)
1784
					res= wsRoot.findMember(sourceAttachmentPath);
1785
1786
				if (res != null)
1787
					file= res.getLocation().toFile();
1788
1789
			}
1768
			if (file.isDirectory()) {
1790
			if (file.isDirectory()) {
1769
				//the path could be an absolute path to the source folder
1791
				//the path could be an absolute path to the source folder
1770
				IPath packagedocPath= sourceAttachmentPath.append(filePath);
1792
				IPath packagedocPath= sourceAttachmentPath.append(filePath);
Lines 1772-1788 Link Here
1772
					return getFileContent(packagedocPath.toFile());
1794
					return getFileContent(packagedocPath.toFile());
1773
1795
1774
			} else {
1796
			} else {
1775
				if (!file.exists()) {
1776
					//the path could be a workspace relative path to a jar or to the source folder
1777
					IWorkspaceRoot wsRoot= ResourcesPlugin.getWorkspace().getRoot();
1778
					IResource res= wsRoot.findMember(sourceAttachmentPath.append(filePath));
1779
					if (res == null)
1780
						res= wsRoot.findMember(sourceAttachmentPath);
1781
1782
					if (res != null)
1783
						file= res.getLocation().toFile();
1784
1785
				}
1786
				if (file.exists()) {
1797
				if (file.exists()) {
1787
					if (JavaModelUtil.PACKAGE_HTML.equals(file.getName()))
1798
					if (JavaModelUtil.PACKAGE_HTML.equals(file.getName()))
1788
						return getFileContent(file);
1799
						return getFileContent(file);
Lines 1883-1910 Link Here
1883
		return encoding;
1894
		return encoding;
1884
	}
1895
	}
1885
1896
1886
	/**
1897
	private static CompilationUnit createCompilationUnit(IJavaElement element, String rawJavadoc) {
1887
	 * Reads a compilation unit and creates the AST DOM for manipulating the Java source.
1888
	 * 
1889
	 * @param typeRoot cane be a compilation unit or a class file
1890
	 * @return parsed compilation unit
1891
	 */
1892
1893
	private static CompilationUnit createAST(ITypeRoot typeRoot) {
1894
		ASTParser parser= createASTParser(typeRoot);
1895
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
1896
		if (typeRoot instanceof ICompilationUnit) {
1897
			parser.setSource((ICompilationUnit) typeRoot);
1898
		} else if (typeRoot instanceof IClassFile) {
1899
			parser.setSource((IClassFile) typeRoot);
1900
		}
1901
		parser.setResolveBindings(true);
1902
		return (CompilationUnit) parser.createAST(null);
1903
1904
	}
1905
1906
1907
	private static CompilationUnit createAST(IJavaElement element, String rawJavadoc) {
1908
		ASTParser parser= createASTParser(element);
1898
		ASTParser parser= createASTParser(element);
1909
		String source= rawJavadoc + "class C{}"; //$NON-NLS-1$
1899
		String source= rawJavadoc + "class C{}"; //$NON-NLS-1$
1910
		parser.setSource(source.toCharArray());
1900
		parser.setSource(source.toCharArray());
Lines 1912-1917 Link Here
1912
	}
1902
	}
1913
1903
1914
	private static ASTParser createASTParser(IJavaElement element) {
1904
	private static ASTParser createASTParser(IJavaElement element) {
1905
		Assert.isNotNull(element);
1915
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
1906
		ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
1916
		IJavaProject javaProject= element.getJavaProject();
1907
		IJavaProject javaProject= element.getJavaProject();
1917
		parser.setProject(javaProject);
1908
		parser.setProject(javaProject);
(-)ui/org/eclipse/jdt/internal/ui/viewsupport/JavaElementLinks.java (-5 / +57 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2008, 2012 IBM Corporation and others.
2
 * Copyright (c) 2008, 2013 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 22-31 Link Here
22
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Display;
23
23
24
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.core.runtime.NullProgressMonitor;
25
26
26
import org.eclipse.jdt.core.IAnnotation;
27
import org.eclipse.jdt.core.IAnnotation;
28
import org.eclipse.jdt.core.ICompilationUnit;
27
import org.eclipse.jdt.core.IField;
29
import org.eclipse.jdt.core.IField;
30
import org.eclipse.jdt.core.IImportDeclaration;
28
import org.eclipse.jdt.core.IJavaElement;
31
import org.eclipse.jdt.core.IJavaElement;
32
import org.eclipse.jdt.core.IJavaProject;
29
import org.eclipse.jdt.core.ILocalVariable;
33
import org.eclipse.jdt.core.ILocalVariable;
30
import org.eclipse.jdt.core.IMember;
34
import org.eclipse.jdt.core.IMember;
31
import org.eclipse.jdt.core.IMethod;
35
import org.eclipse.jdt.core.IMethod;
Lines 325-338 Link Here
325
329
326
		if (segments.length > 2) {
330
		if (segments.length > 2) {
327
			String refTypeName= segments[2];
331
			String refTypeName= segments[2];
328
			if (refTypeName.indexOf('.') == -1) {
332
			try {
329
				try {
333
				if (!element.exists() && JavaModelUtil.PACKAGE_INFO_JAVA.equals(element.getElementName())) {
334
					return resolvePackageInfo(element, refTypeName);
335
				} else if (refTypeName.indexOf('.') == -1) {
330
					IJavaElement resolvedTypeVariable= resolveTypeVariable(element, refTypeName);
336
					IJavaElement resolvedTypeVariable= resolveTypeVariable(element, refTypeName);
331
					if (resolvedTypeVariable != null)
337
					if (resolvedTypeVariable != null)
332
						return resolvedTypeVariable;
338
						return resolvedTypeVariable;
333
				} catch (JavaModelException e) {
334
					JavaPlugin.log(e);
335
				}
339
				}
340
			} catch (JavaModelException e) {
341
				JavaPlugin.log(e);
336
			}
342
			}
337
			if (element instanceof IAnnotation) {
343
			if (element instanceof IAnnotation) {
338
				element= element.getParent();
344
				element= element.getParent();
Lines 411-416 Link Here
411
		return element;
417
		return element;
412
	}
418
	}
413
419
420
	private static IType resolvePackageInfo(IJavaElement element, String refLinkName) throws JavaModelException {
421
		IType type= null;
422
		IJavaProject javaProject= element.getJavaProject();
423
424
		//check if the reference link is a qualified name
425
		if (refLinkName.indexOf('.') != -1) {
426
			return javaProject.findType(refLinkName, new NullProgressMonitor());
427
		}
428
429
		ICompilationUnit cu= null;
430
		//check if the reference link is in enclosing package
431
		if (element instanceof IType) {
432
			IType typeElement= (IType) element;
433
			cu= typeElement.getCompilationUnit();
434
			type= javaProject.findType(typeElement.getPackageFragment().getElementName() + '.' + refLinkName, new NullProgressMonitor());
435
			if (type != null) {
436
				return type;
437
			}
438
		}
439
440
		//check whether the reference link is part of on-demand import or type import
441
		if (cu != null) {
442
			IImportDeclaration[] imports= cu.getImports();
443
			for (IImportDeclaration iImportDeclaration : imports) {
444
				String name= iImportDeclaration.getElementName();
445
				if (iImportDeclaration.isOnDemand()) {
446
					//remove the * from the element name
447
					name= name.substring(0, name.length() - 1);
448
					type= javaProject.findType(name + refLinkName, new NullProgressMonitor());
449
				} else {
450
					if (name.endsWith('.' + refLinkName)) {
451
						type= javaProject.findType(name, new NullProgressMonitor());
452
					}
453
				}
454
				if (type != null) {
455
					return type;
456
				}
457
			}
458
		}
459
460
		//check whether the reference link is from java.lang.
461
		return javaProject.findType("java.lang." + refLinkName, new NullProgressMonitor()); //$NON-NLS-1$
462
	}
463
464
465
414
	private static IJavaElement resolveTypeVariable(IJavaElement baseElement, String typeVariableName) throws JavaModelException {
466
	private static IJavaElement resolveTypeVariable(IJavaElement baseElement, String typeVariableName) throws JavaModelException {
415
		while (baseElement != null) {
467
		while (baseElement != null) {
416
			switch (baseElement.getElementType()) {
468
			switch (baseElement.getElementType()) {

Return to bug 397464