001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.codeassist.select;
011:
012: import org.eclipse.jdt.internal.compiler.ast.*;
013: import org.eclipse.jdt.internal.compiler.lookup.*;
014:
015: /**
016: * Node representing a Javadoc comment including code selection.
017: */
018: public class SelectionJavadoc extends Javadoc {
019:
020: Expression selectedNode;
021:
022: public SelectionJavadoc(int sourceStart, int sourceEnd) {
023: super (sourceStart, sourceEnd);
024: }
025:
026: /* (non-Javadoc)
027: * @see org.eclipse.jdt.internal.compiler.ast.Javadoc#print(int, java.lang.StringBuffer)
028: */
029: public StringBuffer print(int indent, StringBuffer output) {
030: super .print(indent, output);
031: if (this .selectedNode != null) {
032: String selectedString = null;
033: if (this .selectedNode instanceof JavadocFieldReference) {
034: JavadocFieldReference fieldRef = (JavadocFieldReference) this .selectedNode;
035: if (fieldRef.methodBinding != null) {
036: selectedString = "<SelectOnMethod:"; //$NON-NLS-1$
037: } else {
038: selectedString = "<SelectOnField:"; //$NON-NLS-1$
039: }
040: } else if (this .selectedNode instanceof JavadocMessageSend) {
041: selectedString = "<SelectOnMethod:"; //$NON-NLS-1$
042: } else if (this .selectedNode instanceof JavadocAllocationExpression) {
043: selectedString = "<SelectOnConstructor:"; //$NON-NLS-1$
044: } else if (this .selectedNode instanceof JavadocSingleNameReference) {
045: selectedString = "<SelectOnLocalVariable:"; //$NON-NLS-1$
046: } else if (this .selectedNode instanceof JavadocSingleTypeReference) {
047: JavadocSingleTypeReference typeRef = (JavadocSingleTypeReference) this .selectedNode;
048: if (typeRef.packageBinding == null) {
049: selectedString = "<SelectOnType:"; //$NON-NLS-1$
050: }
051: } else if (this .selectedNode instanceof JavadocQualifiedTypeReference) {
052: JavadocQualifiedTypeReference typeRef = (JavadocQualifiedTypeReference) this .selectedNode;
053: if (typeRef.packageBinding == null) {
054: selectedString = "<SelectOnType:"; //$NON-NLS-1$
055: }
056: } else {
057: selectedString = "<SelectOnType:"; //$NON-NLS-1$
058: }
059: int pos = output.length() - 3;
060: output.replace(pos - 2, pos, selectedString + selectedNode
061: + '>');
062: }
063: return output;
064: }
065:
066: /**
067: * Resolve selected node if not null and throw exception to let clients know
068: * that it has been found.
069: *
070: * @throws SelectionNodeFound
071: */
072: private void internalResolve(Scope scope) {
073: if (this .selectedNode != null) {
074: switch (scope.kind) {
075: case Scope.CLASS_SCOPE:
076: this .selectedNode.resolveType((ClassScope) scope);
077: break;
078: case Scope.METHOD_SCOPE:
079: this .selectedNode.resolveType((MethodScope) scope);
080: break;
081: }
082: Binding binding = null;
083: if (this .selectedNode instanceof JavadocFieldReference) {
084: JavadocFieldReference fieldRef = (JavadocFieldReference) this .selectedNode;
085: binding = fieldRef.binding;
086: if (binding == null && fieldRef.methodBinding != null) {
087: binding = fieldRef.methodBinding;
088: }
089: } else if (this .selectedNode instanceof JavadocMessageSend) {
090: binding = ((JavadocMessageSend) this .selectedNode).binding;
091: } else if (this .selectedNode instanceof JavadocAllocationExpression) {
092: binding = ((JavadocAllocationExpression) this .selectedNode).binding;
093: } else if (this .selectedNode instanceof JavadocSingleNameReference) {
094: binding = ((JavadocSingleNameReference) this .selectedNode).binding;
095: } else if (this .selectedNode instanceof JavadocSingleTypeReference) {
096: JavadocSingleTypeReference typeRef = (JavadocSingleTypeReference) this .selectedNode;
097: if (typeRef.packageBinding == null) {
098: binding = typeRef.resolvedType;
099: }
100: } else if (this .selectedNode instanceof JavadocQualifiedTypeReference) {
101: JavadocQualifiedTypeReference typeRef = (JavadocQualifiedTypeReference) this .selectedNode;
102: if (typeRef.packageBinding == null) {
103: binding = typeRef.resolvedType;
104: }
105: } else {
106: binding = this .selectedNode.resolvedType;
107: }
108: throw new SelectionNodeFound(binding);
109: }
110: }
111:
112: /**
113: * Resolve selected node if not null and throw exception to let clients know
114: * that it has been found.
115: *
116: * @throws SelectionNodeFound
117: */
118: public void resolve(ClassScope scope) {
119: internalResolve(scope);
120: }
121:
122: /**
123: * Resolve selected node if not null and throw exception to let clients know
124: * that it has been found.
125: *
126: * @throws SelectionNodeFound
127: */
128: public void resolve(MethodScope scope) {
129: internalResolve(scope);
130: }
131:
132: }
|