Java源码示例:org.eclipse.xtext.diagnostics.AbstractDiagnostic
示例1
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
JvmDeclaredType declaringType = getConstructor().getDeclaringType();
if (declaringType.isAbstract()) {
String message = "Cannot instantiate the abstract type " + declaringType.getSimpleName();
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
Severity.ERROR,
IssueCodes.ABSTRACT_CLASS_INSTANTIATION,
message,
getExpression(),
getDefaultValidationFeature(), -1, null);
result.accept(diagnostic);
return false;
}
return super.validate(result);
}
示例2
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
if (chosenCandidate.validate(result)) {
StringBuilder messageBuilder = new StringBuilder("Suspiciously overloaded method.\n");
messageBuilder.append("The ").append(getFeatureTypeName()).append("\n\t");
appendCandidate(chosenCandidate, messageBuilder);
messageBuilder.append("\noverloads the ").append(getFeatureTypeName()).append("\n\t");
appendCandidate(rejectedCandidate, messageBuilder);
messageBuilder.append(".");
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(chosenCandidate.getSeverity(IssueCodes.SUSPICIOUSLY_OVERLOADED_FEATURE),
IssueCodes.SUSPICIOUSLY_OVERLOADED_FEATURE, messageBuilder.toString(), getExpression(),
XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1, null);
result.accept(diagnostic);
}
return false;
}
示例3
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
if (!getState().isInstanceContext()) {
JvmIdentifiableElement implicitFeature = getFeature();
if (implicitFeature instanceof JvmType) {
JvmIdentifiableElement feature = getState().getResolvedTypes().getLinkedFeature(getOwner());
if (feature == null || feature.eIsProxy() || !(feature instanceof JvmFeature))
return true;
String message = "Cannot make an implicit reference to this from a static context";
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR,
IssueCodes.STATIC_ACCESS_TO_INSTANCE_MEMBER, message, getOwner(),
XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1, null);
result.accept(diagnostic);
return false;
}
}
return super.validate(result);
}
示例4
protected AbstractDiagnostic createTypeDiagnostic(XExpression expression, LightweightTypeReference actualType, LightweightTypeReference expectedType) {
if (!expectedType.isAny()) {
String actualName = actualType.getSimpleName();
String expectedName = expectedType.getSimpleName();
if (actualName.equals(expectedName)) {
if (expectedType.isAssignableFrom(actualType)) {
return null;
}
}
if (expression.eContainingFeature() == XbasePackage.Literals.XABSTRACT_FEATURE_CALL__IMPLICIT_FIRST_ARGUMENT) {
return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
"Type mismatch: cannot convert implicit first argument from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
expression, null, -1, null);
} else {
return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
"Type mismatch: cannot convert from %s to %s", actualType.getHumanReadableName(), expectedType.getHumanReadableName()),
expression, null, -1, null);
}
} else {
return new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.INCOMPATIBLE_TYPES, String.format(
"Type mismatch: type %s is not applicable at this location", actualType.getHumanReadableName()), expression, null, -1,
null);
}
}
示例5
/**
* Validates this linking candidate and adds respective diagnostics to the given queue.
*
* This checks the following criteria:
* <ol>
* <li>{@link #validateVisibility(IAcceptor) visibility},</li>
* <li>{@link #validateArity(IAcceptor) arity},</li>
* <li>{@link #validateTypeArity(IAcceptor) type arity},</li>
* <li>{@link #validateTypeArgumentConformance(IAcceptor) type arguments},</li>
* <li>{@link #validateUnhandledExceptions(IAcceptor) unhandled excptions},</li>
* </ol>
*/
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
if (!validateVisibility(result)) {
return false;
}
if (!validateArity(result)) {
return false;
}
if (!validateTypeArity(result)) {
return false;
}
if (!validateTypeArgumentConformance(result)) {
return false;
}
if (!validateUnhandledExceptions(result)) {
return false;
}
return true;
}
示例6
protected boolean validateVisibility(IAcceptor<? super AbstractDiagnostic> result) {
if (!isVisible()) {
String message = String.format("The %1$s %2$s%3$s is not visible",
getFeatureTypeName(),
getSimpleFeatureName(),
getFeatureParameterTypesAsString());
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
Severity.ERROR,
IssueCodes.FEATURE_NOT_VISIBLE,
message,
getExpression(),
getDefaultValidationFeature(), -1, null);
result.accept(diagnostic);
return false;
}
return true;
}
示例7
protected boolean validateArity(IAcceptor<? super AbstractDiagnostic> result) {
if (getArityMismatch() != 0) {
String message;
if (getArguments().isEmpty()) {
message = String.format("Invalid number of arguments. The %1$s %2$s%3$s is not applicable without arguments" ,
getFeatureTypeName(),
getSimpleFeatureName(),
getFeatureParameterTypesAsString());
} else {
message = String.format("Invalid number of arguments. The %1$s %2$s%3$s is not applicable for the arguments %4$s" ,
getFeatureTypeName(),
getSimpleFeatureName(),
getFeatureParameterTypesAsString(),
getArgumentTypesAsString());
}
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
Severity.ERROR,
IssueCodes.INVALID_NUMBER_OF_ARGUMENTS,
message,
getExpression(),
getInvalidArgumentsValidationFeature(), -1, null);
result.accept(diagnostic);
return false;
}
return true;
}
示例8
protected boolean validateTypeArity(IAcceptor<? super AbstractDiagnostic> result) {
if (getTypeArityMismatch() != 0) {
String message = String.format("Invalid number of type arguments. The %1$s %2$s%3$s is not applicable for the type arguments %4$s",
getFeatureTypeName(),
getSimpleFeatureName(),
getFeatureTypeParametersAsString(true),
getTypeArgumentsAsString(getSyntacticTypeArguments()));
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
Severity.ERROR,
IssueCodes.INVALID_NUMBER_OF_TYPE_ARGUMENTS,
message,
getExpression(),
getDefaultValidationFeature(), -1, null);
result.accept(diagnostic);
return false;
}
return true;
}
示例9
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
JvmType type = (JvmType) description.getElementOrProxy();
String typeKind = "";
if (type instanceof JvmPrimitiveType || type instanceof JvmVoid) {
typeKind = "primitive type";
} else if (type instanceof JvmAnnotationType) {
typeKind = "annotation type";
} else if (type instanceof JvmEnumerationType) {
typeKind = "enum type";
} else if (type instanceof JvmGenericType && ((JvmGenericType) type).isInterface()) {
typeKind = "interface type";
} else if (type instanceof JvmTypeParameter) {
typeKind = "type parameter";
}
String message = String.format("Cannot instantiate the %s %s", typeKind, type.getSimpleName());
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR,
IssueCodes.ILLEGAL_CLASS_INSTANTIATION, message, getExpression(),
XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, -1, null);
result.accept(diagnostic);
return false;
}
示例10
@Override
public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
IssueImpl issue = new Issue.IssueImpl();
issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic);
issue.setSeverity(severity);
issue.setLineNumber(diagnostic.getLine());
issue.setColumn(diagnostic.getColumn());
issue.setMessage(diagnostic.getMessage());
if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) {
org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic;
issue.setOffset(xtextDiagnostic.getOffset());
issue.setLength(xtextDiagnostic.getLength());
}
if (diagnostic instanceof AbstractDiagnostic) {
AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic)diagnostic;
issue.setUriToProblem(castedDiagnostic.getUriToProblem());
issue.setCode(castedDiagnostic.getCode());
issue.setData(castedDiagnostic.getData());
issue.setLineNumberEnd(castedDiagnostic.getLineEnd());
issue.setColumnEnd(castedDiagnostic.getColumnEnd());
}
issue.setType(CheckType.FAST);
acceptor.accept(issue);
}
示例11
/**
* Persist list diagnostics into string to display the list of issue codes.
*
* @param diagnostics
* list of diagnostics
* @param displayPathToTargetObject
* if true, the path through the object hierarchy is printed out up to the root node
* @return
* string with list of issue codes, separated with a line break
*/
// TODO (ACF-4153) generalize for all kinds of errors and move to AbstractXtextTest
private String diagnosticsToString(final List<Resource.Diagnostic> diagnostics, final boolean displayPathToTargetObject) {
StringBuilder sb = new StringBuilder();
for (Resource.Diagnostic diagnostic : diagnostics) {
if (diagnostic instanceof AbstractDiagnostic) {
AbstractDiagnostic diag = (AbstractDiagnostic) diagnostic;
sb.append(" ");
sb.append(diag.getCode());
if (displayPathToTargetObject) {
sb.append(" at line: ");
sb.append(diag.getLine());
sb.append(" on \n");
sb.append(" ");
sb.append(diag.getUriToProblem());
}
sb.append(LINE_BREAK);
}
}
return sb.toString();
}
示例12
@Override
public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
// START: Following copied from super method
LSPIssue issue = new LSPIssue(); // Changed
issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic);
issue.setSeverity(severity);
issue.setLineNumber(diagnostic.getLine());
issue.setColumn(diagnostic.getColumn());
issue.setMessage(diagnostic.getMessage());
if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) {
org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic;
issue.setOffset(xtextDiagnostic.getOffset());
issue.setLength(xtextDiagnostic.getLength());
}
if (diagnostic instanceof AbstractDiagnostic) {
AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic) diagnostic;
issue.setUriToProblem(castedDiagnostic.getUriToProblem());
issue.setCode(castedDiagnostic.getCode());
issue.setData(castedDiagnostic.getData());
// START: Changes here
INode node = ReflectionUtils.getMethodReturn(AbstractDiagnostic.class, "getNode", diagnostic);
int posEnd = castedDiagnostic.getOffset() + castedDiagnostic.getLength();
LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(node, posEnd);
issue.setLineNumberEnd(lineAndColumn.getLine());
issue.setColumnEnd(lineAndColumn.getColumn());
// END: Changes here
}
issue.setType(CheckType.FAST);
acceptor.accept(issue);
// END: Following copied from super method
}
示例13
@Override
public Collection<AbstractDiagnostic> getQueuedDiagnostics() {
List<AbstractDiagnostic> result = Lists.newArrayList();
for(IResolvedTypes delegate: this) {
result.addAll(delegate.getQueuedDiagnostics());
}
return result;
}
示例14
public void addDiagnostics(final Resource resource) {
if (resource instanceof XtextResource) {
if (((XtextResource) resource).isValidationDisabled())
return;
}
class DiagnosticAcceptor implements IAcceptor<AbstractDiagnostic> {
@Override
public void accept(/* @Nullable */ AbstractDiagnostic diagnostic) {
if (diagnostic instanceof EObjectDiagnosticImpl) {
Severity severity = ((EObjectDiagnosticImpl) diagnostic).getSeverity();
if (severity == Severity.ERROR) {
resource.getErrors().add(diagnostic);
} else if (severity == Severity.WARNING) {
resource.getWarnings().add(diagnostic);
}
} else {
resource.getErrors().add(diagnostic);
}
}
}
DiagnosticAcceptor acceptor = new DiagnosticAcceptor();
addQueuedDiagnostics(acceptor);
addLinkingDiagnostics(acceptor);
addTypeDiagnostics(acceptor);
}
示例15
protected void addTypeDiagnostics(IAcceptor<? super AbstractDiagnostic> acceptor) {
for(Map.Entry<XExpression, List<TypeData>> entry: basicGetExpressionTypes().entrySet()) {
XExpression expression = entry.getKey();
if (!isPropagatedType(expression))
addTypeDiagnostic(expression, mergeTypeData(expression, entry.getValue(), false, false), acceptor);
}
}
示例16
@Override
protected boolean validateArity(IAcceptor<? super AbstractDiagnostic> result) {
if (getFeatureCall() instanceof XFeatureCall) {
XExpression implicitReceiver = getImplicitReceiver();
if (implicitReceiver instanceof XFeatureCall) {
JvmIdentifiableElement feature = ((XFeatureCall) implicitReceiver).getFeature();
if (feature instanceof JvmType && !getState().isInstanceContext()) {
return false;
}
}
}
return super.validateArity(result);
}
示例17
protected boolean validateTypeArgumentConformance(IAcceptor<? super AbstractDiagnostic> result) {
if (getTypeArgumentConformanceFailures(result) == 0) {
// TODO use early exit computation
List<XExpression> arguments = getSyntacticArguments();
for(int i = 0; i < arguments.size(); i++) {
XExpression argument = arguments.get(i);
if (isDefiniteEarlyExit(argument)) {
XExpression errorOn = getExpression();
String message = "Unreachable code.";
EStructuralFeature errorFeature = null;
if (i < arguments.size() - 1) {
errorOn = arguments.get(i + 1);
} else {
errorFeature = getDefaultValidationFeature();
if (errorOn instanceof XBinaryOperation) {
message = "Unreachable code. The right argument expression does not complete normally.";
} else {
message = "Unreachable code. The last argument expression does not complete normally.";
}
}
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
Severity.ERROR,
IssueCodes.UNREACHABLE_CODE,
message,
errorOn,
errorFeature,
-1, null);
result.accept(diagnostic);
return false;
}
}
} else {
return false;
}
return true;
}
示例18
protected boolean validateUnhandledExceptions(IAcceptor<? super AbstractDiagnostic> result) {
if (getFeature() instanceof JvmExecutable) {
JvmExecutable executable = (JvmExecutable) getFeature();
if (getUnhandledExceptionSeverity(executable) != Severity.IGNORE) {
if (!executable.getExceptions().isEmpty()) {
return validateUnhandledExceptions(executable, result);
}
}
}
return true;
}
示例19
public void addDiagnostic(AbstractDiagnostic diagnostic) {
if (diagnostics == null) {
diagnostics = Sets.newLinkedHashSet();
}
if (!diagnostics.add(diagnostic)) {
throw new IllegalStateException("Duplicate diagnostic: " + diagnostic);
}
}
示例20
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
final CastOperatorLinkingCandidate ccandidate = getChosenCandidate();
if (ccandidate.validate(result)) {
final String message = MessageFormat.format(
Messages.SuspiciousOverloadedCastOperatorLinkingCandidate_0,
ccandidate.getValidationDescription(), getRejectedCandidate().toString());
final AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(
ccandidate.getState().getSeverity(IssueCodes.SUSPICIOUSLY_OVERLOADED_FEATURE),
IssueCodes.SUSPICIOUSLY_OVERLOADED_FEATURE, message, getExpression(),
XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1, null);
result.accept(diagnostic);
}
return false;
}
示例21
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
return true;
}
示例22
@Override
public Collection<AbstractDiagnostic> getQueuedDiagnostics() {
return Collections.emptyList();
}
示例23
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
// nothing to do
return true;
}
示例24
@Override
public void addDiagnostic(AbstractDiagnostic diagnostic) {
for (int i = 0; i < components.length; i++) {
components[i].addDiagnostic(diagnostic);
}
}
示例25
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
// no op
return true;
}
示例26
@Override
public void addDiagnostic(AbstractDiagnostic diagnostic) {
resolvedTypes.addDiagnostic(diagnostic);
}
示例27
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
// nothing to do
return true;
}
示例28
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
Candidate candidate = getPrimaryCandidate();
if (candidate.validate(result)) {
StringBuilder messageBuilder = new StringBuilder("Ambiguous ").append(getSyntaxDescriptions()).append(".\n");
messageBuilder.append("The ").append(getFeatureTypeName()).append("s");
Iterator<AbstractPendingLinkingCandidate<?>> iter = candidates.iterator();
boolean first = true;
while(iter.hasNext()) {
AbstractPendingLinkingCandidate<?> next = iter.next();
if (!first) {
if (iter.hasNext()) {
messageBuilder.append(",");
} else {
messageBuilder.append(" and");
}
} else {
first = false;
}
messageBuilder.append("\n\t");
if (!next.getDeclaredTypeParameters().isEmpty()) {
messageBuilder.append(next.getFeatureTypeParametersAsString(true)).append(' ');
}
JvmIdentifiableElement feature = next.getFeature();
messageBuilder.append(feature.getSimpleName());
messageBuilder.append(next.getFeatureParameterTypesAsString());
String declarator = getDeclaratorSimpleName(feature);
if (declarator != null) {
messageBuilder.append(" in ").append(declarator);
}
}
if (candidates.size() == 2) {
messageBuilder.append("\nboth match.");
} else {
messageBuilder.append("\nall match.");
}
AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR,
IssueCodes.AMBIGUOUS_FEATURE_CALL, messageBuilder.toString(), getExpression(),
getFeatureToMark(), -1, getDiagnosticData());
result.accept(diagnostic);
return false;
}
return false;
}
示例29
protected void mergeQueuedDiagnostics(ResolvedTypes parent) {
Collection<AbstractDiagnostic> diagnostics = super.getQueuedDiagnostics();
for(AbstractDiagnostic diagnostic: diagnostics) {
parent.addDiagnostic(diagnostic);
}
}
示例30
@Override
public List<AbstractDiagnostic> getQueuedDiagnostics() {
List<AbstractDiagnostic> result = Lists.newArrayList(super.getQueuedDiagnostics());
result.addAll(parent.getQueuedDiagnostics());
return result;
}