Java Code Examples for javax.lang.model.type.ArrayType#getComponentType()

The following examples show how to use javax.lang.model.type.ArrayType#getComponentType() . 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: Names.java    From doma with Apache License 2.0 6 votes vote down vote up
public Name createExternalDomainName(TypeMirror externalDomainType) {
  assertNotNull(externalDomainType);
  ArrayType arrayType = ctx.getMoreTypes().toArrayType(externalDomainType);
  if (arrayType != null) {
    TypeMirror componentType = arrayType.getComponentType();
    TypeElement componentElement = ctx.getMoreTypes().toTypeElement(componentType);
    if (componentElement == null) {
      throw new AptIllegalStateException(componentType.toString());
    }
    Name binaryName = ctx.getMoreElements().getBinaryName(componentElement);
    return ctx.getMoreElements().getName(binaryName + EXTERNAL_DOMAIN_TYPE_ARRAY_SUFFIX);
  }
  TypeElement domainElement = ctx.getMoreTypes().toTypeElement(externalDomainType);
  if (domainElement == null) {
    throw new AptIllegalStateException(externalDomainType.toString());
  }
  return ctx.getMoreElements().getBinaryName(domainElement);
}
 
Example 2
Source File: ApNavigator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror getComponentType(TypeMirror t) {
    if (isArray(t)) {
        ArrayType at = (ArrayType) t;
        return at.getComponentType();
    }

    throw new IllegalArgumentException();
}
 
Example 3
Source File: ApNavigator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror getComponentType(TypeMirror t) {
    if (isArray(t)) {
        ArrayType at = (ArrayType) t;
        return at.getComponentType();
    }

    throw new IllegalArgumentException();
}
 
Example 4
Source File: ApNavigator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 5
Source File: TypeUtil.java    From pocketknife with Apache License 2.0 5 votes vote down vote up
private boolean needToCastArrayType(ArrayType type) throws InvalidTypeException {
    TypeMirror componentType = type.getComponentType();
    if (isPrimitive(componentType)) {
        return false;
    }
    try {
        return needToCastAggregateType(componentType);
    } catch (InvalidTypeException e) {
        throw new InvalidTypeException(type);
    }
}
 
Example 6
Source File: ApNavigator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror getComponentType(TypeMirror t) {
    if (isArray(t)) {
        ArrayType at = (ArrayType) t;
        return at.getComponentType();
    }

    throw new IllegalArgumentException();
}
 
Example 7
Source File: ApNavigator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 8
Source File: ArrayRewriter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private MethodInvocation newSingleDimensionArrayInvocation(
    ArrayType arrayType, Expression dimensionExpr, boolean retainedResult) {
  TypeMirror componentType = arrayType.getComponentType();
  TypeElement iosArrayElement = typeUtil.getIosArray(componentType);
  boolean isPrimitive = componentType.getKind().isPrimitive();

  String selector = (retainedResult ? "newArray" : "array") + "WithLength:"
      + (isPrimitive ? "" : "type:");
  GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(
      selector, iosArrayElement.asType(), iosArrayElement)
      .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
  methodElement.addParameter(GeneratedVariableElement.newParameter(
      "length", typeUtil.getInt(), methodElement));
  if (!isPrimitive) {
    methodElement.addParameter(GeneratedVariableElement.newParameter(
        "type", TypeUtil.IOS_CLASS.asType(), methodElement));
  }
  MethodInvocation invocation = new MethodInvocation(
      new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));

  // Add the array length argument.
  invocation.addArgument(dimensionExpr.copy());

  // Add the type argument for object arrays.
  if (!isPrimitive) {
    invocation.addArgument(new TypeLiteral(componentType, typeUtil));
  }

  return invocation;
}
 
Example 9
Source File: ApNavigator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 10
Source File: ApNavigator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 11
Source File: ApNavigator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 12
Source File: SourceUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the method is a main method
 * @param method to be checked
 * @return true when the method is a main method
 */
public static boolean isMainMethod (final ExecutableElement method) {
    if (!"main".contentEquals(method.getSimpleName())) {                //NOI18N
        return false;
    }
    long flags = ((Symbol.MethodSymbol)method).flags();                 //faster
    if (((flags & Flags.PUBLIC) == 0) || ((flags & Flags.STATIC) == 0)) {
        return false;
    }
    if (method.getReturnType().getKind() != TypeKind.VOID) {
        return false;
    }
    List<? extends VariableElement> params = method.getParameters();
    if (params.size() != 1) {
        return false;
    }
    TypeMirror param = params.get(0).asType();
    if (param.getKind() != TypeKind.ARRAY) {
        return false;
    }
    ArrayType array = (ArrayType) param;
    TypeMirror compound = array.getComponentType();
    if (compound.getKind() != TypeKind.DECLARED) {
        return false;
    }
    return "java.lang.String".contentEquals(((TypeElement)((DeclaredType)compound).asElement()).getQualifiedName());   //NOI18N
}
 
Example 13
Source File: ApNavigator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror getComponentType(TypeMirror t) {
    if (isArray(t)) {
        ArrayType at = (ArrayType) t;
        return at.getComponentType();
    }

    throw new IllegalArgumentException();
}
 
Example 14
Source File: ApNavigator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 15
Source File: ArrayRewriter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private MethodInvocation newInitializedArrayInvocation(
    ArrayType arrayType, List<Expression> elements, boolean retainedResult) {
  TypeMirror componentType = arrayType.getComponentType();
  TypeElement iosArrayElement = typeUtil.getIosArray(componentType);

  GeneratedExecutableElement methodElement = GeneratedExecutableElement.newMethodWithSelector(
      getInitializeSelector(componentType, retainedResult), iosArrayElement.asType(),
      iosArrayElement)
      .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
  methodElement.addParameter(GeneratedVariableElement.newParameter(
      "values", new PointerType(componentType), methodElement));
  methodElement.addParameter(GeneratedVariableElement.newParameter(
      "count", typeUtil.getInt(), methodElement));
  if (!componentType.getKind().isPrimitive()) {
    methodElement.addParameter(GeneratedVariableElement.newParameter(
        "type", TypeUtil.IOS_CLASS.asType(), methodElement));
  }
  MethodInvocation invocation = new MethodInvocation(
      new ExecutablePair(methodElement), arrayType, new SimpleName(iosArrayElement));

  // Create the array initializer and add it as the first parameter.
  ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
  for (Expression element : elements) {
    arrayInit.addExpression(element.copy());
  }
  invocation.addArgument(arrayInit);

  // Add the array size parameter.
  invocation.addArgument(
      NumberLiteral.newIntLiteral(arrayInit.getExpressions().size(), typeUtil));

  // Add the type argument for object arrays.
  if (!componentType.getKind().isPrimitive()) {
    invocation.addArgument(new TypeLiteral(componentType, typeUtil));
  }

  return invocation;
}
 
Example 16
Source File: ApNavigator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 17
Source File: ApNavigator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror getComponentType(TypeMirror t) {
    if (isArray(t)) {
        ArrayType at = (ArrayType) t;
        return at.getComponentType();
    }

    throw new IllegalArgumentException();
}
 
Example 18
Source File: ExternalDomainMetaFactory.java    From doma with Apache License 2.0 4 votes vote down vote up
private void doDomainType(
    TypeElement converterElement, TypeMirror domainType, ExternalDomainMeta meta) {
  meta.setType(domainType);

  ArrayType arrayType = ctx.getMoreTypes().toArrayType(domainType);
  if (arrayType != null) {
    TypeMirror componentType = arrayType.getComponentType();
    if (componentType.getKind() == TypeKind.ARRAY) {
      throw new AptException(Message.DOMA4447, converterElement, new Object[] {});
    }
    TypeElement componentElement = ctx.getMoreTypes().toTypeElement(componentType);
    if (componentElement == null) {
      throw new AptIllegalStateException(componentType.toString());
    }
    if (!componentElement.getTypeParameters().isEmpty()) {
      throw new AptException(Message.DOMA4448, converterElement, new Object[] {});
    }
    return;
  }

  TypeElement domainElement = ctx.getMoreTypes().toTypeElement(domainType);
  if (domainElement == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  if (domainElement.getNestingKind().isNested()) {
    validateEnclosingElement(domainElement);
  }
  PackageElement pkgElement = ctx.getMoreElements().getPackageOf(domainElement);
  if (pkgElement.isUnnamed()) {
    throw new AptException(
        Message.DOMA4197, converterElement, new Object[] {domainElement.getQualifiedName()});
  }
  DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(domainType);
  if (declaredType == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  for (TypeMirror typeArg : declaredType.getTypeArguments()) {
    if (typeArg.getKind() != TypeKind.WILDCARD) {
      throw new AptException(
          Message.DOMA4203, converterElement, new Object[] {domainElement.getQualifiedName()});
    }
  }
  meta.setTypeElement(domainElement);
  TypeParametersDef typeParametersDef = ctx.getMoreElements().getTypeParametersDef(domainElement);
  meta.setTypeParametersDef(typeParametersDef);
}
 
Example 19
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 4 votes vote down vote up
private void parseBindViews(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
    Set<TypeElement> erasedTargetNames) {
  TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

  // Start by verifying common generated code restrictions.
  boolean hasError = isInaccessibleViaGeneratedCode(BindViews.class, "fields", element)
      || isBindingInWrongPackage(BindViews.class, element);

  // Verify that the type is a List or an array.
  TypeMirror elementType = element.asType();
  String erasedType = doubleErasure(elementType);
  TypeMirror viewType = null;
  FieldCollectionViewBinding.Kind kind = null;
  if (elementType.getKind() == TypeKind.ARRAY) {
    ArrayType arrayType = (ArrayType) elementType;
    viewType = arrayType.getComponentType();
    kind = FieldCollectionViewBinding.Kind.ARRAY;
  } else if (LIST_TYPE.equals(erasedType)) {
    DeclaredType declaredType = (DeclaredType) elementType;
    List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
    if (typeArguments.size() != 1) {
      error(element, "@%s List must have a generic component. (%s.%s)",
          BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    } else {
      viewType = typeArguments.get(0);
    }
    kind = FieldCollectionViewBinding.Kind.LIST;
  } else {
    error(element, "@%s must be a List or array. (%s.%s)", BindViews.class.getSimpleName(),
        enclosingElement.getQualifiedName(), element.getSimpleName());
    hasError = true;
  }
  if (viewType != null && viewType.getKind() == TypeKind.TYPEVAR) {
    TypeVariable typeVariable = (TypeVariable) viewType;
    viewType = typeVariable.getUpperBound();
  }

  // Verify that the target type extends from View.
  if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) {
    if (viewType.getKind() == TypeKind.ERROR) {
      note(element, "@%s List or array with unresolved type (%s) "
              + "must elsewhere be generated as a View or interface. (%s.%s)",
          BindViews.class.getSimpleName(), viewType, enclosingElement.getQualifiedName(),
          element.getSimpleName());
    } else {
      error(element, "@%s List or array type must extend from View or be an interface. (%s.%s)",
          BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    }
  }

  // Assemble information on the field.
  String name = element.getSimpleName().toString();
  int[] ids = element.getAnnotation(BindViews.class).value();
  if (ids.length == 0) {
    error(element, "@%s must specify at least one ID. (%s.%s)", BindViews.class.getSimpleName(),
        enclosingElement.getQualifiedName(), element.getSimpleName());
    hasError = true;
  }

  Integer duplicateId = findDuplicate(ids);
  if (duplicateId != null) {
    error(element, "@%s annotation contains duplicate ID %d. (%s.%s)",
        BindViews.class.getSimpleName(), duplicateId, enclosingElement.getQualifiedName(),
        element.getSimpleName());
    hasError = true;
  }

  if (hasError) {
    return;
  }

  TypeName type = TypeName.get(requireNonNull(viewType));
  boolean required = isFieldRequired(element);

  BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
  builder.addFieldCollection(new FieldCollectionViewBinding(name, type, requireNonNull(kind),
      new ArrayList<>(elementToIds(element, BindViews.class, ids).values()), required));

  erasedTargetNames.add(enclosingElement);
}
 
Example 20
Source File: EnhancedForRewriter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void handleArrayIteration(EnhancedForStatement node) {
  Expression expression = node.getExpression();
  ArrayType expressionType = (ArrayType) expression.getTypeMirror();
  VariableElement loopVariable = node.getParameter().getVariableElement();
  TypeMirror componentType = expressionType.getComponentType();
  TypeElement iosArrayType = typeUtil.getIosArray(componentType);
  TypeMirror bufferType = new PointerType(componentType);
  VariableElement arrayVariable = GeneratedVariableElement.newLocalVar(
      "a__", expressionType, null);
  VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null)
      .setTypeQualifiers("const*");
  VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null)
      .setTypeQualifiers("const*");
  VariableElement bufferField = GeneratedVariableElement.newField(
      "buffer", bufferType, iosArrayType)
      .addModifiers(Modifier.PUBLIC);
  VariableElement sizeField = GeneratedVariableElement.newField(
      "size", typeUtil.getInt(), iosArrayType)
      .addModifiers(Modifier.PUBLIC);

  VariableDeclarationStatement arrayDecl =
      new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
  FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
  VariableDeclarationStatement bufferDecl =
      new VariableDeclarationStatement(bufferVariable, bufferAccess);
  InfixExpression endInit = new InfixExpression(
      bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable),
      new FieldAccess(sizeField, new SimpleName(arrayVariable)));
  VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);

  WhileStatement loop = new WhileStatement();
  loop.setExpression(new InfixExpression(
      typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable),
      new SimpleName(endVariable)));
  Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
  loop.setBody(newLoopBody);
  newLoopBody.addStatement(0, new VariableDeclarationStatement(
      loopVariable, new PrefixExpression(
          componentType, PrefixExpression.Operator.DEREFERENCE,
          new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));

  Block block = new Block();
  List<Statement> stmts = block.getStatements();
  stmts.add(arrayDecl);
  stmts.add(bufferDecl);
  stmts.add(endDecl);
  stmts.add(loop);
  replaceLoop(node, block, loop);
}