private FieldDeclaration convert(IField field, IType type) throws JavaModelException { TypeReference typeReference = createTypeReference(field.getTypeSignature()); if (typeReference == null) return null; FieldDeclaration fieldDeclaration = new FieldDeclaration(); fieldDeclaration.name = field.getElementName().toCharArray(); fieldDeclaration.type = typeReference; fieldDeclaration.modifiers = field.getFlags(); return fieldDeclaration; }
/** * Adjusts the visibility of the referenced field found in a compilation unit. * * @param field the referenced field to adjust * @param threshold the visibility threshold, or <code>null</code> for default visibility * @throws JavaModelException if an error occurs */ private void adjustOutgoingVisibility(final IField field, final ModifierKeyword threshold) throws JavaModelException { Assert.isTrue(!field.isBinary() && !field.isReadOnly()); //bug 100555 (moving inner class to top level class; taking private fields with you) final IType declaring= field.getDeclaringType(); if (declaring != null && declaring.equals(fReferenced)) return; if (hasLowerVisibility(field.getFlags(), keywordToVisibility(threshold)) && needsVisibilityAdjustment(field, threshold)) adjustOutgoingVisibility(field, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_field_warning); }
public static String getSetterName(IField field, String[] excludedNames) throws JavaModelException { if (excludedNames == null) { excludedNames= EMPTY; } return getSetterName(field.getJavaProject(), field.getElementName(), field.getFlags(), JavaModelUtil.isBoolean(field), excludedNames); }
IType type = (IType) res; IField field= type.getField(new String(fieldBinding.name)); if (field.exists()) { char[] uniqueKey = fieldBinding.computeUniqueKey(); if(field.isBinary()) { ResolvedBinaryField resolvedField = new ResolvedBinaryField( (JavaElement)field.getParent(), field.getElementName(), new String(uniqueKey)); resolvedField.occurrenceCount = field.getOccurrenceCount(); field = resolvedField; } else { ResolvedSourceField resolvedField = new ResolvedSourceField( (JavaElement)field.getParent(), field.getElementName(), new String(uniqueKey)); resolvedField.occurrenceCount = field.getOccurrenceCount(); field = resolvedField;
protected String getKey(IField field, boolean forceOpen) throws JavaModelException { StringBuffer key = new StringBuffer(); // declaring class String declaringKey = getKey((IType) field.getParent(), forceOpen); key.append(declaringKey); // field name key.append('.'); key.append(field.getElementName()); return key.toString(); }
try { if (getFlag(flags, JavaElementLabels.F_PRE_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) { appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags); } else { appendTypeSignatureLabel(field, field.getTypeSignature(), flags); appendTypeLabel(field.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuilder.append('.'); if (getFlag(flags, JavaElementLabels.F_APP_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { fBuilder.append(JavaElementLabels.DECL_STRING); if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) { appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags); } else { appendTypeSignatureLabel(field, field.getTypeSignature(), flags); appendTypeLabel(field.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { if (fVisibility < 0) { fVisibility = (fField.getFlags() & (Flags.AccPublic | Flags.AccProtected | Flags.AccPrivate)); } RefactoringStatus result = new RefactoringStatus(); result.merge(Checks.checkAvailability(fField)); if (result.hasFatalError()) { return result; } fRoot = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(fField.getCompilationUnit(), true, pm); ISourceRange sourceRange = fField.getNameRange(); ASTNode node = NodeFinder.perform(fRoot, sourceRange.getOffset(), sourceRange.getLength()); if (node == null) { return mappingErrorFound(result, node); } fFieldDeclaration = ASTNodes.getParent(node, VariableDeclarationFragment.class); if (fFieldDeclaration == null) { return mappingErrorFound(result, node); } if (fFieldDeclaration.resolveBinding() == null) { if (!processCompilerError(result, node)) { result.addFatalError(RefactoringCoreMessages.SelfEncapsulateField_type_not_resolveable); } return result; } computeUsedNames(); return result; }
/** * Generates a new setter method for the specified field * * @param field the field * @param astRewrite the AST rewrite to use * @param rewrite the list rewrite to use * @throws CoreException if an error occurs * @throws OperationCanceledException if the operation has been cancelled */ private void generateSetterMethod(final IField field, ASTRewrite astRewrite, final ListRewrite rewrite) throws CoreException, OperationCanceledException { final IType type= field.getDeclaringType(); final String name= GetterSetterUtil.getSetterName(field, null); final IMethod existing= JavaModelUtil.findMethod(name, new String[] { field.getTypeSignature()}, false, type); if (existing == null || !querySkipExistingMethods(existing)) { IJavaElement sibling= null; if (existing != null) { sibling= StubUtility.findNextSibling(existing); removeExistingAccessor(existing, rewrite); } else sibling= fInsert; ASTNode insertion= StubUtility2Core.getNodeToInsertBefore(rewrite, sibling); addNewAccessor(type, field, GetterSetterUtil.getSetterStub(field, name, fSettings.createComments, fVisibility | (field.getFlags() & Flags.AccStatic)), rewrite, insertion); if (Flags.isFinal(field.getFlags())) { FieldDeclaration fieldDecl= ASTNodes.getParent(NodeFinder.perform(fASTRoot, field.getNameRange()), FieldDeclaration.class); if (fieldDecl != null) { ModifierRewrite.create(astRewrite, fieldDecl).setModifiers(0, Modifier.FINAL, null); } } } }
public String getNewGetterName() throws CoreException { IMethod primaryGetterCandidate= JavaModelUtil.findMethod(GetterSetterUtil.getGetterName(fField, new String[0]), new String[0], false, fField.getDeclaringType()); if (! JavaModelUtil.isBoolean(fField) || (primaryGetterCandidate != null && primaryGetterCandidate.exists())) return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), JavaModelUtil.isBoolean(fField), null); //bug 30906 describes why we need to look for other alternatives here return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null); }
/** * Creates {@link Field} objects for all instance fields of the type * * @param type the type declaring the field that will be moved to the extracted class * @return an instance of {@link Field} for every field declared in type that is not static * @throws JavaModelException if the type does not exist or if an exception occurs while accessing its corresponding resource. */ public static Field[] getFields(IType type) throws JavaModelException { IField[] fields= type.getFields(); ArrayList<Field> result= new ArrayList<>(); for (int i= 0; i < fields.length; i++) { IField field= fields[i]; if (!Flags.isStatic(field.getFlags()) && !field.isEnumConstant()) result.add(new Field(field.getElementName())); } return result.toArray(new Field[result.size()]); }
private static void checkFieldInType(IType destinationType, RefactoringStatus result, IField field) throws JavaModelException { IField destinationTypeField= destinationType.getField(field.getElementName()); if (! destinationTypeField.exists()) return; String message= Messages.format(RefactoringCoreMessages.MemberCheckUtil_field_exists, new String[]{field.getElementName(), JavaModelUtil.getFullyQualifiedName(destinationType)}); RefactoringStatusContext context= JavaStatusContext.create(destinationType.getCompilationUnit(), destinationTypeField.getSourceRange()); result.addError(message, context); }
private void addDeclarationUpdate() throws CoreException { ISourceRange nameRange= fField.getNameRange(); TextEdit textEdit= new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName()); ICompilationUnit cu= fField.getCompilationUnit(); String groupName= RefactoringCoreMessages.RenameFieldRefactoring_Update_field_declaration; addTextEdit(fChangeManager.get(cu), groupName, textEdit); }
@JavaSelectionSubscriber(METHOD_BODY) public Status onFieldSelection(final IField var, final JavaSelectionEvent event, final Composite parent) throws JavaModelException { return handle(var, var.getElementName(), var.getTypeSignature(), event, parent); }
@Override public Optional<IType> getEnclosingType() { final IJavaElement enclosing = getEnclosingElement().orNull(); if (enclosing instanceof IType) { return of((IType) enclosing); } else if (enclosing instanceof IField) { return of(((IField) enclosing).getDeclaringType()); } else { return absent(); } }
private void checkArgName() { String fieldName = fField.getElementName(); boolean isStatic = true; try { isStatic = JdtFlags.isStatic(fField); } catch (JavaModelException e) { JavaLanguageServerPlugin.log(e); } if ((isStatic && fArgName.equals(fieldName) && fieldName.equals(fField.getDeclaringType().getElementName())) || JavaConventionsUtil.validateIdentifier(fArgName, fField).getSeverity() == IStatus.ERROR) { fArgName = "_" + fArgName; //$NON-NLS-1$ } }
private FieldInfo(ParameterInfo parameterInfo, IField ifield) { super(); this.pi= parameterInfo; this.ifield= ifield; this.name= ifield.getElementName(); } }
@Override public RefactoringStatus checkNewElementName(String newName) throws CoreException { RefactoringStatus result= Checks.checkEnumConstantName(newName, getField()); if (Checks.isAlreadyNamed(getField(), newName)) { result.addFatalError(RefactoringCoreMessages.RenameEnumConstRefactoring_another_name); } if (getField().getDeclaringType().getField(newName).exists()) { result.addFatalError(RefactoringCoreMessages.RenameEnumConstRefactoring_const_already_defined); } return result; }
/** * Checks if the field is boolean. */ public static boolean isBoolean(IField field) throws JavaModelException{ return field.getTypeSignature().equals(Signature.SIG_BOOLEAN); }
private boolean isEnumConstant(IMember member) throws JavaModelException { return (member.getElementType() == IJavaElement.FIELD) && ((IField)member).isEnumConstant(); } }