-
Notifications
You must be signed in to change notification settings - Fork 316
/
ErrorSafeExtensions.java
162 lines (149 loc) · 5.8 KB
/
ErrorSafeExtensions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xbase.compiler;
import java.util.Objects;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.common.types.JvmAnnotationReference;
import org.eclipse.xtext.common.types.JvmSpecializedTypeReference;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.JvmUnknownTypeReference;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.validation.Issue;
import org.eclipse.xtext.xbase.compiler.output.ErrorTreeAppendable;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
/**
* @author Jan Koehnlein
*/
public class ErrorSafeExtensions {
@Inject
private TypeReferenceSerializer typeReferenceSerializer;
@Inject
private IElementIssueProvider.Factory issueProviderFactory;
public Iterable<Issue> getErrors(EObject element) {
IElementIssueProvider issueProvider = issueProviderFactory.get(element.eResource());
return Iterables.filter(issueProvider.getIssues(element), it -> Severity.ERROR.equals(it.getSeverity()));
}
public boolean hasErrors(EObject element) {
return !IterableExtensions.isEmpty(getErrors(element));
}
public <T extends EObject> void forEachSafely(ITreeAppendable appendable, Iterable<T> elements,
Procedure1<? super LoopParams> loopInitializer, Procedure2<? super T, ? super ITreeAppendable> body) {
if (Iterables.isEmpty(elements)) {
return;
}
LoopParams loopParams = ObjectExtensions.operator_doubleArrow(new LoopParams(), loopInitializer);
boolean allElementsBroken = Iterables
.size(Iterables.<T>filter(elements, (T it) -> hasErrors(it))) == Iterables.size(elements);
ITreeAppendable currentAppendable = null;
if (allElementsBroken) {
currentAppendable = openErrorAppendable(appendable, null);
} else {
currentAppendable = appendable;
}
loopParams.appendPrefix(currentAppendable);
boolean isFirst = true;
boolean isFirstBroken = true;
for (T element : elements) {
if (!hasErrors(element)) {
currentAppendable = closeErrorAppendable(appendable, currentAppendable);
if (!isFirst) {
loopParams.appendSeparator(appendable);
}
isFirst = false;
body.apply(element, appendable);
} else {
if (!allElementsBroken) {
currentAppendable = openErrorAppendable(appendable, currentAppendable);
}
if (!isFirst || !isFirstBroken) {
loopParams.appendSeparator(currentAppendable);
}
isFirstBroken = false;
try {
body.apply(element, currentAppendable);
} catch (Exception ignoreMe) {
}
}
}
if (!allElementsBroken) {
currentAppendable = closeErrorAppendable(appendable, currentAppendable);
}
loopParams.appendSuffix(currentAppendable);
closeErrorAppendable(appendable, currentAppendable);
}
protected ITreeAppendable openErrorAppendable(ITreeAppendable parent, ITreeAppendable child) {
if (child instanceof ErrorTreeAppendable) {
return child;
} else {
return parent.errorChild().append("/* ");
}
}
protected ITreeAppendable closeErrorAppendable(ITreeAppendable parent, ITreeAppendable child) {
if (child instanceof ErrorTreeAppendable && !Objects.equals(child, parent)) {
child.append(" */");
}
return parent;
}
public void serializeSafely(JvmTypeReference typeRef, ITreeAppendable appendable) {
serializeSafely(typeRef, null, appendable);
}
public void serializeSafely(JvmTypeReference typeRef, String surrogateType, ITreeAppendable appendable) {
if (typeRef == null || typeRef.getType() == null) {
if (typeRef instanceof JvmSpecializedTypeReference) {
serializeSafely(((JvmSpecializedTypeReference) typeRef).getEquivalent(), surrogateType, appendable);
} else if (typeRef instanceof JvmUnknownTypeReference) {
appendable.append(((JvmUnknownTypeReference) typeRef).getQualifiedName());
} else {
appendable.append("Object");
ITreeAppendable errorChild = openErrorAppendable(appendable, appendable);
errorChild.append("type is 'null'");
closeErrorAppendable(appendable, errorChild);
}
} else {
if (typeRef.accept(new BrokenTypeRefDetector())) {
ITreeAppendable errorChild = openErrorAppendable(appendable, appendable);
try {
typeReferenceSerializer.serialize(typeRef, typeRef.eContainer(), errorChild);
} catch (Exception ignoreMe) {
}
closeErrorAppendable(appendable, errorChild);
if (surrogateType != null) {
appendable.append(surrogateType);
}
} else {
typeReferenceSerializer.serialize(typeRef, typeRef.eContainer(), appendable);
}
}
}
public void serializeSafely(JvmAnnotationReference annotationRef, ITreeAppendable appendable,
Procedure1<? super ITreeAppendable> onSuccess) {
if (annotationRef == null || annotationRef.getAnnotation() == null) {
ITreeAppendable errorChild = openErrorAppendable(appendable, appendable);
errorChild.append("annotation is 'null'");
closeErrorAppendable(appendable, errorChild);
} else {
if (annotationRef.getAnnotation().eIsProxy()) {
ITreeAppendable errorChild = openErrorAppendable(appendable, appendable);
appendable.append("@");
appendable.append(annotationRef.getAnnotation());
closeErrorAppendable(appendable, errorChild);
} else {
appendable.append("@");
appendable.append(annotationRef.getAnnotation());
onSuccess.apply(appendable);
}
}
}
}