use of javax.lang.model.type.ArrayType in project j2objc by google.
the class ArrayRewriter method maybeRewriteArrayLength.
private void maybeRewriteArrayLength(Expression node, SimpleName name, Expression expr) {
TypeMirror exprType = expr.getTypeMirror();
if (name.getIdentifier().equals("length") && TypeUtil.isArray(exprType)) {
VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), typeUtil.getIosArray(((ArrayType) exprType).getComponentType()));
node.replaceWith(new FieldAccess(sizeField, TreeUtil.remove(expr)));
}
}
use of javax.lang.model.type.ArrayType in project j2objc by google.
the class SwitchRewriter method fixStringValue.
private void fixStringValue(SwitchStatement node) {
Expression expr = node.getExpression();
TypeMirror type = expr.getTypeMirror();
if (!typeUtil.isString(type)) {
return;
}
ArrayType arrayType = typeUtil.getArrayType(type);
ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
int idx = 0;
for (Statement stmt : node.getStatements()) {
if (stmt instanceof SwitchCase) {
SwitchCase caseStmt = (SwitchCase) stmt;
if (!caseStmt.isDefault()) {
arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
}
}
}
TypeMirror intType = typeUtil.getInt();
FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
node.setExpression(invocation);
}
use of javax.lang.model.type.ArrayType in project j2objc by google.
the class TypeUtil method getDimensions.
public static int getDimensions(ArrayType arrayType) {
int dimCount = 0;
TypeMirror t = arrayType;
while (t.getKind().equals(TypeKind.ARRAY)) {
dimCount++;
t = (((ArrayType) t).getComponentType());
}
return dimCount;
}
use of javax.lang.model.type.ArrayType in project immutables by immutables.
the class TypeStringProvider method caseType.
void caseType(TypeMirror type) {
if (ended) {
// to prevent additional recursive effects when using workaround
return;
}
switch(type.getKind()) {
case ERROR:
unresolvedTypeHasOccured = true;
//$FALL-THROUGH$
case DECLARED:
DeclaredType declaredType = (DeclaredType) type;
appendResolved(declaredType);
appendTypeArguments(type, declaredType);
break;
case ARRAY:
TypeMirror componentType = ((ArrayType) type).getComponentType();
int mark = buffer.length();
caseType(componentType);
cutTypeArgument(type, mark);
buffer.append('[').append(']');
break;
case WILDCARD:
WildcardType wildcard = (WildcardType) type;
@Nullable TypeMirror extendsBound = wildcard.getExtendsBound();
@Nullable TypeMirror superBound = wildcard.getSuperBound();
if (extendsBound != null) {
buffer.append("? extends ");
caseType(extendsBound);
} else if (superBound != null) {
buffer.append("? super ");
caseType(superBound);
} else {
buffer.append('?');
}
break;
case TYPEVAR:
if (allowedTypevars.length != 0) {
TypeVariable typeVariable = (TypeVariable) type;
String var = typeVariable.toString();
int indexOfVar = Arrays.asList(allowedTypevars).indexOf(var);
if (indexOfVar >= 0) {
if (typevarArguments != null) {
buffer.append(typevarArguments[indexOfVar]);
} else {
hasTypeVariables = true;
buffer.append(var);
}
break;
}
// If we don't have such parameter we consider this is the quirk
// that was witnessed in Eclipse, we let the code below deal with it.
}
// ended flag
if (tryToUseSourceAsAWorkaround()) {
ended = true;
break;
}
reporter.withElement(element).error("It is a compiler/annotation processing bug to receive type variable '%s' here." + " To avoid it — do not use not yet generated types in %s attribute", type, element.getSimpleName());
// just append as toString whatever we have
buffer.append(type);
break;
default:
buffer.append(type);
}
}
use of javax.lang.model.type.ArrayType in project immutables by immutables.
the class ValueAttribute method introspectType.
@Override
protected void introspectType() {
TypeMirror typeMirror = returnType;
// Special case for primitive Optional, may become a pattern for specialized types
if (typeKind.isOptionalSpecializedJdk()) {
typeParameters = ImmutableList.of(optionalSpecializedType());
// no delegation to introspect further
return;
}
if (isContainerType()) {
if (typeMirror.getKind() == TypeKind.DECLARED || typeMirror.getKind() == TypeKind.ERROR) {
DeclaredType declaredType = (DeclaredType) typeMirror;
List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
if (!typeArguments.isEmpty()) {
final TypeMirror typeArgument = typeArguments.get(0);
if (isSetType() && protoclass().environment().hasOrdinalModule()) {
this.generateOrdinalValueSet = new TypeIntrospectionBase() {
@Override
protected TypeMirror internalTypeMirror() {
return typeArgument;
}
@Override
protected TypeHierarchyCollector collectTypeHierarchy(TypeMirror typeMirror) {
TypeHierarchyCollector collector = containingType.createTypeHierarchyCollector(reporter, element);
collector.collectFrom(typeMirror);
return collector;
}
}.isOrdinalValue();
}
if (isMapType()) {
TypeMirror typeSecondArgument = typeArguments.get(1);
if (typeSecondArgument.getKind() == TypeKind.DECLARED) {
TypeElement typeElement = (TypeElement) ((DeclaredType) typeSecondArgument).asElement();
this.containedSecondaryTypeElement = typeElement;
}
}
typeMirror = typeArgument;
}
}
} else if (isArrayType()) {
arrayComponent = ((ArrayType) typeMirror).getComponentType();
typeMirror = arrayComponent;
}
if (typeMirror.getKind() == TypeKind.DECLARED) {
TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
this.containedTypeElement = typeElement;
}
introspectTypeMirror(typeMirror);
introspectSupertypes();
}
Aggregations