Example usage for org.eclipse.jdt.internal.compiler.ast Expression print

List of usage examples for org.eclipse.jdt.internal.compiler.ast Expression print

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast Expression print.

Prototype

@Override
    public StringBuffer print(int indent, StringBuffer output) 

Source Link

Usage

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

/**
 * Provides AnnotationValues with the data it needs to do its thing.
 *///from  w  w  w . j  a v a 2  s.c  o m
public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(Class<A> type,
        final EclipseNode annotationNode) {

    final Annotation annotation = (Annotation) annotationNode.get();
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();

    MemberValuePair[] memberValuePairs = annotation.memberValuePairs();

    if (memberValuePairs != null)
        for (final MemberValuePair pair : memberValuePairs) {
            List<String> raws = new ArrayList<String>();
            List<Object> expressionValues = new ArrayList<Object>();
            List<Object> guesses = new ArrayList<Object>();
            Expression[] expressions = null;

            char[] n = pair.name;
            String mName = (n == null || n.length == 0) ? "value" : new String(pair.name);
            final Expression rhs = pair.value;
            if (rhs instanceof ArrayInitializer) {
                expressions = ((ArrayInitializer) rhs).expressions;
            } else if (rhs != null) {
                expressions = new Expression[] { rhs };
            }
            if (expressions != null)
                for (Expression ex : expressions) {
                    StringBuffer sb = new StringBuffer();
                    ex.print(0, sb);
                    raws.add(sb.toString());
                    expressionValues.add(ex);
                    guesses.add(calculateValue(ex));
                }

            final Expression[] exprs = expressions;
            values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) {
                @Override
                public void setError(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;

                    if (ex == null)
                        ex = annotation;

                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;

                    annotationNode.addError(message, sourceStart, sourceEnd);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;

                    if (ex == null)
                        ex = annotation;

                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;

                    annotationNode.addWarning(message, sourceStart, sourceEnd);
                }
            });
        }

    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(annotationNode, new ArrayList<String>(),
                    new ArrayList<Object>(), new ArrayList<Object>(), false) {
                @Override
                public void setError(String message, int valueIdx) {
                    annotationNode.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    annotationNode.addWarning(message);
                }
            });
        }
    }

    return new AnnotationValues<A>(type, values, annotationNode);
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java

License:Open Source License

/**
 * Assemble an ifstatement with negated condition, that hides the branch instruction from the debugger,
 * even when stepping into the condition. 
 */// www.  j  a va 2  s.  c o m
public IfStatement stealthIfNotStatement(final Expression condition, Statement thenStatement) {
    // mark step-over after the condition:
    Expression recordingCondition = new Expression() {
        public StringBuffer printExpression(int indent, StringBuffer output) {
            return condition.print(indent, output);
        }

        public TypeBinding resolveType(BlockScope scope) {
            return condition.resolveType(scope);
        }

        public void computeConversion(Scope scope, TypeBinding runtimeType, TypeBinding compileTimeType) {
            condition.computeConversion(scope, runtimeType, compileTimeType);
            this.constant = condition.constant;
            this.bits = condition.bits;
        }

        public Constant optimizedBooleanConstant() {
            this.constant = condition.optimizedBooleanConstant();
            this.bits = condition.bits;
            return this.constant;
        }

        public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo,
                boolean valueRequired) {
            return condition.analyseCode(currentScope, flowContext, flowInfo, valueRequired);
        }

        public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
            return condition.analyseCode(currentScope, flowContext, flowInfo);
        }

        public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
            condition.generateCode(currentScope, codeStream, valueRequired);
            // payload:
            codeStream.pcToSourceMap[codeStream.pcToSourceMapSize++] = codeStream.position;
            codeStream.pcToSourceMap[codeStream.pcToSourceMapSize++] = STEP_OVER_LINENUMBER;
        }
    };
    return new IfStatement(new UnaryExpression( // NOT
            recordingCondition, OperatorIds.NOT), thenStatement, this.sourceStart, this.sourceEnd);
}