001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.core;
011:
012: import org.eclipse.core.resources.IResourceStatus;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IPath;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.jdt.core.ClasspathContainerInitializer;
018: import org.eclipse.jdt.core.IClasspathContainer;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.IJavaModelStatus;
021: import org.eclipse.jdt.core.IJavaModelStatusConstants;
022: import org.eclipse.jdt.core.IJavaProject;
023: import org.eclipse.jdt.core.IPackageFragment;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.JavaModelException;
026: import org.eclipse.jdt.internal.core.util.Messages;
027:
028: /**
029: * @see IJavaModelStatus
030: */
031:
032: public class JavaModelStatus extends Status implements
033: IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus {
034:
035: /**
036: * The elements related to the failure, or <code>null</code>
037: * if no elements are involved.
038: */
039: protected IJavaElement[] elements = new IJavaElement[0];
040: /**
041: * The path related to the failure, or <code>null</code>
042: * if no path is involved.
043: */
044: protected IPath path;
045: /**
046: * The <code>String</code> related to the failure, or <code>null</code>
047: * if no <code>String</code> is involved.
048: */
049: protected String string;
050: /**
051: * Empty children
052: */
053: protected final static IStatus[] NO_CHILDREN = new IStatus[] {};
054: protected IStatus[] children = NO_CHILDREN;
055:
056: /**
057: * Singleton OK object
058: */
059: public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(
060: OK, OK, Messages.status_OK);
061:
062: /**
063: * Constructs an Java model status with no corresponding elements.
064: */
065: public JavaModelStatus() {
066: // no code for an multi-status
067: super (ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); //$NON-NLS-1$
068: }
069:
070: /**
071: * Constructs an Java model status with no corresponding elements.
072: */
073: public JavaModelStatus(int code) {
074: super (ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
075: this .elements = JavaElement.NO_ELEMENTS;
076: }
077:
078: /**
079: * Constructs an Java model status with the given corresponding
080: * elements.
081: */
082: public JavaModelStatus(int code, IJavaElement[] elements) {
083: super (ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
084: this .elements = elements;
085: this .path = null;
086: }
087:
088: /**
089: * Constructs an Java model status with no corresponding elements.
090: */
091: public JavaModelStatus(int code, String string) {
092: this (ERROR, code, string);
093: }
094:
095: /**
096: * Constructs an Java model status with no corresponding elements.
097: */
098: public JavaModelStatus(int severity, int code, String string) {
099: super (severity, JavaCore.PLUGIN_ID, code,
100: "JavaModelStatus", null); //$NON-NLS-1$
101: this .elements = JavaElement.NO_ELEMENTS;
102: this .path = null;
103: this .string = string;
104: }
105:
106: /**
107: * Constructs an Java model status with no corresponding elements.
108: */
109: public JavaModelStatus(int code, Throwable throwable) {
110: super (ERROR, JavaCore.PLUGIN_ID, code,
111: "JavaModelStatus", throwable); //$NON-NLS-1$
112: this .elements = JavaElement.NO_ELEMENTS;
113: }
114:
115: /**
116: * Constructs an Java model status with no corresponding elements.
117: */
118: public JavaModelStatus(int code, IPath path) {
119: super (ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); //$NON-NLS-1$
120: this .elements = JavaElement.NO_ELEMENTS;
121: this .path = path;
122: }
123:
124: /**
125: * Constructs an Java model status with the given corresponding
126: * element.
127: */
128: public JavaModelStatus(int code, IJavaElement element) {
129: this (code, new IJavaElement[] { element });
130: }
131:
132: /**
133: * Constructs an Java model status with the given corresponding
134: * element and string
135: */
136: public JavaModelStatus(int code, IJavaElement element, String string) {
137: this (code, new IJavaElement[] { element });
138: this .string = string;
139: }
140:
141: /**
142: * Constructs an Java model status with the given corresponding
143: * element and path
144: */
145: public JavaModelStatus(int code, IJavaElement element, IPath path) {
146: this (code, new IJavaElement[] { element });
147: this .path = path;
148: }
149:
150: /**
151: * Constructs an Java model status with the given corresponding
152: * element, path and string
153: */
154: public JavaModelStatus(int code, IJavaElement element, IPath path,
155: String string) {
156: this (code, new IJavaElement[] { element });
157: this .path = path;
158: this .string = string;
159: }
160:
161: /**
162: * Constructs an Java model status with the given corresponding
163: * element and path
164: */
165: public JavaModelStatus(int severity, int code,
166: IJavaElement element, IPath path, String msg) {
167: super (severity, JavaCore.PLUGIN_ID, code,
168: "JavaModelStatus", null); //$NON-NLS-1$
169: this .elements = new IJavaElement[] { element };
170: this .path = path;
171: this .string = msg;
172: }
173:
174: /**
175: * Constructs an Java model status with no corresponding elements.
176: */
177: public JavaModelStatus(CoreException coreException) {
178: super (ERROR, JavaCore.PLUGIN_ID, CORE_EXCEPTION,
179: "JavaModelStatus", coreException); //$NON-NLS-1$
180: elements = JavaElement.NO_ELEMENTS;
181: }
182:
183: protected int getBits() {
184: int severity = 1 << (getCode() % 100 / 33);
185: int category = 1 << ((getCode() / 100) + 3);
186: return severity | category;
187: }
188:
189: /**
190: * @see IStatus
191: */
192: public IStatus[] getChildren() {
193: return children;
194: }
195:
196: /**
197: * @see IJavaModelStatus
198: */
199: public IJavaElement[] getElements() {
200: return elements;
201: }
202:
203: /**
204: * Returns the message that is relevant to the code of this status.
205: */
206: public String getMessage() {
207: Throwable exception = getException();
208: if (exception == null) {
209: switch (getCode()) {
210: case CORE_EXCEPTION:
211: return Messages.status_coreException;
212:
213: case BUILDER_INITIALIZATION_ERROR:
214: return Messages.build_initializationError;
215:
216: case BUILDER_SERIALIZATION_ERROR:
217: return Messages.build_serializationError;
218:
219: case DEVICE_PATH:
220: return Messages.bind(
221: Messages.status_cannotUseDeviceOnPath,
222: getPath().toString());
223:
224: case DOM_EXCEPTION:
225: return Messages.status_JDOMError;
226:
227: case ELEMENT_DOES_NOT_EXIST:
228: return Messages.bind(Messages.element_doesNotExist,
229: ((JavaElement) elements[0])
230: .toStringWithAncestors());
231:
232: case ELEMENT_NOT_ON_CLASSPATH:
233: return Messages.bind(Messages.element_notOnClasspath,
234: ((JavaElement) elements[0])
235: .toStringWithAncestors());
236:
237: case EVALUATION_ERROR:
238: return Messages.bind(Messages.status_evaluationError,
239: string);
240:
241: case INDEX_OUT_OF_BOUNDS:
242: return Messages.status_indexOutOfBounds;
243:
244: case INVALID_CONTENTS:
245: return Messages.status_invalidContents;
246:
247: case INVALID_DESTINATION:
248: return Messages.bind(
249: Messages.status_invalidDestination,
250: ((JavaElement) elements[0])
251: .toStringWithAncestors());
252:
253: case INVALID_ELEMENT_TYPES:
254: StringBuffer buff = new StringBuffer(
255: Messages.operation_notSupported);
256: for (int i = 0; i < elements.length; i++) {
257: if (i > 0) {
258: buff.append(", "); //$NON-NLS-1$
259: }
260: buff.append(((JavaElement) elements[i])
261: .toStringWithAncestors());
262: }
263: return buff.toString();
264:
265: case INVALID_NAME:
266: return Messages.bind(Messages.status_invalidName,
267: string);
268:
269: case INVALID_PACKAGE:
270: return Messages.bind(Messages.status_invalidPackage,
271: string);
272:
273: case INVALID_PATH:
274: if (string != null) {
275: return string;
276: } else {
277: return Messages
278: .bind(
279: Messages.status_invalidPath,
280: new String[] { getPath() == null ? "null" : getPath().toString() } //$NON-NLS-1$
281: );
282: }
283:
284: case INVALID_PROJECT:
285: return Messages.bind(Messages.status_invalidProject,
286: string);
287:
288: case INVALID_RESOURCE:
289: return Messages.bind(Messages.status_invalidResource,
290: string);
291:
292: case INVALID_RESOURCE_TYPE:
293: return Messages.bind(
294: Messages.status_invalidResourceType, string);
295:
296: case INVALID_SIBLING:
297: if (string != null) {
298: return Messages.bind(
299: Messages.status_invalidSibling, string);
300: } else {
301: return Messages.bind(
302: Messages.status_invalidSibling,
303: ((JavaElement) elements[0])
304: .toStringWithAncestors());
305: }
306:
307: case IO_EXCEPTION:
308: return Messages.status_IOException;
309:
310: case NAME_COLLISION:
311: if (elements != null && elements.length > 0) {
312: IJavaElement element = elements[0];
313: if (element instanceof PackageFragment
314: && ((PackageFragment) element)
315: .isDefaultPackage()) {
316: return Messages.operation_cannotRenameDefaultPackage;
317: }
318: }
319: if (string != null) {
320: return string;
321: } else {
322: return Messages.bind(Messages.status_nameCollision,
323: ""); //$NON-NLS-1$
324: }
325: case NO_ELEMENTS_TO_PROCESS:
326: return Messages.operation_needElements;
327:
328: case NULL_NAME:
329: return Messages.operation_needName;
330:
331: case NULL_PATH:
332: return Messages.operation_needPath;
333:
334: case NULL_STRING:
335: return Messages.operation_needString;
336:
337: case PATH_OUTSIDE_PROJECT:
338: return Messages.bind(
339: Messages.operation_pathOutsideProject,
340: new String[] {
341: string,
342: ((JavaElement) elements[0])
343: .toStringWithAncestors() });
344:
345: case READ_ONLY:
346: IJavaElement element = elements[0];
347: String name = element.getElementName();
348: if (element instanceof IPackageFragment
349: && name
350: .equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) {
351: return Messages.status_defaultPackageReadOnly;
352: }
353: return Messages.bind(Messages.status_readOnly, name);
354:
355: case RELATIVE_PATH:
356: return Messages.bind(
357: Messages.operation_needAbsolutePath, getPath()
358: .toString());
359:
360: case TARGET_EXCEPTION:
361: return Messages.status_targetException;
362:
363: case UPDATE_CONFLICT:
364: return Messages.status_updateConflict;
365:
366: case NO_LOCAL_CONTENTS:
367: return Messages.bind(Messages.status_noLocalContents,
368: getPath().toString());
369:
370: case CP_CONTAINER_PATH_UNBOUND:
371: IJavaProject javaProject = (IJavaProject) elements[0];
372: ClasspathContainerInitializer initializer = JavaCore
373: .getClasspathContainerInitializer(this .path
374: .segment(0));
375: String description = null;
376: if (initializer != null)
377: description = initializer.getDescription(this .path,
378: javaProject);
379: if (description == null)
380: description = path.makeRelative().toString();
381: return Messages.bind(
382: Messages.classpath_unboundContainerPath,
383: new String[] { description,
384: javaProject.getElementName() });
385:
386: case INVALID_CP_CONTAINER_ENTRY:
387: javaProject = (IJavaProject) elements[0];
388: IClasspathContainer container = null;
389: description = null;
390: try {
391: container = JavaCore.getClasspathContainer(path,
392: javaProject);
393: } catch (JavaModelException e) {
394: // project doesn't exist: ignore
395: }
396: if (container == null) {
397: initializer = JavaCore
398: .getClasspathContainerInitializer(path
399: .segment(0));
400: if (initializer != null)
401: description = initializer.getDescription(path,
402: javaProject);
403: } else {
404: description = container.getDescription();
405: }
406: if (description == null)
407: description = path.makeRelative().toString();
408: return Messages.bind(
409: Messages.classpath_invalidContainer,
410: new String[] { description,
411: javaProject.getElementName() });
412:
413: case CP_VARIABLE_PATH_UNBOUND:
414: javaProject = (IJavaProject) elements[0];
415: return Messages.bind(
416: Messages.classpath_unboundVariablePath,
417: new String[] { path.makeRelative().toString(),
418: javaProject.getElementName() });
419:
420: case CLASSPATH_CYCLE:
421: javaProject = (IJavaProject) elements[0];
422: return Messages.bind(Messages.classpath_cycle,
423: javaProject.getElementName());
424:
425: case DISABLED_CP_EXCLUSION_PATTERNS:
426: javaProject = (IJavaProject) elements[0];
427: String projectName = javaProject.getElementName();
428: IPath newPath = path;
429: if (path.segment(0).toString().equals(projectName)) {
430: newPath = path.removeFirstSegments(1);
431: }
432: return Messages
433: .bind(
434: Messages.classpath_disabledInclusionExclusionPatterns,
435: new String[] {
436: newPath.makeRelative()
437: .toString(),
438: projectName });
439:
440: case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS:
441: javaProject = (IJavaProject) elements[0];
442: projectName = javaProject.getElementName();
443: newPath = path;
444: if (path.segment(0).toString().equals(projectName)) {
445: newPath = path.removeFirstSegments(1);
446: }
447: return Messages
448: .bind(
449: Messages.classpath_disabledMultipleOutputLocations,
450: new String[] {
451: newPath.makeRelative()
452: .toString(),
453: projectName });
454:
455: case INCOMPATIBLE_JDK_LEVEL:
456: javaProject = (IJavaProject) elements[0];
457: return Messages
458: .bind(
459: Messages.classpath_incompatibleLibraryJDKLevel,
460: new String[] {
461: javaProject.getElementName(),
462: javaProject
463: .getOption(
464: JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
465: true),
466: path.makeRelative().toString(),
467: string, });
468:
469: case CANNOT_RETRIEVE_ATTACHED_JAVADOC:
470: if (elements != null && elements.length == 1) {
471: if (this .string != null) {
472: return Messages
473: .bind(
474: Messages.status_cannot_retrieve_attached_javadoc,
475: ((JavaElement) elements[0])
476: .toStringWithAncestors(),
477: this .string);
478: }
479: return Messages
480: .bind(
481: Messages.status_cannot_retrieve_attached_javadoc,
482: ((JavaElement) elements[0])
483: .toStringWithAncestors(),
484: ""); //$NON-NLS-1$
485: }
486: if (this .string != null) {
487: return Messages
488: .bind(
489: Messages.status_cannot_retrieve_attached_javadoc,
490: this .string, "");//$NON-NLS-1$
491: }
492: break;
493:
494: case UNKNOWN_JAVADOC_FORMAT:
495: return Messages.bind(
496: Messages.status_unknown_javadoc_format,
497: ((JavaElement) elements[0])
498: .toStringWithAncestors());
499:
500: case DEPRECATED_VARIABLE:
501: javaProject = (IJavaProject) elements[0];
502: return Messages.bind(
503: Messages.classpath_deprecated_variable,
504: new String[] { path.segment(0).toString(),
505: javaProject.getElementName(),
506: this .string });
507: }
508: if (string != null) {
509: return string;
510: } else {
511: return ""; //$NON-NLS-1$
512: }
513: } else {
514: String message = exception.getMessage();
515: if (message != null) {
516: return message;
517: } else {
518: return exception.toString();
519: }
520: }
521: }
522:
523: /**
524: * @see IJavaModelStatus#getPath()
525: */
526: public IPath getPath() {
527: return path;
528: }
529:
530: /**
531: * @see IStatus#getSeverity()
532: */
533: public int getSeverity() {
534: if (children == NO_CHILDREN)
535: return super .getSeverity();
536: int severity = -1;
537: for (int i = 0, max = children.length; i < max; i++) {
538: int childrenSeverity = children[i].getSeverity();
539: if (childrenSeverity > severity) {
540: severity = childrenSeverity;
541: }
542: }
543: return severity;
544: }
545:
546: /**
547: * @see IJavaModelStatus#getString()
548: * @deprecated
549: */
550: public String getString() {
551: return string;
552: }
553:
554: /**
555: * @see IJavaModelStatus#isDoesNotExist()
556: */
557: public boolean isDoesNotExist() {
558: int code = getCode();
559: return code == ELEMENT_DOES_NOT_EXIST
560: || code == ELEMENT_NOT_ON_CLASSPATH;
561: }
562:
563: /**
564: * @see IStatus#isMultiStatus()
565: */
566: public boolean isMultiStatus() {
567: return children != NO_CHILDREN;
568: }
569:
570: /**
571: * @see IStatus#isOK()
572: */
573: public boolean isOK() {
574: return getCode() == OK;
575: }
576:
577: /**
578: * @see IStatus#matches(int)
579: */
580: public boolean matches(int mask) {
581: if (!isMultiStatus()) {
582: return matches(this , mask);
583: } else {
584: for (int i = 0, max = children.length; i < max; i++) {
585: if (matches((JavaModelStatus) children[i], mask))
586: return true;
587: }
588: return false;
589: }
590: }
591:
592: /**
593: * Helper for matches(int).
594: */
595: protected boolean matches(JavaModelStatus status, int mask) {
596: int severityMask = mask & 0x7;
597: int categoryMask = mask & ~0x7;
598: int bits = status.getBits();
599: return ((severityMask == 0) || (bits & severityMask) != 0)
600: && ((categoryMask == 0) || (bits & categoryMask) != 0);
601: }
602:
603: /**
604: * Creates and returns a new <code>IJavaModelStatus</code> that is a
605: * a multi-status status.
606: *
607: * @see IStatus#isMultiStatus()
608: */
609: public static IJavaModelStatus newMultiStatus(
610: IJavaModelStatus[] children) {
611: JavaModelStatus jms = new JavaModelStatus();
612: jms.children = children;
613: return jms;
614: }
615:
616: /**
617: * Returns a printable representation of this exception for debugging
618: * purposes.
619: */
620: public String toString() {
621: if (this == VERIFIED_OK) {
622: return "JavaModelStatus[OK]"; //$NON-NLS-1$
623: }
624: StringBuffer buffer = new StringBuffer();
625: buffer.append("Java Model Status ["); //$NON-NLS-1$
626: buffer.append(getMessage());
627: buffer.append("]"); //$NON-NLS-1$
628: return buffer.toString();
629: }
630: }
|