下面列出了javax.lang.model.type.ArrayType#getComponentType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
public TypeMirror getComponentType(TypeMirror t) {
if (isArray(t)) {
ArrayType at = (ArrayType) t;
return at.getComponentType();
}
throw new IllegalArgumentException();
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
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;
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
public TypeMirror getComponentType(TypeMirror t) {
if (isArray(t)) {
ArrayType at = (ArrayType) t;
return at.getComponentType();
}
throw new IllegalArgumentException();
}
/**
* 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
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
public TypeMirror getComponentType(TypeMirror t) {
if (isArray(t)) {
ArrayType at = (ArrayType) t;
return at.getComponentType();
}
throw new IllegalArgumentException();
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
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;
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
public TypeMirror getComponentType(TypeMirror t) {
if (isArray(t)) {
ArrayType at = (ArrayType) t;
return at.getComponentType();
}
throw new IllegalArgumentException();
}
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);
}
}
public boolean isArrayButNotByteArray(TypeMirror t) {
if(!isArray(t))
return false;
ArrayType at = (ArrayType) t;
TypeMirror ct = at.getComponentType();
return !ct.equals(primitiveByte);
}
public TypeMirror getComponentType(TypeMirror t) {
if (isArray(t)) {
ArrayType at = (ArrayType) t;
return at.getComponentType();
}
throw new IllegalArgumentException();
}
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);
}
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);
}
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);
}