Search in sources :

Example 1 with NameValue

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);
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) ArrayList(java.util.ArrayList) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 2 with NameValue

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)));
    }
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) ArrayList(java.util.ArrayList) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 3 with NameValue

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);
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) MethodTree(com.sun.source.tree.MethodTree) ArrayList(java.util.ArrayList) VariableTree(com.sun.source.tree.VariableTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree)

Example 4 with NameValue

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);
}
Also used : ArrayList(java.util.ArrayList) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) NewClassTree(com.sun.source.tree.NewClassTree) NameValue(org.stjs.generator.javascript.NameValue) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) BlockTree(com.sun.source.tree.BlockTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AssignmentTree(com.sun.source.tree.AssignmentTree)

Example 5 with NameValue

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);
}
Also used : NameValue(org.stjs.generator.javascript.NameValue) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)6 NameValue (org.stjs.generator.javascript.NameValue)6 AssignmentTree (com.sun.source.tree.AssignmentTree)3 ExpressionTree (com.sun.source.tree.ExpressionTree)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 BlockTree (com.sun.source.tree.BlockTree)2 VariableTree (com.sun.source.tree.VariableTree)2 ClassTree (com.sun.source.tree.ClassTree)1 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 MethodTree (com.sun.source.tree.MethodTree)1 NewArrayTree (com.sun.source.tree.NewArrayTree)1 NewClassTree (com.sun.source.tree.NewClassTree)1 StatementTree (com.sun.source.tree.StatementTree)1 Tree (com.sun.source.tree.Tree)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 TypeVariable (javax.lang.model.type.TypeVariable)1