Example usage for org.eclipse.jdt.internal.compiler.impl Constant NotAConstant

List of usage examples for org.eclipse.jdt.internal.compiler.impl Constant NotAConstant

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.impl Constant NotAConstant.

Prototype

Constant NotAConstant

To view the source code for org.eclipse.jdt.internal.compiler.impl Constant NotAConstant.

Click Source Link

Usage

From source file:com.android.tools.lint.EcjParser.java

License:Apache License

@Nullable
private Object getConstantValue(@Nullable Object value) {
    if (value instanceof Constant) {
        if (value == Constant.NotAConstant) {
            return null;
        }// ww  w . ja v a2s  .c  om
        if (value instanceof StringConstant) {
            return ((StringConstant) value).stringValue();
        } else if (value instanceof IntConstant) {
            return ((IntConstant) value).intValue();
        } else if (value instanceof BooleanConstant) {
            return ((BooleanConstant) value).booleanValue();
        } else if (value instanceof FloatConstant) {
            return ((FloatConstant) value).floatValue();
        } else if (value instanceof LongConstant) {
            return ((LongConstant) value).longValue();
        } else if (value instanceof DoubleConstant) {
            return ((DoubleConstant) value).doubleValue();
        } else if (value instanceof ShortConstant) {
            return ((ShortConstant) value).shortValue();
        } else if (value instanceof CharConstant) {
            return ((CharConstant) value).charValue();
        } else if (value instanceof ByteConstant) {
            return ((ByteConstant) value).byteValue();
        }
    } else if (value instanceof Object[]) {
        Object[] array = (Object[]) value;
        if (array.length > 0) {
            List<Object> list = Lists.newArrayListWithExpectedSize(array.length);
            for (Object element : array) {
                list.add(getConstantValue(element));
            }
            // Pick type of array. Annotations are limited to Strings, Classes
            // and Annotations
            if (!list.isEmpty()) {
                Object first = list.get(0);
                if (first instanceof String) {
                    //noinspection SuspiciousToArrayCall
                    return list.toArray(new String[list.size()]);
                } else if (first instanceof java.lang.annotation.Annotation) {
                    //noinspection SuspiciousToArrayCall
                    return list.toArray(new Annotation[list.size()]);
                } else if (first instanceof Class) {
                    //noinspection SuspiciousToArrayCall
                    return list.toArray(new Class[list.size()]);
                }
            }

            return list.toArray();
        }
    } else if (value instanceof AnnotationBinding) {
        return new EcjResolvedAnnotation((AnnotationBinding) value);
    }

    return value;
}

From source file:com.android.tools.lint.psi.EcjPsiManager.java

License:Apache License

@Nullable
static Object getConstantValue(@Nullable Constant value) {
    if (value == null || value == Constant.NotAConstant) {
        return null;
    } else if (value instanceof StringConstant) {
        return value.stringValue();
    } else if (value instanceof IntConstant) {
        return value.intValue();
    } else if (value instanceof BooleanConstant) {
        return value.booleanValue();
    } else if (value instanceof FloatConstant) {
        return value.floatValue();
    } else if (value instanceof LongConstant) {
        return value.longValue();
    } else if (value instanceof DoubleConstant) {
        return value.doubleValue();
    } else if (value instanceof ShortConstant) {
        return value.shortValue();
    } else if (value instanceof CharConstant) {
        return value.charValue();
    } else if (value instanceof ByteConstant) {
        return value.byteValue();
    }//from w  w  w.j  a  v a  2 s.  c o  m

    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.Member.java

License:Open Source License

/**
 * Converts a field constant from the compiler's representation
 * to the Java Model constant representation (Number or String).
 *///w  ww. j  ava  2 s . co  m
protected static Object convertConstant(Constant constant) {
    if (constant == null)
        return null;
    if (constant == Constant.NotAConstant) {
        return null;
    }
    switch (constant.typeID()) {
    case TypeIds.T_boolean:
        return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
    case TypeIds.T_byte:
        return new Byte(constant.byteValue());
    case TypeIds.T_char:
        return new Character(constant.charValue());
    case TypeIds.T_double:
        return new Double(constant.doubleValue());
    case TypeIds.T_float:
        return new Float(constant.floatValue());
    case TypeIds.T_int:
        return new Integer(constant.intValue());
    case TypeIds.T_long:
        return new Long(constant.longValue());
    case TypeIds.T_short:
        return new Short(constant.shortValue());
    case TypeIds.T_JavaLangString:
        return constant.stringValue();
    default:
        return null;
    }
}

From source file:com.google.gwt.dev.jjs.impl.GenerateJavaAST.java

License:Apache License

/**
 * Returns <code>true</code> if JDT optimized the condition to
 * <code>false</code>.//from  w  w  w.  j a  va2s. c o  m
 */
private static boolean isOptimizedFalse(Expression condition) {
    if (condition != null) {
        Constant cst = condition.optimizedBooleanConstant();
        if (cst != Constant.NotAConstant) {
            if (cst.booleanValue() == false) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.google.gwt.dev.jjs.impl.GenerateJavaAST.java

License:Apache License

/**
 * Returns <code>true</code> if JDT optimized the condition to
 * <code>true</code>./*ww w  . j  a  v a2 s  .  co m*/
 */
private static boolean isOptimizedTrue(Expression condition) {
    if (condition != null) {
        Constant cst = condition.optimizedBooleanConstant();
        if (cst != Constant.NotAConstant) {
            if (cst.booleanValue()) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.google.gwt.dev.jjs.impl.GwtAstBuilder.java

License:Apache License

private boolean isOptimizableCompileTimeConstant(Binding binding) {
    if (binding instanceof LocalVariableBinding
            && ((LocalVariableBinding) binding).constant() != Constant.NotAConstant) {
        // Propagate constants in local variables regardless whether we are optimizing compile time
        // constants or not. This is necessary as the JDT will not compute an emulation path for a
        // local constant referred in a nested class closure.
        return true;
    }//from   ww w. j  a va  2 s.  c  o  m
    if (!(binding instanceof FieldBinding)) {
        return false;
    }
    FieldBinding fieldBinding = (FieldBinding) binding;
    return (compilerContext.getOptions().shouldJDTInlineCompileTimeConstants()
            || isBinaryBinding(fieldBinding.declaringClass)) && isCompileTimeConstant(fieldBinding);
}

From source file:com.google.gwt.dev.jjs.impl.GwtAstBuilder.java

License:Apache License

private static boolean isCompileTimeConstant(FieldBinding binding) {
    assert !binding.isFinal() || !binding.isVolatile();
    boolean isCompileTimeConstant = binding.isStatic() && binding.isFinal()
            && binding.constant() != Constant.NotAConstant;
    assert !isCompileTimeConstant || binding.type.isBaseType() || (binding.type.id == TypeIds.T_JavaLangString);
    return isCompileTimeConstant;
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.java

License:Open Source License

private FieldNode fieldBindingToFieldNode(FieldBinding fieldBinding, TypeDeclaration groovyTypeDecl) {
    String name = new String(fieldBinding.name);
    int modifiers = fieldBinding.modifiers;
    ClassNode fieldType = resolver.convertToClassNode(fieldBinding.type);
    Constant c = fieldBinding.constant();

    Expression initializerExpression = null;
    // FIXASC for performance reasons could fetch the initializer lazily if a JDTFieldNode were created
    if (c == Constant.NotAConstant) {
        /**//  w  w  w .java  2  s . com
         * If the field binding is for a real source field, we should be able to see any initializer in it.
         */
        if (groovyTypeDecl != null) {
            FieldDeclaration fieldDecl = groovyTypeDecl.declarationOf(fieldBinding);
            if (fieldDecl instanceof FieldDeclarationWithInitializer) {
                initializerExpression = ((FieldDeclarationWithInitializer) fieldDecl).getGroovyInitializer();
            }
        }
    } else {
        if (c instanceof StringConstant) {
            initializerExpression = new ConstantExpression(((StringConstant) c).stringValue());
        } else if (c instanceof BooleanConstant) {
            initializerExpression = new ConstantExpression(((BooleanConstant) c).booleanValue());
        } else if (c instanceof IntConstant) {
            initializerExpression = new ConstantExpression(((IntConstant) c).intValue());
        } else if (c instanceof LongConstant) {
            initializerExpression = new ConstantExpression(((LongConstant) c).longValue());
        } else if (c instanceof DoubleConstant) {
            initializerExpression = new ConstantExpression(((DoubleConstant) c).doubleValue());
        } else if (c instanceof FloatConstant) {
            initializerExpression = new ConstantExpression(((FloatConstant) c).floatValue());
        } else if (c instanceof ByteConstant) {
            initializerExpression = new ConstantExpression(((ByteConstant) c).byteValue());
        } else if (c instanceof CharConstant) {
            initializerExpression = new ConstantExpression(((CharConstant) c).charValue());
        } else if (c instanceof ShortConstant) {
            initializerExpression = new ConstantExpression(((ShortConstant) c).shortValue());
        }
    }
    FieldNode fNode = new JDTFieldNode(fieldBinding, resolver, name, modifiers, fieldType, this,
            initializerExpression);
    return fNode;
}

From source file:org.eclipse.che.jdt.BinaryTypeConvector.java

License:Open Source License

public static JsonElement toJsonConstant(Constant constant) {
    if (constant == null)
        return JsonNull.INSTANCE;
    JsonObject con = new JsonObject();
    con.addProperty("typeId", constant.typeID());
    JsonElement val;
    switch (constant.typeID()) {
    case T_int:
        val = new JsonPrimitive(constant.intValue());
        break;/* w  w w  . j a va  2  s  .c  o m*/
    case T_byte:
        val = new JsonPrimitive(constant.byteValue());
        break;
    case T_short:
        val = new JsonPrimitive(constant.shortValue());
        break;
    case T_char:
        val = new JsonPrimitive(constant.charValue());
        break;
    case T_float:
        val = new JsonPrimitive(String.valueOf(constant.floatValue()));
        break;
    case T_double:
        if (Constant.NotAConstant.equals(constant)) {
            val = new JsonPrimitive("NaN");
            con.addProperty("NotAConstant", 1);
        } else {
            val = new JsonPrimitive(constant.stringValue());
        }
        break;
    case T_boolean:
        val = new JsonPrimitive(constant.booleanValue());
        break;
    case T_long:
        val = new JsonPrimitive(String.valueOf(constant.longValue()));
        break;
    case T_JavaLangString:
        val = new JsonPrimitive(constant.stringValue());
        break;
    default:
        val = JsonNull.INSTANCE;
    }
    con.add("value", val);
    return con;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.java

License:Open Source License

public FieldBinding addSyntheticFieldForInnerclass(LocalVariableBinding actualOuterLocalVariable) {
    if (this.synthetics == null)
        this.synthetics = new HashMap[MAX_SYNTHETICS];
    if (this.synthetics[SourceTypeBinding.FIELD_EMUL] == null)
        this.synthetics[SourceTypeBinding.FIELD_EMUL] = new HashMap(5);

    FieldBinding synthField = (FieldBinding) this.synthetics[SourceTypeBinding.FIELD_EMUL]
            .get(actualOuterLocalVariable);
    if (synthField == null) {
        synthField = new SyntheticFieldBinding(
                CharOperation.concat(TypeConstants.SYNTHETIC_OUTER_LOCAL_PREFIX, actualOuterLocalVariable.name),
                actualOuterLocalVariable.type,
                ClassFileConstants.AccPrivate | ClassFileConstants.AccFinal | ClassFileConstants.AccSynthetic,
                this, Constant.NotAConstant, this.synthetics[SourceTypeBinding.FIELD_EMUL].size());
        this.synthetics[SourceTypeBinding.FIELD_EMUL].put(actualOuterLocalVariable, synthField);
    }/*w w w .ja  va  2 s.  c  om*/

    // ensure there is not already such a field defined by the user
    boolean needRecheck;
    int index = 1;
    do {
        needRecheck = false;
        FieldBinding existingField;
        if ((existingField = getField(synthField.name, true /*resolve*/)) != null) {
            TypeDeclaration typeDecl = this.scope.referenceContext;
            FieldDeclaration[] fieldDeclarations = typeDecl.fields;
            int max = fieldDeclarations == null ? 0 : fieldDeclarations.length;
            for (int i = 0; i < max; i++) {
                FieldDeclaration fieldDecl = fieldDeclarations[i];
                if (fieldDecl.binding == existingField) {
                    synthField.name = CharOperation.concat(TypeConstants.SYNTHETIC_OUTER_LOCAL_PREFIX,
                            actualOuterLocalVariable.name, ("$" + String.valueOf(index++)).toCharArray()); //$NON-NLS-1$
                    needRecheck = true;
                    break;
                }
            }
        }
    } while (needRecheck);
    return synthField;
}