use of org.eclipse.n4js.packagejson.PackageJsonProperties in project n4js by eclipse.
the class AbstractPackageJSONValidatorExtension method checkUsingCheckPropertyMethods.
/**
* Collects all {@link CheckProperty} methods of this class and invokes them on the corresponding properties in the
* given JSON document
*/
@Check
public void checkUsingCheckPropertyMethods(JSONDocument document) {
final List<Method> allMethods = Arrays.asList(this.getClass().getDeclaredMethods());
final List<Pair<CheckProperty, Method>> checkKeyMethods = allMethods.stream().filter(m -> m.getAnnotationsByType(CheckProperty.class).length != 0).filter(m -> isValidCheckKeyMethod(m)).map(m -> Pair.of(m.getAnnotationsByType(CheckProperty.class)[0], m)).collect(Collectors.toList());
final Multimap<String, JSONValue> documentValues = collectDocumentValues(document);
for (Pair<CheckProperty, Method> methodPair : checkKeyMethods) {
final CheckProperty annotation = methodPair.getKey();
final Method method = methodPair.getValue();
final PackageJsonProperties property = annotation.property();
final Collection<JSONValue> values = documentValues.get(property.getPath());
final DataCollector dcCheckMethod = N4JSDataCollectors.createDataCollectorForCheckMethod(method.getName());
// check each value that has been specified for keyPath
for (JSONValue value : values) {
if (value != null) {
try (Measurement m = dcCheckMethod.getMeasurement()) {
// invoke method without any or with value as single argument
if (method.getParameterTypes().length == 0) {
method.invoke(this);
} else {
method.invoke(this, value);
}
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new IllegalStateException("Failed to invoke @CheckProperty method " + method + ": " + e);
} catch (InvocationTargetException e) {
// GH-2002: TEMPORARY DEBUG LOGGING
// Only passing the exception to Logger#error(String,Throwable) does not emit the stack trace of
// the caught exception in all logger configurations; we therefore include the stack trace in
// the main message:
LOGGER.error("Failed to invoke @CheckProperty method " + method + ": " + e.getTargetException().getMessage() + "\n" + Throwables.getStackTraceAsString(e.getTargetException()));
e.getTargetException().printStackTrace();
}
}
}
}
}
use of org.eclipse.n4js.packagejson.PackageJsonProperties in project n4js by eclipse.
the class JSONIdeContentProposalProvider method createNameValueProposals.
private void createNameValueProposals(ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
EObject model = context.getCurrentModel();
List<String> namePath = CompletionUtils.getJsonPathNames(model);
Set<String> alreadyUsedNames = CompletionUtils.getAlreadyUsedNames(model);
List<PackageJsonProperties> pathProps = PackageJsonProperties.valuesOfPath(namePath);
for (PackageJsonProperties pathProp : pathProps) {
String name = pathProp.name;
String label = name;
if (!alreadyUsedNames.contains(name) && this.isMatchingPairPrefix(context, name)) {
if (context.getPrefix().startsWith("\"")) {
label = '"' + name + '"';
}
String proposal = null;
String kind = null;
if (pathProp.valueType == JSONStringLiteral.class) {
proposal = String.format("\"%s\": \"$1\"$0", name);
kind = ContentAssistEntry.KIND_PROPERTY;
} else if (pathProp.valueType == JSONArray.class) {
proposal = String.format("\"%s\": [\n\t$1\n]$0", name);
kind = ContentAssistEntry.KIND_VALUE;
} else if (pathProp.valueType == JSONObject.class) {
proposal = String.format("\"%s\": {\n\t$1\n}$0", name);
kind = ContentAssistEntry.KIND_CLASS;
}
if (proposal != null) {
addTemplateProposal(proposal, label, pathProp.description, kind, context, acceptor);
}
}
}
if (pathProps.isEmpty()) {
addTemplateProposal("\"${1:name}\": \"$2\"$0", "<value>", "Generic name value pair", ContentAssistEntry.KIND_PROPERTY, context, acceptor);
addTemplateProposal("\"${1:name}\": [\n\t$2\n]$0", "<array>", "Generic name array pair", ContentAssistEntry.KIND_VALUE, context, acceptor);
addTemplateProposal("\"${1:name}\": {\n\t$2\n}$0", "<object>", "Generic name object pair", ContentAssistEntry.KIND_CLASS, context, acceptor);
}
}
Aggregations