use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangResource resourceNode) {
BSymbol resourceSymbol = resourceNode.symbol;
SymbolEnv resourceEnv = SymbolEnv.createResourceActionSymbolEnv(resourceNode, resourceSymbol.scope, env);
resourceNode.annAttachments.forEach(a -> {
a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.RESOURCE);
this.analyzeDef(a, resourceEnv);
});
defineResourceEndpoint(resourceNode, resourceEnv);
resourceNode.docAttachments.forEach(doc -> analyzeDef(doc, resourceEnv));
resourceNode.requiredParams.forEach(p -> analyzeDef(p, resourceEnv));
resourceNode.endpoints.forEach(e -> {
symbolEnter.defineNode(e, resourceEnv);
analyzeDef(e, resourceEnv);
});
analyzeStmt(resourceNode.body, resourceEnv);
this.processWorkers(resourceNode, resourceEnv);
}
use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangService serviceNode) {
BServiceSymbol serviceSymbol = (BServiceSymbol) serviceNode.symbol;
SymbolEnv serviceEnv = SymbolEnv.createServiceEnv(serviceNode, serviceSymbol.scope, env);
handleServiceTypeStruct(serviceNode);
handleServiceEndpointBinds(serviceNode, serviceSymbol);
serviceNode.annAttachments.forEach(a -> {
a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.SERVICE);
this.analyzeDef(a, serviceEnv);
});
serviceNode.docAttachments.forEach(doc -> analyzeDef(doc, serviceEnv));
serviceNode.vars.forEach(v -> this.analyzeDef(v, serviceEnv));
serviceNode.endpoints.forEach(e -> {
symbolEnter.defineNode(e, serviceEnv);
analyzeDef(e, serviceEnv);
});
this.analyzeDef(serviceNode.initFunction, serviceEnv);
serviceNode.resources.forEach(r -> this.analyzeDef(r, serviceEnv));
}
use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint in project ballerina by ballerina-lang.
the class SemanticAnalyzer method visit.
public void visit(BLangObject objectNode) {
BSymbol objectSymbol = objectNode.symbol;
SymbolEnv objectEnv = SymbolEnv.createPkgLevelSymbolEnv(objectNode, objectSymbol.scope, env);
objectNode.fields.forEach(field -> analyzeDef(field, objectEnv));
objectNode.annAttachments.forEach(annotationAttachment -> {
annotationAttachment.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.STRUCT);
annotationAttachment.accept(this);
});
objectNode.docAttachments.forEach(doc -> analyzeDef(doc, objectEnv));
analyzeDef(objectNode.initFunction, objectEnv);
// Visit temporary init statements in the init function
SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(objectNode.initFunction, objectNode.initFunction.symbol.scope, objectEnv);
objectNode.initFunction.initFunctionStmts.values().forEach(s -> analyzeNode(s, funcEnv));
objectNode.functions.forEach(f -> analyzeDef(f, objectEnv));
}
use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint in project ballerina by ballerina-lang.
the class AnnotationAttachment method convertToPackageModel.
/**
* Converts a {@link org.ballerinalang.model.AnnotationAttachment} to {@link AnnotationAttachment}.
* @param annotationAttachment The model to be converted.
* @return Converted model.
*/
public static AnnotationAttachment convertToPackageModel(BLangAnnotationAttachmentPoint annotationAttachment) {
if (null != annotationAttachment) {
AnnotationAttachment tempAnnotationAttachment = new AnnotationAttachment();
// tempAnnotationAttachment.setName(annotationAttachment.getName());
// tempAnnotationAttachment.setPackageName(annotationAttachment.getPkgName());
// tempAnnotationAttachment.setPackagePath(annotationAttachment.getPkgPath());
BLangAnnotationAttachmentPoint.AttachmentPoint attachmentPoint = annotationAttachment.getAttachmentPoint();
if (attachmentPoint != null) {
tempAnnotationAttachment.setAttachedPoint(attachmentPoint.getValue());
}
return tempAnnotationAttachment;
} else {
return null;
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint in project ballerina by ballerina-lang.
the class BallerinaParserService method generateJSON.
public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs) throws InvocationTargetException, IllegalAccessException {
if (node == null) {
return JsonNull.INSTANCE;
}
Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream().flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet());
JsonObject nodeJson = new JsonObject();
JsonArray wsJsonArray = new JsonArray();
Set<Whitespace> ws = node.getWS();
if (ws != null && !ws.isEmpty()) {
for (Whitespace whitespace : ws) {
JsonObject wsJson = new JsonObject();
wsJson.addProperty("ws", whitespace.getWs());
wsJson.addProperty("i", whitespace.getIndex());
wsJson.addProperty("text", whitespace.getPrevious());
wsJson.addProperty("static", whitespace.isStatic());
wsJsonArray.add(wsJson);
}
nodeJson.add("ws", wsJsonArray);
}
Diagnostic.DiagnosticPosition position = node.getPosition();
if (position != null) {
JsonObject positionJson = new JsonObject();
positionJson.addProperty("startColumn", position.getStartColumn());
positionJson.addProperty("startLine", position.getStartLine());
positionJson.addProperty("endColumn", position.getEndColumn());
positionJson.addProperty("endLine", position.getEndLine());
nodeJson.add("position", positionJson);
}
/* Virtual props */
JsonArray type = getType(node);
if (type != null) {
nodeJson.add(SYMBOL_TYPE, type);
}
if (node.getKind() == NodeKind.INVOCATION) {
assert node instanceof BLangInvocation : node.getClass();
BLangInvocation invocation = (BLangInvocation) node;
if (invocation.symbol != null && invocation.symbol.kind != null) {
nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString());
}
}
for (Method m : methods) {
String name = m.getName();
if (name.equals("getWS") || name.equals("getPosition")) {
continue;
}
String jsonName;
if (name.startsWith("get")) {
jsonName = toJsonName(name, 3);
} else if (name.startsWith("is")) {
jsonName = toJsonName(name, 2);
} else {
continue;
}
Object prop = m.invoke(node);
/* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */
if (node.getKind() == NodeKind.LITERAL && "value".equals(jsonName)) {
if (prop instanceof String) {
nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"');
nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop));
} else {
nodeJson.addProperty(jsonName, String.valueOf(prop));
}
continue;
}
if (node.getKind() == NodeKind.ANNOTATION && node instanceof BLangAnnotation) {
JsonArray attachmentPoints = new JsonArray();
((BLangAnnotation) node).getAttachmentPoints().stream().map(BLangAnnotationAttachmentPoint::getAttachmentPoint).map(BLangAnnotationAttachmentPoint.AttachmentPoint::getValue).map(JsonPrimitive::new).forEach(attachmentPoints::add);
nodeJson.add("attachmentPoints", attachmentPoints);
}
if (node.getKind() == NodeKind.USER_DEFINED_TYPE && jsonName.equals("typeName")) {
IdentifierNode typeNode = (IdentifierNode) prop;
Node structNode;
if (typeNode.getValue().startsWith("$anonStruct$") && (structNode = anonStructs.remove(typeNode.getValue())) != null) {
JsonObject anonStruct = generateJSON(structNode, anonStructs).getAsJsonObject();
anonStruct.addProperty("anonStruct", true);
nodeJson.add("anonStruct", anonStruct);
continue;
}
}
if (prop instanceof List && jsonName.equals("types")) {
// Currently we don't need any Symbols for the UI. So skipping for now.
continue;
}
/* Node classes */
if (prop instanceof Node) {
nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs));
} else if (prop instanceof List) {
List listProp = (List) prop;
JsonArray listPropJson = new JsonArray();
nodeJson.add(jsonName, listPropJson);
for (Object listPropItem : listProp) {
if (listPropItem instanceof Node) {
/* Remove top level anon func and struct */
if (node.getKind() == NodeKind.COMPILATION_UNIT) {
if (listPropItem instanceof BLangStruct && ((BLangStruct) listPropItem).isAnonymous) {
anonStructs.put(((BLangStruct) listPropItem).getName().getValue(), ((BLangStruct) listPropItem));
continue;
}
if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) {
continue;
}
}
listPropJson.add(generateJSON((Node) listPropItem, anonStructs));
} else {
logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem);
}
}
/* Runtime model classes */
} else if (prop instanceof Set && jsonName.equals("flags")) {
Set flags = (Set) prop;
for (Flag flag : Flag.values()) {
nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag));
}
} else if (prop instanceof Set) {
// TODO : limit this else if to getInputs getOutputs of transform.
Set vars = (Set) prop;
JsonArray listVarJson = new JsonArray();
nodeJson.add(jsonName, listVarJson);
for (Object obj : vars) {
listVarJson.add(obj.toString());
}
} else if (prop instanceof NodeKind) {
String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString());
nodeJson.addProperty(jsonName, kindName);
} else if (prop instanceof OperatorKind) {
nodeJson.addProperty(jsonName, prop.toString());
/* Generic classes */
} else if (prop instanceof String) {
nodeJson.addProperty(jsonName, (String) prop);
} else if (prop instanceof Number) {
nodeJson.addProperty(jsonName, (Number) prop);
} else if (prop instanceof Boolean) {
nodeJson.addProperty(jsonName, (Boolean) prop);
} else if (prop instanceof Enum) {
nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name()));
} else if (prop != null) {
nodeJson.addProperty(jsonName, prop.toString());
String message = "Node " + node.getClass().getSimpleName() + " contains unknown type prop: " + jsonName + " of type " + prop.getClass();
logger.error(message);
}
}
return nodeJson;
}
Aggregations