use of org.stjs.generator.javascript.NameValue in project st-js by st-js.
the class ClassWriter method getAnnotationForElement.
private JS getAnnotationForElement(AnnotationTree ann, WriterVisitor<JS> visitor, GenerationContext<JS> context) {
// XXX: should use here type names?
if (AnnotationHelper.getRetentionType(ann.getAnnotationType()) == RetentionPolicy.SOURCE) {
return null;
}
String annEntryKey = ann.getAnnotationType().toString();
if (!context.getConfiguration().getAnnotations().contains(annEntryKey)) {
return null;
}
List<NameValue<JS>> annotationArgsDesc = new ArrayList<NameValue<JS>>();
for (ExpressionTree arg : ann.getArguments()) {
AssignmentTree assign = (AssignmentTree) arg;
annotationArgsDesc.add(NameValue.of(assign.getVariable().toString(), writeAnnotationValue(visitor, assign.getExpression(), context)));
}
return context.js().object(annotationArgsDesc);
}
use of org.stjs.generator.javascript.NameValue in project st-js by st-js.
the class ClassWriter method addAnnotationsForElement.
private void addAnnotationsForElement(String name, List<NameValue<JS>> props, WriterVisitor<JS> visitor, List<? extends AnnotationTree> annotations, GenerationContext<JS> context) {
if (annotations.isEmpty()) {
return;
}
List<NameValue<JS>> annotationsDesc = new ArrayList<NameValue<JS>>();
for (AnnotationTree ann : annotations) {
JS annotationArgs = getAnnotationForElement(ann, visitor, context);
if (annotationArgs != null) {
// XXX: hack here to quote the type name - to change when using the type names
String annEntryKey = ann.getAnnotationType().toString();
annotationsDesc.add(NameValue.of("\"" + annEntryKey + "\"", annotationArgs));
}
}
if (!annotationsDesc.isEmpty()) {
props.add(NameValue.of(name, context.js().object(annotationsDesc)));
}
}
use of org.stjs.generator.javascript.NameValue in project st-js by st-js.
the class ClassWriter method getAnnotationDescription.
/**
* build the annotation description element
*
* <pre>
* $annotations : {
* _: {....}
* field1: {...}
* method1: {...}
* method1$0: {...}
* method1$1: {...}...
* }
* </pre>
*
* for each annotation list you have:
*
* <pre>
* {
* "annotationType1": [expr1, expr2, expr3],
* "annotationType2": []
* }
* </pre>
*/
private JS getAnnotationDescription(WriterVisitor<JS> visitor, ClassTree classTree, GenerationContext<JS> context) {
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
addAnnotationsForElement("_", props, visitor, classTree.getModifiers().getAnnotations(), context);
for (Tree member : classTree.getMembers()) {
if (MemberWriters.shouldSkip(context.getCurrentWrapper().child(member))) {
continue;
}
if (member instanceof VariableTree) {
VariableTree field = (VariableTree) member;
addAnnotationsForElement(field.getName().toString(), props, visitor, field.getModifiers().getAnnotations(), context);
} else if (member instanceof MethodTree) {
addAnnotationsForMethod((MethodTree) member, props, visitor, context);
}
}
return context.js().object(props);
}
use of org.stjs.generator.javascript.NameValue in project st-js by st-js.
the class NewClassWriter method getObjectInitializer.
/**
* special construction for object initialization new Object(){{x = 1; y = 2; }};
*/
private JS getObjectInitializer(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
NewClassTree tree = tw.getTree();
BlockTree initBlock = getDoubleBracesBlock(tree);
if (initBlock == null) {
if (tw.child(tree.getIdentifier()).isSyntheticType()) {
// the syntethic type will generate {} constructor even without the initBlock
return tw.getContext().js().object(new ArrayList<NameValue<JS>>());
}
return null;
}
if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
// this is already checked and not allowed
return null;
}
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
for (StatementTree stmt : initBlock.getStatements()) {
// check the right type of statements x=y is done in NewClassObjectInitCheck
ExpressionTree expr = ((ExpressionStatementTree) stmt).getExpression();
if (expr instanceof AssignmentTree) {
AssignmentTree assign = (AssignmentTree) expr;
props.add(NameValue.of(getPropertyName(assign.getVariable()), visitor.scan(assign.getExpression(), tw.getContext())));
} else {
MethodInvocationTree meth = (MethodInvocationTree) expr;
String propertyName = MethodToPropertyTemplate.getPropertyName(meth);
JS value = visitor.scan(meth.getArguments().get(0), tw.getContext());
props.add(NameValue.of(propertyName, value));
}
}
return tw.getContext().js().object(props);
}
use of org.stjs.generator.javascript.NameValue in project st-js by st-js.
the class ClassWriter method getTypeDescription.
@SuppressWarnings("unused")
private JS getTypeDescription(WriterVisitor<JS> visitor, ClassTree tree, GenerationContext<JS> context) {
// if (isGlobal(type)) {
// printer.print(JavascriptKeywords.NULL);
// return;
// }
TypeElement type = TreeUtils.elementFromDeclaration(tree);
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
for (Element member : ElementUtils.getAllFieldsIn(type)) {
TypeMirror memberType = ElementUtils.getType(member);
if (JavaNodes.isJavaScriptPrimitive(memberType)) {
continue;
}
if (member.getKind() == ElementKind.ENUM_CONSTANT) {
continue;
}
if (memberType instanceof TypeVariable) {
// what to do with fields of generic parameters !?
continue;
}
if (!skipTypeDescForField(member)) {
props.add(NameValue.of(member.getSimpleName(), getFieldTypeDesc(memberType, context)));
}
}
return context.js().object(props);
}
Aggregations