/** @see {@link #fullyQualifiedName(IMethodBinding)}. */ public static Option<String> fullyQualifiedName(IVariableBinding field) { ITypeBinding declaring_class = field.getDeclaringClass(); String fq_type_name = fqTypeName(declaring_class); return fq_type_name == null ? Option.<String>none() : Option.some(removeTypeParam(fq_type_name) + "." + field.getName()); }
private void printAnnotationValue(AST ast, Object value) { if (value == null) { print("nil"); } else if (value instanceof IVariableBinding) { IVariableBinding var = (IVariableBinding) value; ITypeBinding declaringClass = var.getDeclaringClass(); printf("[%s %s]", NameTable.getFullName(declaringClass), var.getName()); } else if (value instanceof ITypeBinding) { ITypeBinding type = (ITypeBinding) value; printf("[[%s class] getClass]", NameTable.getFullName(type)); } else if (value instanceof String) { StringLiteral node = ast.newStringLiteral(); node.setLiteralValue((String) value); print(StatementGenerator.generateStringLiteral(node)); } else if (value instanceof Number || value instanceof Character || value instanceof Boolean) { print(value.toString()); } else if (value.getClass().isArray()) { print("[IOSObjectArray arrayWithObjects:(id[]) { "); Object[] array = (Object[]) value; for (int i = 0; i < array.length; i++) { if (i > 0) { print(", "); } printAnnotationValue(ast, array[i]); } printf(" } count:%d type:[[NSObject class] getClass]]", array.length); } else { assert false : "unknown annotation value type"; } }
public static boolean isPrimitiveConstant(IVariableBinding binding) { return isFinal(binding) && binding.getType().isPrimitive() && binding.getConstantValue() != null // Exclude local variables declared final. && binding.getDeclaringClass() != null; }
public URI getFullURI(IVariableBinding binding) { SegmentSequence.Builder builder = SegmentSequence.newBuilder(""); URI uri = getFullURI(binding.getDeclaringClass(), builder); builder.append("."); builder.append(binding.getName()); return uri.appendFragment(builder.toString()); }
private boolean hasAddedStaticImport(SimpleName name) { IBinding binding = name.resolveBinding(); if (binding instanceof IVariableBinding) { IVariableBinding variable = (IVariableBinding) binding; return hasAddedStaticImport( variable.getDeclaringClass().getQualifiedName(), variable.getName(), true); } else if (binding instanceof IMethodBinding) { IMethodBinding method = (IMethodBinding) binding; return hasAddedStaticImport( method.getDeclaringClass().getQualifiedName(), method.getName(), false); } return false; }
private void handleFieldChecks(Name node, IVariableBinding binding) { // add field access check checks.add(new FieldAccessCheck(file, jdtTypingProvider, bridge(node), binding)); // add assignment check for final fields if (Modifier.isFinal((binding).getModifiers())) { ITypeBinding declClass = binding.getDeclaringClass(); if (declClass != null && declClass.isFromSource()) checks.add(new FieldAssignmentCheck(file, jdtTypingProvider, bridge(node), binding)); } }
private void possibleStaticImportFound(Name name) { if (fStaticImports == null || fASTRoot == null) { return; } while (name.isQualifiedName()) { name = ((QualifiedName) name).getQualifier(); } if (!isAffected(name)) { return; } IBinding binding = name.resolveBinding(); SimpleName simpleName = (SimpleName) name; if (binding == null || binding instanceof ITypeBinding || !Modifier.isStatic(binding.getModifiers()) || simpleName.isDeclaration()) { return; } if (binding instanceof IVariableBinding) { IVariableBinding varBinding = (IVariableBinding) binding; if (varBinding.isField()) { varBinding = varBinding.getVariableDeclaration(); ITypeBinding declaringClass = varBinding.getDeclaringClass(); if (declaringClass != null && !declaringClass.isLocal()) { if (new ScopeAnalyzer(fASTRoot) .isDeclaredInScope( varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY)) return; fStaticImports.add(simpleName); } } } else if (binding instanceof IMethodBinding) { IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration(); ITypeBinding declaringClass = methodBinding.getDeclaringClass(); if (declaringClass != null && !declaringClass.isLocal()) { if (new ScopeAnalyzer(fASTRoot) .isDeclaredInScope( methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY)) return; fStaticImports.add(simpleName); } } }
public void applyRemoves(ImportRewrite importRewrite) { IBinding[] bindings = getImportsToRemove(); for (int i = 0; i < bindings.length; i++) { if (bindings[i] instanceof ITypeBinding) { ITypeBinding typeBinding = (ITypeBinding) bindings[i]; importRewrite.removeImport(typeBinding.getTypeDeclaration().getQualifiedName()); } else if (bindings[i] instanceof IMethodBinding) { IMethodBinding binding = (IMethodBinding) bindings[i]; importRewrite.removeStaticImport( binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName()); } else if (bindings[i] instanceof IVariableBinding) { IVariableBinding binding = (IVariableBinding) bindings[i]; importRewrite.removeStaticImport( binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName()); } } }
private boolean fieldCanBeFinal( VariableDeclarationFragment fragment, IVariableBinding binding) { if (Modifier.isStatic(((FieldDeclaration) fragment.getParent()).getModifiers())) return false; if (!fWrittenVariables.containsKey(binding)) { // variable is not written if (fragment.getInitializer() == null) { // variable is not initialized return false; } else { return true; } } if (fragment.getInitializer() != null) // variable is initialized and written return false; ITypeBinding declaringClass = binding.getDeclaringClass(); if (declaringClass == null) return false; ArrayList writes = (ArrayList) fWrittenVariables.get(binding); if (!isWrittenInTypeConstructors(writes, declaringClass)) return false; HashSet writingConstructorBindings = new HashSet(); ArrayList writingConstructors = new ArrayList(); for (int i = 0; i < writes.size(); i++) { SimpleName name = (SimpleName) writes.get(i); MethodDeclaration constructor = getWritingConstructor(name); if (writingConstructors.contains( constructor)) // variable is written twice or more in constructor return false; if (canReturn(constructor)) return false; writingConstructors.add(constructor); IMethodBinding constructorBinding = constructor.resolveBinding(); if (constructorBinding == null) return false; writingConstructorBindings.add(constructorBinding); } for (int i = 0; i < writingConstructors.size(); i++) { MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(i); if (callsWritingConstructor( constructor, writingConstructorBindings)) // writing constructor calls other writing constructor return false; } MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(0); TypeDeclaration typeDecl = (TypeDeclaration) ASTNodes.getParent(constructor, TypeDeclaration.class); if (typeDecl == null) return false; MethodDeclaration[] methods = typeDecl.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].isConstructor()) { IMethodBinding methodBinding = methods[i].resolveBinding(); if (methodBinding == null) return false; if (!writingConstructorBindings.contains(methodBinding)) { if (!callsWritingConstructor( methods[i], writingConstructorBindings)) // non writing constructor does not call a writing // constructor return false; } } } return true; }
public static boolean isWeakReference(IVariableBinding var) { return hasNamedAnnotation(var, "Weak") || hasWeakPropertyAttribute(var) || var.getName().startsWith("this$") && hasNamedAnnotation(var.getDeclaringClass(), "WeakOuter"); }