Java Code Examples for javax.lang.model.element.VariableElement#getSimpleName()

The following examples show how to use javax.lang.model.element.VariableElement#getSimpleName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyAnnotatedField.java    From simple-preferences with Apache License 2.0 6 votes vote down vote up
public KeyAnnotatedField(VariableElement element) throws ProcessingException {
  if (element.getModifiers().contains(Modifier.PRIVATE)) {
    throw new ProcessingException(element,
        "Field %s is private, must be accessible from inherited class", element.getSimpleName());
  }
  annotatedElement = element;

  type = TypeName.get(element.asType());

  Key annotation = element.getAnnotation(Key.class);
  name = element.getSimpleName().toString();
  preferenceKey = Strings.isNullOrEmpty(annotation.name()) ? Utils.lowerCamelToLowerSnake(name)
      : annotation.name();
  omitGetterPrefix = annotation.omitGetterPrefix();
  needCommitMethod = annotation.needCommitMethod();
}
 
Example 2
Source File: WebParamDuplicity.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** check if another param has @WebParam annotation of the saem name
 *
 * @param subject
 * @param nameValue
 * @return
 */
private boolean isDuplicate(VariableElement subject, Object nameValue) {
    Element methodEl = subject.getEnclosingElement();
    if (ElementKind.METHOD == methodEl.getKind()) {
        for (VariableElement var: ((ExecutableElement)methodEl).getParameters()) {
            Name paramName = var.getSimpleName();
            if (!paramName.contentEquals(subject.getSimpleName())) {
                AnnotationMirror paramAnn = Utilities.findAnnotation(var, ANNOTATION_WEBPARAM);
                if (paramAnn != null) {
                    AnnotationValue val = Utilities.getAnnotationAttrValue(paramAnn, ANNOTATION_ATTRIBUTE_NAME);
                    if (val != null) {
                        if (nameValue.equals(val.getValue())) {
                            return true;
                        }
                    } else if (paramName.contentEquals(nameValue.toString())) {
                        return true;
                    }
                } else if (paramName.contentEquals(nameValue.toString())) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 3
Source File: AMethod.java    From annotation-tools with MIT License 5 votes vote down vote up
/**
 * Populates the method parameter map for the method.
 * Ensures that the method parameter map always has an entry for each parameter.
 *
 * @param methodElt the method whose parameters should be vivified
 */
private void vivifyAndAddTypeMirrorToParameters(ExecutableElement methodElt) {
    for (int i = 0; i < methodElt.getParameters().size(); i++) {
        VariableElement ve = methodElt.getParameters().get(i);
        TypeMirror type = ve.asType();
        Name name = ve.getSimpleName();
        vivifyAndAddTypeMirrorToParameter(i, type, name);
    }
}
 
Example 4
Source File: IntroduceHint.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Crates method formal parameters, following code style conventions.
 * The trees in 'statements' will be rewritten to use the new identifiers.
 * 
 * @param copy working copy
 * @param parameters variables to turn into parameters
 * @param statements trees that should refer to parameters
 * @return 
 */
static List<VariableTree> createVariables(WorkingCopy copy, List<VariableElement> parameters, 
        TreePath targetParent,
        List<TreePath> statements) {
    final TreeMaker make = copy.getTreeMaker();
    List<VariableTree> formalArguments = new LinkedList<VariableTree>();
    CodeStyle cs = CodeStyle.getDefault(copy.getFileObject());
    
    String prefix = cs.getParameterNamePrefix();
    String suffix = cs.getParameterNameSuffix(); 
    Map<VariableElement, CharSequence> renamedVariables = new HashMap<VariableElement, CharSequence>();
    Set<Name> changedNames = new HashSet<Name>();
    for (VariableElement p : parameters) {
        TypeMirror tm = p.asType();
        Tree type = make.Type(tm);
        Name formalArgName = p.getSimpleName();
        Set<Modifier> formalArgMods = EnumSet.noneOf(Modifier.class);
        
        if (p.getModifiers().contains(Modifier.FINAL)) {
            formalArgMods.add(Modifier.FINAL);
        }
        String strippedName = Utilities.stripVariableName(cs, p);
        CharSequence codeStyleName = Utilities.guessName(copy, strippedName, targetParent, prefix, suffix, p.getKind() == ElementKind.PARAMETER);
        if (!formalArgName.contentEquals(codeStyleName)) {
            renamedVariables.put(p, codeStyleName);
            changedNames.add(formalArgName);
        } else {
            codeStyleName = formalArgName;
        }
        formalArguments.add(make.Variable(make.Modifiers(formalArgMods), codeStyleName, type, null));
    }
    if (!changedNames.isEmpty()) {
        VariableRenamer renamer = new VariableRenamer(copy, renamedVariables, changedNames);
        for (TreePath stPath : statements) {
            renamer.scan(stPath, null);
        }
    }
    return formalArguments;
}
 
Example 5
Source File: T8136453.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void run() {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    List<String> opts = Arrays.asList("-parameters");
    JavacTask task = (JavacTask) compiler.getTask(null, null, null, opts, null, null);
    TypeElement t = task.getElements().getTypeElement("T");
    ExecutableElement testMethod = ElementFilter.methodsIn(t.getEnclosedElements()).get(0);
    VariableElement param = testMethod.getParameters().get(0);
    Name paramName = param.getSimpleName();

    if (!paramName.contentEquals("p")) {
        throw new AssertionError("Wrong parameter name: " + paramName);
    }
}
 
Example 6
Source File: EnumSaveStatementWriter.java    From postman with MIT License 5 votes vote down vote up
@Override
public void writeFieldWriteStatement(VariableElement field, JavaWriter writer)
        throws IOException {
    Name fieldName = field.getSimpleName();
    writer.beginControlFlow(String.format("if (object.%s != null)", fieldName));
    writer.emitStatement("bundle.putString(\"%s\", object.%s.name())", fieldName, fieldName);
    writer.endControlFlow();
}
 
Example 7
Source File: AutoCodecProcessor.java    From bazel with Apache License 2.0 5 votes vote down vote up
private MethodSpec buildSerializeMethodWithInstantiator(
    TypeElement encodedType, List<? extends VariableElement> fields, AutoCodec annotation)
    throws SerializationProcessingFailedException {
  MethodSpec.Builder serializeBuilder =
      AutoCodecUtil.initializeSerializeMethodBuilder(encodedType, annotation, env);
  for (VariableElement parameter : fields) {
    Optional<FieldValueAndClass> hasField =
        getFieldByNameRecursive(encodedType, parameter.getSimpleName().toString());
    if (hasField.isPresent()) {
      if (findRelationWithGenerics(hasField.get().value.asType(), parameter.asType())
          == Relation.UNRELATED_TO) {
        throw new SerializationProcessingFailedException(
            parameter,
            "%s: parameter %s's type %s is unrelated to corresponding field type %s",
            encodedType.getQualifiedName(),
            parameter.getSimpleName(),
            parameter.asType(),
            hasField.get().value.asType());
      }
      TypeKind typeKind = parameter.asType().getKind();
      serializeBuilder.addStatement(
          "$T unsafe_$L = ($T) $T.getInstance().get$L(input, $L_offset)",
          sanitizeTypeParameter(parameter.asType(), env),
          parameter.getSimpleName(),
          sanitizeTypeParameter(parameter.asType(), env),
          UnsafeProvider.class,
          typeKind.isPrimitive() ? firstLetterUpper(typeKind.toString().toLowerCase()) : "Object",
          parameter.getSimpleName());
          marshallers.writeSerializationCode(
              new Marshaller.Context(
                  serializeBuilder, parameter.asType(), "unsafe_" + parameter.getSimpleName()));
    } else {
      addSerializeParameterWithGetter(encodedType, parameter, serializeBuilder);
    }
  }
  return serializeBuilder.build();
}
 
Example 8
Source File: Gen.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
Example 9
Source File: Gen.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
Example 10
Source File: JNIWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f) {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    Assert.check(f.getModifiers().contains(Modifier.STATIC));

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }

            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }

    return null;
}
 
Example 11
Source File: JNIWriter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f) {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    Assert.check(f.getModifiers().contains(Modifier.STATIC));

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }

            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }

    return null;
}
 
Example 12
Source File: LLNI.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
Example 13
Source File: PatchDtoGenerator.java    From pnc with Apache License 2.0 4 votes vote down vote up
private void generatePatchClass(TypeElement dto) throws IOException {
    String dtoName = dto.getSimpleName().toString();
    String patchBuilderClassName = dtoName + "PatchBuilder";

    List<MethodSpec> methods = new ArrayList<>();

    // DTO fields
    List<VariableElement> fields = ElementFilter.fieldsIn(dto.getEnclosedElements());

    // Ref fields
    Element dtoRef = ((DeclaredType) dto.getSuperclass()).asElement();
    fields.addAll(ElementFilter.fieldsIn(dtoRef.getEnclosedElements()));

    for (VariableElement dtoField : fields) {
        Name fieldName = dtoField.getSimpleName();
        PatchSupport annotation = dtoField.getAnnotation(PatchSupport.class);
        if (annotation == null) {
            logger.info("Skipping DTO field " + fieldName);
            continue;
        }
        logger.info("Processing DTO field " + fieldName);

        for (PatchSupport.Operation operation : annotation.value()) {
            if (operation.equals(PatchSupport.Operation.ADD)) {
                createAddMethod(methods, dtoField, patchBuilderClassName);
            } else if (operation.equals(PatchSupport.Operation.REMOVE)) {
                createRemoveMethod(methods, dtoField, patchBuilderClassName);
            } else if (operation.equals(PatchSupport.Operation.REPLACE)) {
                createReplaceMethod(methods, dtoField, patchBuilderClassName);
            }
        }

    }

    MethodSpec constructor = MethodSpec.constructorBuilder()
            .addModifiers(Modifier.PUBLIC)
            .addStatement("super(org.jboss.pnc.dto." + dtoName + ".class)")
            .build();

    TypeSpec javaPatchClass = TypeSpec.classBuilder(patchBuilderClassName)
            .addModifiers(Modifier.PUBLIC)
            .addMethod(constructor)
            .addMethods(methods)
            .superclass(
                    ParameterizedTypeName.get(
                            ClassName.get("org.jboss.pnc.client.patch", "PatchBase"),
                            ClassName.get("org.jboss.pnc.client.patch", patchBuilderClassName),
                            TypeName.get(dto.asType())))
            .build();

    JavaFile.builder("org.jboss.pnc.client.patch", javaPatchClass).build().writeTo(processingEnv.getFiler());
}
 
Example 14
Source File: JNIWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f) {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    Assert.check(f.getModifiers().contains(Modifier.STATIC));

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }

            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }

    return null;
}
 
Example 15
Source File: Gen.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
Example 16
Source File: LLNI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}
 
Example 17
Source File: Gen.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f)
        throws Util.Exit {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    if (!f.getModifiers().contains(Modifier.STATIC))
        util.bug("tried.to.define.non.static");

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }
            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }
    return null;
}
 
Example 18
Source File: JNIWriter.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f) {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    Assert.check(f.getModifiers().contains(Modifier.STATIC));

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }

            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }

    return null;
}
 
Example 19
Source File: JNIWriter.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
protected String defineForStatic(TypeElement c, VariableElement f) {
    CharSequence cnamedoc = c.getQualifiedName();
    CharSequence fnamedoc = f.getSimpleName();

    String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
    String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);

    Assert.check(f.getModifiers().contains(Modifier.STATIC));

    if (f.getModifiers().contains(Modifier.FINAL)) {
        Object value = null;

        value = f.getConstantValue();

        if (value != null) { /* so it is a ConstantExpression */
            String constString = null;
            if ((value instanceof Integer)
                || (value instanceof Byte)
                || (value instanceof Short)) {
                /* covers byte, short, int */
                constString = value.toString() + "L";
            } else if (value instanceof Boolean) {
                constString = ((Boolean) value) ? "1L" : "0L";
            } else if (value instanceof Character) {
                Character ch = (Character) value;
                constString = String.valueOf(((int) ch) & 0xffff) + "L";
            } else if (value instanceof Long) {
                // Visual C++ supports the i64 suffix, not LL.
                if (isWindows)
                    constString = value.toString() + "i64";
                else
                    constString = value.toString() + "LL";
            } else if (value instanceof Float) {
                /* bug for bug */
                float fv = ((Float)value).floatValue();
                if (Float.isInfinite(fv))
                    constString = ((fv < 0) ? "-" : "") + "Inff";
                else
                    constString = value.toString() + "f";
            } else if (value instanceof Double) {
                /* bug for bug */
                double d = ((Double)value).doubleValue();
                if (Double.isInfinite(d))
                    constString = ((d < 0) ? "-" : "") + "InfD";
                else
                    constString = value.toString();
            }

            if (constString != null) {
                StringBuilder s = new StringBuilder("#undef ");
                s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
                s.append("#define "); s.append(cname); s.append("_");
                s.append(fname); s.append(" "); s.append(constString);
                return s.toString();
            }

        }
    }

    return null;
}
 
Example 20
Source File: LLNI.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected String addStaticStructMember(VariableElement field, String cname) {
    String res = null;
    Object exp = null;

    if (!field.getModifiers().contains(Modifier.STATIC))
        return res;
    if (!field.getModifiers().contains(Modifier.FINAL))
        return res;

    exp = field.getConstantValue();

    if (exp != null) {
        /* Constant. */

        String     cn     = cname + "_" + field.getSimpleName();
        String     suffix = null;
        long           val = 0;
        /* Can only handle int, long, float, and double fields. */
        if (exp instanceof Byte
            || exp instanceof Short
            || exp instanceof Integer) {
            suffix = "L";
            val = ((Number)exp).intValue();
        }
        else if (exp instanceof Long) {
            // Visual C++ supports the i64 suffix, not LL
            suffix = isWindows ? "i64" : "LL";
            val = ((Long)exp).longValue();
        }
        else if (exp instanceof Float)  suffix = "f";
        else if (exp instanceof Double) suffix = "";
        else if (exp instanceof Character) {
            suffix = "L";
            Character ch = (Character) exp;
            val = ((int) ch) & 0xffff;
        }
        if (suffix != null) {
            // Some compilers will generate a spurious warning
            // for the integer constants for Integer.MIN_VALUE
            // and Long.MIN_VALUE so we handle them specially.
            if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
                (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn
                    + " (" + (val + 1) + suffix + "-1)" + lineSep;
            } else if (suffix.equals("L") || suffix.endsWith("LL")) {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + val + suffix + lineSep;
            } else {
                res = "    #undef  " + cn + lineSep
                    + "    #define " + cn + " " + exp + suffix + lineSep;
            }
        }
    }
    return res;
}