use of org.eclipse.n4js.json.JSON.JSONDocument in project n4js by eclipse.
the class CompletionUtils method getJsonPathNames.
/**
* Return the property names on the path towards the current model.
*/
public static List<String> getJsonPathNames(EObject model) {
List<String> namePath = new LinkedList<>();
EObject eobj = model;
while (!(eobj instanceof JSONDocument)) {
if (eobj instanceof NameValuePair) {
NameValuePair nvp = (NameValuePair) eobj;
namePath.add(0, nvp.getName());
}
eobj = eobj.eContainer();
}
return namePath;
}
use of org.eclipse.n4js.json.JSON.JSONDocument in project n4js by eclipse.
the class JSONValidator method checkDocumentForComments.
/**
* Checks the document for comments (single or multi-line) which are not valid JSON constructs but accepted by our
* parser.
*/
@Check
public void checkDocumentForComments(JSONDocument document) {
ICompositeNode documentNode = NodeModelUtils.findActualNodeFor(document);
ICompositeNode rootNode = documentNode.getRootNode();
// find hidden leaf nodes that fulfill #isCommentNode criteria and add an issue
StreamSupport.stream(rootNode.getAsTreeIterable().spliterator(), false).filter(n -> n instanceof HiddenLeafNode).filter(n -> isCommentNode(n)).forEach(n -> {
addIssue(JSONIssueCodes.getMessageForJSON_COMMENT_UNSUPPORTED(), document, n.getOffset(), n.getLength(), JSONIssueCodes.JSON_COMMENT_UNSUPPORTED);
});
}
use of org.eclipse.n4js.json.JSON.JSONDocument in project n4js by eclipse.
the class ProjectDescriptionLoader method loadPackageJSONAtLocation.
private JSONDocument loadPackageJSONAtLocation(SafeURI<?> location) {
Path path = location.appendSegment(N4JSGlobals.PACKAGE_JSON).toFileSystemPath();
if (!Files.isReadable(path)) {
path = location.appendSegment(N4JSGlobals.PACKAGE_JSON + "." + N4JSGlobals.XT_FILE_EXTENSION).toFileSystemPath();
if (!Files.isReadable(path)) {
return null;
}
}
try {
String jsonString = Files.readString(path, StandardCharsets.UTF_8);
try {
JSONDocument doc = JSONFactory.eINSTANCE.createJSONDocument();
JsonElement jsonElement = JsonParser.parseString(jsonString);
doc.setContent(copy(jsonElement));
return doc;
} catch (JsonParseException e) {
JSONDocument packageJSON = loadXtextFileAtLocation(location, N4JSGlobals.PACKAGE_JSON, jsonString, JSONDocument.class);
return packageJSON;
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
use of org.eclipse.n4js.json.JSON.JSONDocument in project n4js by eclipse.
the class ProjectDescriptionLoader method loadVersionAndN4JSNatureFromProjectDescriptionAtLocation.
/**
* Loads the project description of the N4JS project at the given {@code location} and returns the version string or
* <code>null</code> if undefined or in case of error.
*/
public Pair<String, Boolean> loadVersionAndN4JSNatureFromProjectDescriptionAtLocation(SafeURI<?> location) {
JSONDocument packageJSON = loadPackageJSONAtLocation(location);
JSONValue versionValue = null;
boolean hasN4JSNature = false;
if (packageJSON != null) {
versionValue = JSONModelUtils.getProperty(packageJSON, VERSION.name).orElse(null);
hasN4JSNature = JSONModelUtils.getProperty(packageJSON, N4JS.name).isPresent();
}
Pair<String, Boolean> result = Tuples.create(asNonEmptyStringOrNull(versionValue), hasN4JSNature);
return result;
}
use of org.eclipse.n4js.json.JSON.JSONDocument 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();
}
}
}
}
}
Aggregations