public ITypeBinding resolveJavaType(String name) { ITypeBinding result = javaBindingMap.get(name); if (result == null) { result = ast.resolveWellKnownType(name); } return result; }
/** * Checks if the assignment needs a downcast and inserts it if necessary * * @param expression the right hand-side * @param expressionType the type of the right hand-side. Can be null * @param ast the AST * @param variableType the Type of the variable the expression will be assigned to * @param is50OrHigher if <code>true</code> java 5.0 code will be assumed * @return the casted expression if necessary */ private static Expression createNarrowCastIfNessecary( Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) { PrimitiveType castTo = null; if (variableType.isEqualTo(expressionType)) return expression; // no cast for same type if (is50OrHigher) { if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.CHAR); if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.BYTE); if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.SHORT); } if (ast.resolveWellKnownType("char").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.CHAR); if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.BYTE); if (ast.resolveWellKnownType("short").isEqualTo(variableType)) // $NON-NLS-1$ castTo = ast.newPrimitiveType(PrimitiveType.SHORT); if (castTo != null) { CastExpression cast = ast.newCastExpression(); if (ASTNodes.needsParentheses(expression)) { ParenthesizedExpression parenthesized = ast.newParenthesizedExpression(); parenthesized.setExpression(expression); cast.setExpression(parenthesized); } else cast.setExpression(expression); cast.setType(castTo); return cast; } return expression; }
private Type evaluateVariableType( AST ast, ImportRewrite imports, ImportRewriteContext importRewriteContext, IBinding targetContext) { if (fOriginalNode.getParent() instanceof MethodInvocation) { MethodInvocation parent = (MethodInvocation) fOriginalNode.getParent(); if (parent.getExpression() == fOriginalNode) { // _x_.foo() -> guess qualifier type by looking for a type with method 'foo' ITypeBinding[] bindings = ASTResolving.getQualifierGuess( fOriginalNode.getRoot(), parent.getName().getIdentifier(), parent.arguments(), targetContext); if (bindings.length > 0) { for (int i = 0; i < bindings.length; i++) { addLinkedPositionProposal(KEY_TYPE, bindings[i]); } return imports.addImport(bindings[0], ast, importRewriteContext); } } } ITypeBinding binding = ASTResolving.guessBindingForReference(fOriginalNode); if (binding != null) { if (binding.isWildcardType()) { binding = ASTResolving.normalizeWildcardType(binding, isVariableAssigned(), ast); if (binding == null) { // only null binding applies binding = ast.resolveWellKnownType("java.lang.Object"); // $NON-NLS-1$ } } if (isVariableAssigned()) { ITypeBinding[] typeProposals = ASTResolving.getRelaxingTypes(ast, binding); for (int i = 0; i < typeProposals.length; i++) { addLinkedPositionProposal(KEY_TYPE, typeProposals[i]); } } return imports.addImport(binding, ast, importRewriteContext); } // no binding, find type AST node instead -> ABC a= x-> use 'ABC' as is Type type = ASTResolving.guessTypeForReference(ast, fOriginalNode); if (type != null) { return type; } if (fVariableKind == CONST_FIELD) { return ast.newSimpleType(ast.newSimpleName("String")); // $NON-NLS-1$ } return ast.newSimpleType(ast.newSimpleName("Object")); // $NON-NLS-1$ }
public Types(AST ast) { this.ast = ast; // Find core java types. javaObjectType = ast.resolveWellKnownType("java.lang.Object"); javaClassType = ast.resolveWellKnownType("java.lang.Class"); javaCloneableType = ast.resolveWellKnownType("java.lang.Cloneable"); javaStringType = ast.resolveWellKnownType("java.lang.String"); javaThrowableType = ast.resolveWellKnownType("java.lang.Throwable"); javaVoidType = ast.resolveWellKnownType("java.lang.Void"); ITypeBinding binding = ast.resolveWellKnownType("java.lang.Integer"); javaNumberType = binding.getSuperclass(); // Create core IOS types. NSCopying = mapIOSType(IOSTypeBinding.newInterface("NSCopying", javaCloneableType)); NSObject = mapIOSType(IOSTypeBinding.newClass("NSObject", javaObjectType)); NSNumber = mapIOSType(IOSTypeBinding.newClass("NSNumber", javaNumberType, NSObject)); NSString = mapIOSType(IOSTypeBinding.newClass("NSString", javaStringType, NSObject)); NSException = mapIOSType(IOSTypeBinding.newClass("NSException", javaThrowableType, NSObject)); IOSClass = mapIOSType(IOSTypeBinding.newUnmappedClass("IOSClass")); mapIOSType(IOSTypeBinding.newUnmappedClass("NSZone")); idType = mapIOSType(IOSTypeBinding.newUnmappedClass("id")); initializeArrayTypes(); initializeTypeMap(); initializeCommonJavaTypes(); populatePrimitiveAndWrapperTypeMaps(); ITypeBinding voidType = ast.resolveWellKnownType("void"); // Commonly used methods. retainMethod = IOSMethodBinding.newMethod(NameTable.RETAIN_METHOD, Modifier.PUBLIC, idType, NSObject); releaseMethod = IOSMethodBinding.newMethod(NameTable.RELEASE_METHOD, Modifier.PUBLIC, voidType, NSObject); autoreleaseMethod = IOSMethodBinding.newMethod(NameTable.AUTORELEASE_METHOD, Modifier.PUBLIC, idType, NSObject); allocMethod = IOSMethodBinding.newMethod(NameTable.ALLOC_METHOD, Modifier.PUBLIC, idType, NSObject); deallocMethod = IOSMethodBinding.newMethod(NameTable.DEALLOC_METHOD, Modifier.PUBLIC, idType, NSObject); }
public ITypeBinding getLocalRefType() { if (localRefType == null) { ITypeBinding objectType = ast.resolveWellKnownType("java.lang.Object"); GeneratedTypeBinding refType = GeneratedTypeBinding.newTypeBinding( "com.google.j2objc.util.ScopedLocalRef", objectType, false); GeneratedVariableBinding varBinding = new GeneratedVariableBinding( "var", Modifier.PUBLIC, objectType, true, false, refType, null); refType.addField(varBinding); GeneratedMethodBinding constructor = GeneratedMethodBinding.newConstructor(refType, Modifier.PUBLIC, this); constructor.addParameter(objectType); refType.addMethod(constructor); localRefType = refType; } return localRefType; }
private void initReturnType(ImportRewrite rewriter) { AST ast = fEnclosingBodyDeclaration.getAST(); fReturnType = null; fReturnTypeBinding = null; switch (fReturnKind) { case ACCESS_TO_LOCAL: VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration); fReturnType = ASTNodeFactory.newType( ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter)); if (declaration.resolveBinding() != null) { fReturnTypeBinding = declaration.resolveBinding().getType(); } break; case EXPRESSION: Expression expression = (Expression) getFirstSelectedNode(); if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding(); } else { fExpressionBinding = expression.resolveTypeBinding(); } if (fExpressionBinding != null) { if (fExpressionBinding.isNullType()) { getStatus() .addFatalError( RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression)); } else { ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast); if (normalizedBinding != null) { ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter); fReturnType = rewriter.addImport(normalizedBinding, ast, context); fReturnTypeBinding = normalizedBinding; } } } else { fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ getStatus() .addError( RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression)); } break; case RETURN_STATEMENT_VALUE: LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode()); if (enclosingLambdaExpr != null) { fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null); IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding(); fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null; } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) { fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2(); fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null; } break; default: fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ } if (fReturnType == null) { fReturnType = ast.newPrimitiveType(PrimitiveType.VOID); fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$ } }
/** Given a fully-qualified type name, return its binding. */ public ITypeBinding mapTypeName(String typeName) { ITypeBinding binding = ast.resolveWellKnownType(typeName); return mapType(binding); }
private void loadPrimitiveAndWrapperTypes(String primitiveName, String wrapperName) { ITypeBinding primitive = ast.resolveWellKnownType(primitiveName); ITypeBinding wrapper = ast.resolveWellKnownType(wrapperName); primitiveToWrapperTypes.put(primitive, wrapper); wrapperToPrimitiveTypes.put(wrapper, primitive); }
private void initializePrimitiveArray(String javaTypeName, String iosTypeName) { ITypeBinding javaType = ast.resolveWellKnownType(javaTypeName); IOSTypeBinding iosType = mapIOSType(IOSTypeBinding.newUnmappedClass(iosTypeName)); iosType.setHeader("IOSPrimitiveArray.h"); arrayBindingMap.put(javaType, iosType); }