private boolean isEnum(final Type type) { return type.resolveBinding().isEnum(); }
public boolean isEnumNameMethodInvocation (MethodInvocation node) { return node.getName().getIdentifier().equals("name") && node.getExpression() != null && node.getExpression().resolveTypeBinding().isEnum(); }
public boolean isEnumOrdinalMethodInvocation (MethodInvocation node) { return node.getName().getIdentifier().equals("ordinal") && node.getExpression() != null && node.getExpression().resolveTypeBinding().isEnum(); }
private static boolean getMissingCaseStatementProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> proposals) { if (node instanceof SwitchCase) { node= node.getParent(); } if (!(node instanceof SwitchStatement)) return false; SwitchStatement switchStatement= (SwitchStatement)node; ITypeBinding expressionBinding= switchStatement.getExpression().resolveTypeBinding(); if (expressionBinding == null || !expressionBinding.isEnum()) return false; ArrayList<String> missingEnumCases= new ArrayList<>(); boolean hasDefault= LocalCorrectionsSubProcessor.evaluateMissingSwitchCases(expressionBinding, switchStatement.statements(), missingEnumCases); if (missingEnumCases.size() == 0 && hasDefault) return false; if (proposals == null) return true; LocalCorrectionsSubProcessor.createMissingCaseProposals(context, switchStatement, missingEnumCases, proposals); return true; }
private static boolean getMissingCaseStatementProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> proposals) { if (node instanceof SwitchCase) { node= node.getParent(); } if (!(node instanceof SwitchStatement)) return false; SwitchStatement switchStatement= (SwitchStatement)node; ITypeBinding expressionBinding= switchStatement.getExpression().resolveTypeBinding(); if (expressionBinding == null || !expressionBinding.isEnum()) return false; ArrayList<String> missingEnumCases= new ArrayList<>(); boolean hasDefault= LocalCorrectionsSubProcessor.evaluateMissingSwitchCases(expressionBinding, switchStatement.statements(), missingEnumCases); if (missingEnumCases.size() == 0 && hasDefault) return false; if (proposals == null) return true; LocalCorrectionsSubProcessor.createMissingCaseProposals(context, switchStatement, missingEnumCases, proposals); return true; }
private static boolean canReplace(IASTFragment fragment) { ASTNode node = fragment.getAssociatedNode(); ASTNode parent = node.getParent(); if (parent instanceof VariableDeclarationFragment) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent; if (node.equals(vdf.getName())) { return false; } } if (parent instanceof ExpressionStatement) { return false; } if (parent instanceof SwitchCase) { if (node instanceof Name) { Name name = (Name) node; ITypeBinding typeBinding = name.resolveTypeBinding(); if (typeBinding != null) { return !typeBinding.isEnum(); } } } return true; }
public boolean consumes(SemanticToken token) { // 1: match types SimpleName name= token.getNode(); ASTNode node= name.getParent(); int nodeType= node.getNodeType(); if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ENUM_DECLARATION) return false; while (nodeType == ASTNode.QUALIFIED_NAME) { node= node.getParent(); nodeType= node.getNodeType(); if (nodeType == ASTNode.IMPORT_DECLARATION) return false; } // 2: match enums IBinding binding= token.getBinding(); return binding instanceof ITypeBinding && ((ITypeBinding) binding).isEnum(); } }
@Override public boolean consumes(SemanticToken token) { // 1: match types SimpleName name = token.getNode(); ASTNode node = name.getParent(); int nodeType = node.getNodeType(); if (nodeType != ASTNode.METHOD_INVOCATION && nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ENUM_DECLARATION) { return false; } while (nodeType == ASTNode.QUALIFIED_NAME) { node = node.getParent(); nodeType = node.getNodeType(); if (nodeType == ASTNode.IMPORT_DECLARATION) { return false; } } // 2: match enums IBinding binding = token.getBinding(); return binding instanceof ITypeBinding && ((ITypeBinding) binding).isEnum(); } }
@Override public boolean consumes(SemanticToken token) { // 1: match types SimpleName name= token.getNode(); ASTNode node= name.getParent(); int nodeType= node.getNodeType(); if (nodeType != ASTNode.METHOD_INVOCATION && nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ENUM_DECLARATION) return false; while (nodeType == ASTNode.QUALIFIED_NAME) { node= node.getParent(); nodeType= node.getNodeType(); if (nodeType == ASTNode.IMPORT_DECLARATION) return false; } // 2: match enums IBinding binding= token.getBinding(); return binding instanceof ITypeBinding && ((ITypeBinding) binding).isEnum(); } }
@Override public boolean consumes(SemanticToken token) { // 1: match types SimpleName name= token.getNode(); ASTNode node= name.getParent(); int nodeType= node.getNodeType(); if (nodeType != ASTNode.METHOD_INVOCATION && nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.ENUM_DECLARATION) return false; while (nodeType == ASTNode.QUALIFIED_NAME) { node= node.getParent(); nodeType= node.getNodeType(); if (nodeType == ASTNode.IMPORT_DECLARATION) return false; } // 2: match enums IBinding binding= token.getBinding(); return binding instanceof ITypeBinding && ((ITypeBinding) binding).isEnum(); } }
/** * Translates the given Polyglot type to a name suitable for use in a DOMO TypeReference (i.e. a bytecode-compliant type name). */ public String typeToTypeID(ITypeBinding type) { if (type.isPrimitive()) return type.getBinaryName(); else if (type.isArray()) // arrays' binary names in JDT are like "[Ljava.lang.String;" return type.getBinaryName().replace('.', '/').replace(";", ""); else if (type.isLocal() || type.isAnonymous()) return anonLocalTypeToTypeID(type); else if (type.isClass() || type.isEnum() || type.isInterface()) // in polyglot interfaces are classes too. not in JDT // class binary names in JDT are like "java.lang.String" return 'L' + type.getBinaryName().replace('.', '/'); // TODO: else if (type.isTypeVariable()) { return typeToTypeID(JDT2CAstUtils.getTypesVariablesBase(type, fAst)); } Assertions.UNREACHABLE("typeToTypeID() encountered the type " + type + " that is neither primitive, array, nor class!"); return null; }
@Override RefactoringStatus checkMember(Object memberBinding) { RefactoringStatus status= new RefactoringStatus(); IVariableBinding variableBinding= (IVariableBinding)memberBinding; ITypeBinding fieldsType= variableBinding.getType(); if (fieldsType.isArray()) fieldsType= fieldsType.getElementType(); if (!fieldsType.isPrimitive() && !fieldsType.isEnum() && !alreadyCheckedMemberTypes.contains(fieldsType) && !fieldsType.equals(fTypeBinding)) { status.merge(checkHashCodeEqualsExists(fieldsType, false, ActionMessages.GenerateHashCodeEqualsAction_field_type)); alreadyCheckedMemberTypes.add(fieldsType); } if (Modifier.isTransient(variableBinding.getModifiers())) status.addWarning(Messages.format(ActionMessages.GenerateHashCodeEqualsAction_transient_field_included_error, BasicElementLabels.getJavaElementName(variableBinding.getName())), createRefactoringStatusContext(variableBinding.getJavaElement())); return status; }
@Override RefactoringStatus checkMember(Object memberBinding) { RefactoringStatus status= new RefactoringStatus(); IVariableBinding variableBinding= (IVariableBinding)memberBinding; ITypeBinding fieldsType= variableBinding.getType(); if (fieldsType.isArray()) fieldsType= fieldsType.getElementType(); if (!fieldsType.isPrimitive() && !fieldsType.isEnum() && !alreadyCheckedMemberTypes.contains(fieldsType) && !fieldsType.equals(fTypeBinding)) { status.merge(checkHashCodeEqualsExists(fieldsType, false)); alreadyCheckedMemberTypes.add(fieldsType); } if (Modifier.isTransient(variableBinding.getModifiers())) status.addWarning(Messages.format(ActionMessages.GenerateHashCodeEqualsAction_transient_field_included_error, BasicElementLabels.getJavaElementName(variableBinding.getName())), createRefactoringStatusContext(variableBinding.getJavaElement())); return status; }
private boolean handleAssignment(final ClassInstanceCreation node, final Assignment a) { Expression lhs = a.getLeftHandSide(); if (isTargetType(lhs.resolveTypeBinding())) { ITypeBinding[] typeArguments = lhs.resolveTypeBinding().getTypeArguments(); if (typeArguments.length > 0 && typeArguments[0].isEnum()) { final TypeNameDecider typeNameDecider = new TypeNameDecider(lhs); ASTBuilder b = ctx.getASTBuilder(); Type[] types = new Type[typeArguments.length]; for (int i = 0; i < types.length; i++) { types[i] = b.toType(typeArguments[i], typeNameDecider); } return replace(node, types); } } return VISIT_SUBTREE; }
public boolean visit(SwitchCase node) { // switch on enum allows to use enum constants without qualification if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) { SwitchStatement switchStatement= (SwitchStatement) node.getParent(); ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding(); if (binding != null && binding.isEnum()) { IVariableBinding[] declaredFields= binding.getDeclaredFields(); for (int i= 0; i < declaredFields.length; i++) { IVariableBinding curr= declaredFields[i]; if (curr.isEnumConstant()) { fBreak= fRequestor.acceptBinding(curr); if (fBreak) return false; } } } } return false; }
@Override public boolean visit(SwitchCase node) { // switch on enum allows to use enum constants without qualification if (hasFlag(VARIABLES, fFlags) && !node.isDefault() && isInside(node.getExpression())) { SwitchStatement switchStatement= (SwitchStatement) node.getParent(); ITypeBinding binding= switchStatement.getExpression().resolveTypeBinding(); if (binding != null && binding.isEnum()) { IVariableBinding[] declaredFields= binding.getDeclaredFields(); for (int i= 0; i < declaredFields.length; i++) { IVariableBinding curr= declaredFields[i]; if (curr.isEnumConstant()) { fBreak= fRequestor.acceptBinding(curr); if (fBreak) return false; } } } } return false; }
@Override public CAstType getCAstTypeFor(Object astType) { ITypeBinding jdtType = JDT2CAstUtils.getErasedType((ITypeBinding) astType, fAst); CAstType type = super.getCAstTypeFor(astType); // check cache first // Handle the case where we haven't seen an AST decl for some type before // processing a reference. This can certainly happen with classes in byte- // code libraries, for which we never see an AST decl. // In this case, just create a new type and return that. if (type == null) { if (jdtType.isClass() || jdtType.isEnum() || jdtType.isInterface()) // in JDT interfaces are not classes type = new JdtJavaType(jdtType); else if (jdtType.isPrimitive()) { type = JavaPrimitiveTypeMap.lookupType(jdtType.getName()); } else if (jdtType.isArray()) { type = new JdtJavaArrayType(jdtType); } else Assertions.UNREACHABLE("getCAstTypeFor() passed type that is not primitive, array, or class?"); super.map((ITypeBinding)astType, type); // put in cache } return type; }
private static ImageDescriptor getBaseImageDescriptor(IBinding binding, int flags) { if (binding instanceof ITypeBinding) { ITypeBinding typeBinding= (ITypeBinding) binding; if (typeBinding.isArray()) { typeBinding= typeBinding.getElementType(); } if (typeBinding.isCapture()) { typeBinding.getWildcard(); } return getTypeImageDescriptor(typeBinding.getDeclaringClass() != null, typeBinding, flags); } else if (binding instanceof IMethodBinding) { ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass(); int modifiers= binding.getModifiers(); if (type.isEnum() && (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)) && ((IMethodBinding) binding).isConstructor()) return JavaPluginImages.DESC_MISC_PRIVATE; return getMethodImageDescriptor(binding.getModifiers()); } else if (binding instanceof IVariableBinding) return getFieldImageDescriptor((IVariableBinding) binding); return JavaPluginImages.DESC_OBJS_UNKNOWN; }
private static ImageDescriptor getBaseImageDescriptor(IBinding binding, int flags) { if (binding instanceof ITypeBinding) { ITypeBinding typeBinding= (ITypeBinding) binding; if (typeBinding.isArray()) { typeBinding= typeBinding.getElementType(); } if (typeBinding.isCapture()) { typeBinding.getWildcard(); } return getTypeImageDescriptor(typeBinding.getDeclaringClass() != null, typeBinding, flags); } else if (binding instanceof IMethodBinding) { ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass(); int modifiers= binding.getModifiers(); if (type.isEnum() && (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)) && ((IMethodBinding) binding).isConstructor()) return JavaPluginImages.DESC_MISC_PRIVATE; return getMethodImageDescriptor(binding.getModifiers()); } else if (binding instanceof IVariableBinding) return getFieldImageDescriptor((IVariableBinding) binding); return JavaPluginImages.DESC_OBJS_UNKNOWN; }
private static ImageDescriptor getBaseImageDescriptor(IBinding binding, int flags) { if (binding instanceof ITypeBinding) { ITypeBinding typeBinding= (ITypeBinding) binding; if (typeBinding.isArray()) { typeBinding= typeBinding.getElementType(); } if (typeBinding.isCapture()) { typeBinding.getWildcard(); } return getTypeImageDescriptor(typeBinding.getDeclaringClass() != null, typeBinding, flags); } else if (binding instanceof IMethodBinding) { ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass(); int modifiers= binding.getModifiers(); if (type.isEnum() && (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)) && ((IMethodBinding) binding).isConstructor()) return JavaPluginImages.DESC_MISC_PRIVATE; return getMethodImageDescriptor(binding.getModifiers()); } else if (binding instanceof IVariableBinding) return getFieldImageDescriptor((IVariableBinding) binding); return JavaPluginImages.DESC_OBJS_UNKNOWN; }