1 /*******************************************************************************
2  * Copyright (c) 2005, 2018 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 - rewrote spec
13  *
14  *******************************************************************************/
15 
16 package org.eclipse.jdt.internal.core.builder;
17 
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.jdt.core.compiler.*;
20 
21 public class CompilationParticipantResult extends BuildContext {
22 	protected SourceFile sourceFile;
23 	protected boolean hasAnnotations; // only set during processAnnotations
24 	protected IFile[] addedFiles; // added/changed generated source files that need to be compiled
25 	protected IFile[] deletedFiles; // previously generated source files that should be deleted
26 	protected CategorizedProblem[] problems; // new problems to report against this compilationUnit
27 	protected String[] dependencies; // fully-qualified type names of any new dependencies, each name is of the form 'p1.p2.A.B'
28 	private boolean isTestCode;
29 
CompilationParticipantResult(SourceFile sourceFile, boolean isTestCode)30 protected CompilationParticipantResult(SourceFile sourceFile, boolean isTestCode) {
31 	this.sourceFile = sourceFile;
32 	this.isTestCode = isTestCode;
33 	this.hasAnnotations = false;
34 	this.addedFiles = null;
35 	this.deletedFiles = null;
36 	this.problems = null;
37 	this.dependencies = null;
38 }
39 
40 /**
41  * Returns the contents of the compilation unit.
42  *
43  * @return the contents of the compilation unit
44  */
45 @Override
getContents()46 public char[] getContents() {
47 	return this.sourceFile.getContents();
48 }
49 
50 /**
51  * Returns the <code>IFile</code> representing the compilation unit.
52  *
53  * @return the <code>IFile</code> representing the compilation unit
54  */
55 @Override
getFile()56 public IFile getFile() {
57 	return this.sourceFile.resource;
58 }
59 
60 /**
61  * Returns whether the compilation unit contained any annotations when it was compiled.
62  *
63  * NOTE: This is only valid during {@link CompilationParticipant#processAnnotations(BuildContext[])}.
64  *
65  * @return whether the compilation unit contained any annotations when it was compiled
66  */
67 @Override
hasAnnotations()68 public boolean hasAnnotations() {
69 	return this.hasAnnotations; // only set during processAnnotations
70 }
71 
72 /**
73  * Record the added/changed generated files that need to be compiled.
74  *
75  * @param addedGeneratedFiles the added/changed files
76  */
77 @Override
recordAddedGeneratedFiles(IFile[] addedGeneratedFiles)78 public void recordAddedGeneratedFiles(IFile[] addedGeneratedFiles) {
79 	int length2 = addedGeneratedFiles.length;
80 	if (length2 == 0) return;
81 
82 	int length1 = this.addedFiles == null ? 0 : this.addedFiles.length;
83 	IFile[] merged = new IFile[length1 + length2];
84 	if (length1 > 0) // always make a copy even if currently empty
85 		System.arraycopy(this.addedFiles, 0, merged, 0, length1);
86 	System.arraycopy(addedGeneratedFiles, 0, merged, length1, length2);
87 	this.addedFiles = merged;
88 }
89 
90 /**
91  * Record the generated files that need to be deleted.
92  *
93  * @param deletedGeneratedFiles the files that need to be deleted
94  */
95 @Override
recordDeletedGeneratedFiles(IFile[] deletedGeneratedFiles)96 public void recordDeletedGeneratedFiles(IFile[] deletedGeneratedFiles) {
97 	int length2 = deletedGeneratedFiles.length;
98 	if (length2 == 0) return;
99 
100 	int length1 = this.deletedFiles == null ? 0 : this.deletedFiles.length;
101 	IFile[] merged = new IFile[length1 + length2];
102 	if (length1 > 0) // always make a copy even if currently empty
103 		System.arraycopy(this.deletedFiles, 0, merged, 0, length1);
104 	System.arraycopy(deletedGeneratedFiles, 0, merged, length1, length2);
105 	this.deletedFiles = merged;
106 }
107 
108 /**
109  * Record the fully-qualified type names of any new dependencies, each name is of the form "p1.p2.A.B".
110  *
111  * @param typeNameDependencies the fully-qualified type names of new dependencies
112  */
113 @Override
recordDependencies(String[] typeNameDependencies)114 public void recordDependencies(String[] typeNameDependencies) {
115 	int length2 = typeNameDependencies.length;
116 	if (length2 == 0) return;
117 
118 	int length1 = this.dependencies == null ? 0 : this.dependencies.length;
119 	String[] merged = new String[length1 + length2];
120 	if (length1 > 0) // always make a copy even if currently empty
121 		System.arraycopy(this.dependencies, 0, merged, 0, length1);
122 	System.arraycopy(typeNameDependencies, 0, merged, length1, length2);
123 	this.dependencies = merged;
124 }
125 
126 /**
127  * Record new problems to report against this compilationUnit.
128  * Markers are persisted for these problems only for the declared managed marker type
129  * (see the 'compilationParticipant' extension point).
130  *
131  * @param newProblems the problems to report
132  */
133 @Override
recordNewProblems(CategorizedProblem[] newProblems)134 public void recordNewProblems(CategorizedProblem[] newProblems) {
135 	int length2 = newProblems.length;
136 	if (length2 == 0) return;
137 
138 	int length1 = this.problems == null ? 0 : this.problems.length;
139 	CategorizedProblem[] merged = new CategorizedProblem[length1 + length2];
140 	if (length1 > 0) // always make a copy even if currently empty
141 		System.arraycopy(this.problems, 0, merged, 0, length1);
142 	System.arraycopy(newProblems, 0, merged, length1, length2);
143 	this.problems = merged;
144 }
145 
reset(boolean detectedAnnotations)146 void reset(boolean detectedAnnotations) {
147 	// called prior to processAnnotations
148 	this.hasAnnotations = detectedAnnotations;
149 	this.addedFiles = null;
150 	this.deletedFiles = null;
151 	this.problems = null;
152 	this.dependencies = null;
153 }
154 
155 @Override
toString()156 public String toString() {
157 	return this.sourceFile.toString();
158 }
159 
160 @Override
isTestCode()161 public boolean isTestCode() {
162 	return this.isTestCode;
163 }
164 }
165