1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.core;
15 
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jdt.core.IClasspathEntry;
20 import org.eclipse.jdt.core.IJavaModelStatus;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.internal.core.builder.JavaBuilder;
23 
24 /*
25  * Validates the raw classpath format and the resolved classpath of this project,
26  * updating markers if necessary.
27  */
28 public class ClasspathValidation {
29 
30 	private JavaProject project;
31 
ClasspathValidation(JavaProject project)32 	public ClasspathValidation(JavaProject project) {
33 		this.project = project;
34 	}
35 
validate()36 	public void validate() {
37 		JavaModelManager.PerProjectInfo perProjectInfo;
38 		try {
39 			perProjectInfo = this.project.getPerProjectInfo();
40 		} catch (JavaModelException e) {
41 			// project doesn't exist
42 			IProject resource = this.project.getProject();
43 			if (resource.isAccessible()) {
44 				this.project.flushClasspathProblemMarkers(true/*flush cycle markers*/, true/*flush classpath format markers*/, true /*flush overlapping output markers*/);
45 
46 				// remove problems and tasks created  by the builder
47 				JavaBuilder.removeProblemsAndTasksFor(resource);
48 			}
49 			return;
50 		}
51 
52 		// use synchronized block to ensure consistency
53 		IClasspathEntry[] rawClasspath;
54 		IPath outputLocation;
55 		IJavaModelStatus status;
56 		synchronized (perProjectInfo) {
57 			rawClasspath = perProjectInfo.rawClasspath;
58 			outputLocation = perProjectInfo.outputLocation;
59 			status = perProjectInfo.rawClasspathStatus; // status has been set during POST_CHANGE
60 		}
61 
62 		// update classpath format problems
63 		this.project.flushClasspathProblemMarkers(false/*cycle*/, true/*format*/, false/*overlapping*/);
64 		if (!status.isOK())
65 			this.project.createClasspathProblemMarker(status);
66 
67 		// update overlapping output problem markers
68 		this.project.flushClasspathProblemMarkers(false/*cycle*/, false/*format*/, true/*overlapping*/);
69 
70 		// update resolved classpath problems
71 		this.project.flushClasspathProblemMarkers(false/*cycle*/, false/*format*/, false/*overlapping*/);
72 
73 		if (rawClasspath != JavaProject.INVALID_CLASSPATH && outputLocation != null) {
74 		 	for (int i = 0; i < rawClasspath.length; i++) {
75 				status = ClasspathEntry.validateClasspathEntry(this.project, rawClasspath[i], false/*src attach*/, false /*not referred by a container*/);
76 				if (!status.isOK()) {
77 					this.project.createClasspathProblemMarker(status);
78 				}
79 			 }
80 			status = ClasspathEntry.validateClasspath(this.project, rawClasspath, outputLocation);
81 			if (status.getCode() != IStatus.OK)
82 				this.project.createClasspathProblemMarker(status);
83 		 }
84 	}
85 
86 }
87