1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.resources.IResourceStatus; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jdt.core.ClasspathContainerInitializer; 19 import org.eclipse.jdt.core.IClasspathContainer; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaModelStatus; 22 import org.eclipse.jdt.core.IJavaModelStatusConstants; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.IPackageFragment; 25 import org.eclipse.jdt.core.JavaCore; 26 import org.eclipse.jdt.core.JavaModelException; 27 import org.eclipse.jdt.internal.core.util.Messages; 28 29 32 33 public class JavaModelStatus extends Status implements IJavaModelStatus, IJavaModelStatusConstants, IResourceStatus { 34 35 39 protected IJavaElement[] elements = new IJavaElement[0]; 40 44 protected IPath path; 45 49 protected String string; 50 53 protected final static IStatus[] NO_CHILDREN = new IStatus[] {}; 54 protected IStatus[] children= NO_CHILDREN; 55 56 59 public static final IJavaModelStatus VERIFIED_OK = new JavaModelStatus(OK, OK, Messages.status_OK); 60 61 64 public JavaModelStatus() { 65 super(ERROR, JavaCore.PLUGIN_ID, 0, "JavaModelStatus", null); } 68 71 public JavaModelStatus(int code) { 72 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); this.elements= JavaElement.NO_ELEMENTS; 74 } 75 79 public JavaModelStatus(int code, IJavaElement[] elements) { 80 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); this.elements= elements; 82 this.path= null; 83 } 84 87 public JavaModelStatus(int code, String string) { 88 this(ERROR, code, string); 89 } 90 93 public JavaModelStatus(int severity, int code, String string) { 94 super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); this.elements= JavaElement.NO_ELEMENTS; 96 this.path= null; 97 this.string = string; 98 } 99 102 public JavaModelStatus(int code, Throwable throwable) { 103 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", throwable); this.elements= JavaElement.NO_ELEMENTS; 105 } 106 109 public JavaModelStatus(int code, IPath path) { 110 super(ERROR, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); this.elements= JavaElement.NO_ELEMENTS; 112 this.path= path; 113 } 114 118 public JavaModelStatus(int code, IJavaElement element) { 119 this(code, new IJavaElement[]{element}); 120 } 121 125 public JavaModelStatus(int code, IJavaElement element, String string) { 126 this(code, new IJavaElement[]{element}); 127 this.string = string; 128 } 129 130 134 public JavaModelStatus(int code, IJavaElement element, IPath path) { 135 this(code, new IJavaElement[]{element}); 136 this.path = path; 137 } 138 139 143 public JavaModelStatus(int code, IJavaElement element, IPath path, String string) { 144 this(code, new IJavaElement[]{element}); 145 this.path = path; 146 this.string = string; 147 } 148 149 153 public JavaModelStatus(int severity, int code, IJavaElement element, IPath path, String msg) { 154 super(severity, JavaCore.PLUGIN_ID, code, "JavaModelStatus", null); this.elements= new IJavaElement[]{element}; 156 this.path = path; 157 this.string = msg; 158 } 159 160 163 public JavaModelStatus(CoreException coreException) { 164 super(ERROR, JavaCore.PLUGIN_ID, CORE_EXCEPTION, "JavaModelStatus", coreException); elements= JavaElement.NO_ELEMENTS; 166 } 167 protected int getBits() { 168 int severity = 1 << (getCode() % 100 / 33); 169 int category = 1 << ((getCode() / 100) + 3); 170 return severity | category; 171 } 172 175 public IStatus[] getChildren() { 176 return children; 177 } 178 181 public IJavaElement[] getElements() { 182 return elements; 183 } 184 187 public String getMessage() { 188 Throwable exception = getException(); 189 if (exception == null) { 190 switch (getCode()) { 191 case CORE_EXCEPTION : 192 return Messages.status_coreException; 193 194 case BUILDER_INITIALIZATION_ERROR: 195 return Messages.build_initializationError; 196 197 case BUILDER_SERIALIZATION_ERROR: 198 return Messages.build_serializationError; 199 200 case DEVICE_PATH: 201 return Messages.bind(Messages.status_cannotUseDeviceOnPath, getPath().toString()); 202 203 case DOM_EXCEPTION: 204 return Messages.status_JDOMError; 205 206 case ELEMENT_DOES_NOT_EXIST: 207 return Messages.bind(Messages.element_doesNotExist, ((JavaElement)elements[0]).toStringWithAncestors()); 208 209 case ELEMENT_NOT_ON_CLASSPATH: 210 return Messages.bind(Messages.element_notOnClasspath, ((JavaElement)elements[0]).toStringWithAncestors()); 211 212 case EVALUATION_ERROR: 213 return Messages.bind(Messages.status_evaluationError, string); 214 215 case INDEX_OUT_OF_BOUNDS: 216 return Messages.status_indexOutOfBounds; 217 218 case INVALID_CONTENTS: 219 return Messages.status_invalidContents; 220 221 case INVALID_DESTINATION: 222 return Messages.bind(Messages.status_invalidDestination, ((JavaElement)elements[0]).toStringWithAncestors()); 223 224 case INVALID_ELEMENT_TYPES: 225 StringBuffer buff= new StringBuffer (Messages.operation_notSupported); 226 for (int i= 0; i < elements.length; i++) { 227 if (i > 0) { 228 buff.append(", "); } 230 buff.append(((JavaElement)elements[i]).toStringWithAncestors()); 231 } 232 return buff.toString(); 233 234 case INVALID_NAME: 235 return Messages.bind(Messages.status_invalidName, string); 236 237 case INVALID_PACKAGE: 238 return Messages.bind(Messages.status_invalidPackage, string); 239 240 case INVALID_PATH: 241 if (string != null) { 242 return string; 243 } else { 244 return Messages.bind( 245 Messages.status_invalidPath, 246 new String [] {getPath() == null ? "null" : getPath().toString()} ); 248 } 249 250 case INVALID_PROJECT: 251 return Messages.bind(Messages.status_invalidProject, string); 252 253 case INVALID_RESOURCE: 254 return Messages.bind(Messages.status_invalidResource, string); 255 256 case INVALID_RESOURCE_TYPE: 257 return Messages.bind(Messages.status_invalidResourceType, string); 258 259 case INVALID_SIBLING: 260 if (string != null) { 261 return Messages.bind(Messages.status_invalidSibling, string); 262 } else { 263 return Messages.bind(Messages.status_invalidSibling, ((JavaElement)elements[0]).toStringWithAncestors()); 264 } 265 266 case IO_EXCEPTION: 267 return Messages.status_IOException; 268 269 case NAME_COLLISION: 270 if (elements != null && elements.length > 0) { 271 IJavaElement element = elements[0]; 272 if (element instanceof PackageFragment && ((PackageFragment) element).isDefaultPackage()) { 273 return Messages.operation_cannotRenameDefaultPackage; 274 } 275 } 276 if (string != null) { 277 return string; 278 } else { 279 return Messages.bind(Messages.status_nameCollision, ""); } 281 case NO_ELEMENTS_TO_PROCESS: 282 return Messages.operation_needElements; 283 284 case NULL_NAME: 285 return Messages.operation_needName; 286 287 case NULL_PATH: 288 return Messages.operation_needPath; 289 290 case NULL_STRING: 291 return Messages.operation_needString; 292 293 case PATH_OUTSIDE_PROJECT: 294 return Messages.bind(Messages.operation_pathOutsideProject, new String [] {string, ((JavaElement)elements[0]).toStringWithAncestors()}); 295 296 case READ_ONLY: 297 IJavaElement element = elements[0]; 298 String name = element.getElementName(); 299 if (element instanceof IPackageFragment && name.equals(IPackageFragment.DEFAULT_PACKAGE_NAME)) { 300 return Messages.status_defaultPackageReadOnly; 301 } 302 return Messages.bind(Messages.status_readOnly, name); 303 304 case RELATIVE_PATH: 305 return Messages.bind(Messages.operation_needAbsolutePath, getPath().toString()); 306 307 case TARGET_EXCEPTION: 308 return Messages.status_targetException; 309 310 case UPDATE_CONFLICT: 311 return Messages.status_updateConflict; 312 313 case NO_LOCAL_CONTENTS : 314 return Messages.bind(Messages.status_noLocalContents, getPath().toString()); 315 316 case CP_CONTAINER_PATH_UNBOUND: 317 IJavaProject javaProject = (IJavaProject)elements[0]; 318 ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(this.path.segment(0)); 319 String description = null; 320 if (initializer != null) description = initializer.getDescription(this.path, javaProject); 321 if (description == null) description = path.makeRelative().toString(); 322 return Messages.bind(Messages.classpath_unboundContainerPath, new String [] {description, javaProject.getElementName()}); 323 324 case INVALID_CP_CONTAINER_ENTRY: 325 javaProject = (IJavaProject)elements[0]; 326 IClasspathContainer container = null; 327 description = null; 328 try { 329 container = JavaCore.getClasspathContainer(path, javaProject); 330 } catch(JavaModelException e){ 331 } 333 if (container == null) { 334 initializer = JavaCore.getClasspathContainerInitializer(path.segment(0)); 335 if (initializer != null) description = initializer.getDescription(path, javaProject); 336 } else { 337 description = container.getDescription(); 338 } 339 if (description == null) description = path.makeRelative().toString(); 340 return Messages.bind(Messages.classpath_invalidContainer, new String [] {description, javaProject.getElementName()}); 341 342 case CP_VARIABLE_PATH_UNBOUND: 343 javaProject = (IJavaProject)elements[0]; 344 return Messages.bind(Messages.classpath_unboundVariablePath, new String [] {path.makeRelative().toString(), javaProject.getElementName()}); 345 346 case CLASSPATH_CYCLE: 347 javaProject = (IJavaProject)elements[0]; 348 return Messages.bind(Messages.classpath_cycle, javaProject.getElementName()); 349 350 case DISABLED_CP_EXCLUSION_PATTERNS: 351 javaProject = (IJavaProject)elements[0]; 352 String projectName = javaProject.getElementName(); 353 IPath newPath = path; 354 if (path.segment(0).toString().equals(projectName)) { 355 newPath = path.removeFirstSegments(1); 356 } 357 return Messages.bind(Messages.classpath_disabledInclusionExclusionPatterns, new String [] {newPath.makeRelative().toString(), projectName}); 358 359 case DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS: 360 javaProject = (IJavaProject)elements[0]; 361 projectName = javaProject.getElementName(); 362 newPath = path; 363 if (path.segment(0).toString().equals(projectName)) { 364 newPath = path.removeFirstSegments(1); 365 } 366 return Messages.bind(Messages.classpath_disabledMultipleOutputLocations, new String [] {newPath.makeRelative().toString(), projectName}); 367 368 case INCOMPATIBLE_JDK_LEVEL: 369 javaProject = (IJavaProject)elements[0]; 370 return Messages.bind(Messages.classpath_incompatibleLibraryJDKLevel, new String []{ 371 javaProject.getElementName(), 372 javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true), 373 path.makeRelative().toString(), 374 string, 375 }); 376 377 case CANNOT_RETRIEVE_ATTACHED_JAVADOC : 378 if (elements != null && elements.length == 1) { 379 if (this.string != null) { 380 return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, ((JavaElement)elements[0]).toStringWithAncestors(), this.string); 381 } 382 return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, ((JavaElement)elements[0]).toStringWithAncestors(), ""); } 384 if (this.string != null) { 385 return Messages.bind(Messages.status_cannot_retrieve_attached_javadoc, this.string, ""); } 387 break; 388 389 case UNKNOWN_JAVADOC_FORMAT : 390 return Messages.bind(Messages.status_unknown_javadoc_format, ((JavaElement)elements[0]).toStringWithAncestors()); 391 392 case DEPRECATED_VARIABLE : 393 javaProject = (IJavaProject)elements[0]; 394 return Messages.bind(Messages.classpath_deprecated_variable, new String [] {path.segment(0).toString(), javaProject.getElementName(), this.string}); 395 } 396 if (string != null) { 397 return string; 398 } else { 399 return ""; } 401 } else { 402 String message = exception.getMessage(); 403 if (message != null) { 404 return message; 405 } else { 406 return exception.toString(); 407 } 408 } 409 } 410 413 public IPath getPath() { 414 return path; 415 } 416 419 public int getSeverity() { 420 if (children == NO_CHILDREN) return super.getSeverity(); 421 int severity = -1; 422 for (int i = 0, max = children.length; i < max; i++) { 423 int childrenSeverity = children[i].getSeverity(); 424 if (childrenSeverity > severity) { 425 severity = childrenSeverity; 426 } 427 } 428 return severity; 429 } 430 434 public String getString() { 435 return string; 436 } 437 440 public boolean isDoesNotExist() { 441 int code = getCode(); 442 return code == ELEMENT_DOES_NOT_EXIST || code == ELEMENT_NOT_ON_CLASSPATH; 443 } 444 447 public boolean isMultiStatus() { 448 return children != NO_CHILDREN; 449 } 450 453 public boolean isOK() { 454 return getCode() == OK; 455 } 456 459 public boolean matches(int mask) { 460 if (! isMultiStatus()) { 461 return matches(this, mask); 462 } else { 463 for (int i = 0, max = children.length; i < max; i++) { 464 if (matches((JavaModelStatus) children[i], mask)) 465 return true; 466 } 467 return false; 468 } 469 } 470 473 protected boolean matches(JavaModelStatus status, int mask) { 474 int severityMask = mask & 0x7; 475 int categoryMask = mask & ~0x7; 476 int bits = status.getBits(); 477 return ((severityMask == 0) || (bits & severityMask) != 0) && ((categoryMask == 0) || (bits & categoryMask) != 0); 478 } 479 485 public static IJavaModelStatus newMultiStatus(IJavaModelStatus[] children) { 486 JavaModelStatus jms = new JavaModelStatus(); 487 jms.children = children; 488 return jms; 489 } 490 494 public String toString() { 495 if (this == VERIFIED_OK){ 496 return "JavaModelStatus[OK]"; } 498 StringBuffer buffer = new StringBuffer (); 499 buffer.append("Java Model Status ["); buffer.append(getMessage()); 501 buffer.append("]"); return buffer.toString(); 503 } 504 } 505 | Popular Tags |