Package org.eclipse.jdt.core.dom

Examples of org.eclipse.jdt.core.dom.FieldDeclaration


        while (bodyIter.hasNext()) {
            Object nextDeclaration = bodyIter.next();

            // Handle only field declarations.
            if (nextDeclaration instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) nextDeclaration;
                boolean isStatic = Modifier.isStatic(fieldDecl.getModifiers());

                // If HANDLE_STATIC_FIELDS is set to false, do not refactor
                // static fields.
                if (isStatic && (HANDLE_STATIC_FIELDS != true)) {
                    continue;
                }

                // Handle only private fields or the $CHECKPOINT special field.
                if (Modifier.isPrivate(fieldDecl.getModifiers())) {
                    Type type = Type.getType(fieldDecl);

                    // Iterate over all the fragments in the field declaration.
                    Iterator fragmentIter = fieldDecl.fragments().iterator();

                    while (fragmentIter.hasNext()) {
                        VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragmentIter
                                .next();
                        String fieldName = fragment.getName().getIdentifier();

                        // Get the list of numbers of indices.
                        Hashtable[] tables = new Hashtable[] { _accessedFields,
                                _specialAccessedFields, _backupFields };

                        for (int i = 0; i < tables.length; i++) {
                            List indicesList = _getAccessedField(tables[i],
                                    currentClass.getName(), fieldName);

                            if (indicesList == null) {
                                continue;
                            }

                            // Iterate over all the numbers of indices.
                            Iterator indicesIter = indicesList.iterator();

                            while (indicesIter.hasNext()) {
                                int indices = ((Integer) indicesIter.next())
                                        .intValue();

                                // Create an extra method for every different
                                // number of indices.
                                if (tables[i] == _backupFields) {
                                    newMethods.add(_createBackupMethod(ast,
                                            root, state, fieldName, type,
                                            indices, isStatic));
                                } else {
                                    newMethods
                                            .add(_createAssignMethod(
                                                    ast,
                                                    root,
                                                    state,
                                                    fieldName,
                                                    type,
                                                    indices,
                                                    tables[i] == _specialAccessedFields,
                                                    isStatic));
                                }
                            }
                        }

                        fieldNames.add(fieldName);
                        fieldTypes.add(type);

                        // Create a record field.
                        FieldDeclaration field = _createFieldRecord(ast, root,
                                state, fieldName, type.dimensions(), isStatic);

                        if (field != null) {
                            newFields.add(field);
                        }
                    }
                }
            }
        }

        boolean isInterface = node instanceof TypeDeclaration
                && ((TypeDeclaration) node).isInterface();

        boolean isAnonymous = node instanceof AnonymousClassDeclaration;

        if (isAnonymous) {
            Class[] interfaces = currentClass.getInterfaces();

            for (int i = 0; i < interfaces.length; i++) {
                if (state.getCrossAnalyzedTypes().contains(
                        interfaces[i].getName())) {
                    isAnonymous = false;
                }
            }
        }

        RehandleDeclarationRecord declarationRecord = null;

        if (isAnonymous) {
            Class[] interfaces = currentClass.getInterfaces();

            if (interfaces.length == 1) {
                declarationRecord = new RehandleDeclarationRecord(
                        bodyDeclarations);
                addToLists(_rehandleDeclaration, interfaces[0].getName(),
                        declarationRecord);
            }
        }

        // Do not handle anonymous class declarations in a static method.
        boolean ignore = !_isInStatic.isEmpty()
                && (_isInStatic.peek() == Boolean.TRUE) && isAnonymous;

        // Add an array of all the records.
        if (!isInterface && !ignore) {
            newFields.add(_createRecordArray(ast, root, state, fieldNames));
        }

        // Add a commit method.
        MethodDeclaration commitMethod = null;

        if (!ignore) {
            commitMethod = _createCommitMethod(ast, root, state, fieldNames,
                    fieldTypes, isAnonymous, isInterface);
            newMethods.add(commitMethod);
        }

        if (declarationRecord != null) {
            if (!ignore) {
                declarationRecord._addExtendedDeclaration(commitMethod);
            }

            MethodDeclaration fixedCommitMethod = _createCommitMethod(ast,
                    root, state, fieldNames, fieldTypes, false, isInterface);
            declarationRecord._addFixedDeclaration(fixedCommitMethod);
        }

        // Add a restore method.
        MethodDeclaration restoreMethod = null;

        if (!ignore) {
            restoreMethod = _createRestoreMethod(ast, root, state, fieldNames,
                    fieldTypes, isAnonymous, isInterface);
            newMethods.add(restoreMethod);
        }

        if (declarationRecord != null) {
            if (!ignore) {
                declarationRecord._addExtendedDeclaration(restoreMethod);
            }

            MethodDeclaration fixedRestoreMethod = _createRestoreMethod(ast,
                    root, state, fieldNames, fieldTypes, false, isInterface);
            declarationRecord._addFixedDeclaration(fixedRestoreMethod);
        }

        // Get checkpoint method.
        MethodDeclaration getCheckpoint = null;

        if (!ignore) {
            getCheckpoint = _createGetCheckpointMethod(ast, root, state,
                    isAnonymous, isInterface);

            if (getCheckpoint != null) {
                newMethods.add(getCheckpoint);
            }
        }

        if (declarationRecord != null) {
            if (!ignore) {
                declarationRecord._addExtendedDeclaration(getCheckpoint);
            }

            MethodDeclaration fixedGetCheckpoint = _createGetCheckpointMethod(
                    ast, root, state, false, isInterface);
            declarationRecord._addFixedDeclaration(fixedGetCheckpoint);
        }

        // Set checkpoint method.
        MethodDeclaration setCheckpoint = null;

        if (!ignore) {
            setCheckpoint = _createSetCheckpointMethod(ast, root, state,
                    isAnonymous, isInterface);

            if (setCheckpoint != null) {
                newMethods.add(setCheckpoint);
            }
        }

        if (declarationRecord != null) {
            if (!ignore) {
                declarationRecord._addExtendedDeclaration(setCheckpoint);
            }

            MethodDeclaration fixedSetCheckpoint = _createSetCheckpointMethod(
                    ast, root, state, false, isInterface);
            declarationRecord._addFixedDeclaration(fixedSetCheckpoint);
        }

        // Add an interface.
        if (!ignore) {
            if (isAnonymous) {
                TypeDeclaration proxy = _createProxyClass(ast, root, state);
                bodyDeclarations.add(proxy);

                if (declarationRecord != null) {
                    declarationRecord._addExtendedDeclaration(proxy);
                }
            } else {
                // Set the class to implement Rollbackable.
                if (node instanceof TypeDeclaration) {
                    String rollbackType = getClassName(Rollbackable.class,
                            state, root);
                    ((TypeDeclaration) node).superInterfaceTypes().add(
                            ast.newSimpleType(createName(ast, rollbackType)));
                }

                if (!isInterface) {
                    // Create a checkpoint field.
                    FieldDeclaration checkpointField = _createCheckpointField(
                            ast, root, state);

                    if (checkpointField != null) {
                        bodyDeclarations.add(0, checkpointField);
                    }

                    // Create a record for the checkpoint field.
                    FieldDeclaration record = _createCheckpointRecord(ast,
                            root, state);

                    if (record != null) {
                        newFields.add(0, record);
                    }
View Full Code Here


      TypeDeclaration typeDeclaration = compilationUnitCache.getTypeDeclaration(targetClass);
      FieldDeclaration[] fields = typeDeclaration.getFields();

      Iterator<FieldDeclaration> iterator = Arrays.asList(fields).iterator();
      while (iterator.hasNext()) {
        FieldDeclaration field = iterator.next();
        if (field.fragments().size() == 0) {
          continue;
        }

        VariableDeclarationFragment varibleDeclaration = (VariableDeclarationFragment) field.fragments().get(0);
        if (varibleDeclaration.getName().toString().equals(targetField)) {
          if (isAnnotationAlreadyUsedOnDeclaration(annotation, field)) {
            warnings.add(String.format(Messages.getString("org.apache.openejb.helper.annotation.warnings.1"), annotation.getCanonicalName(), targetClass + "." + targetField)); //$NON-NLS-1$
            return;
          }

          addImportToCompilationUnit(annotation.getCanonicalName(), cu);
         
          Annotation modifier = createModifier(cu.getAST(), annotation, properties, cu);
          field.modifiers().add(0, modifier);
        }
      }

    } catch (CoreException e) {
      warnings.add(String.format(Messages.getString("org.apache.openejb.helper.annotation.warnings.4"), annotation.getCanonicalName(), targetField, targetClass)); //$NON-NLS-1$
View Full Code Here

      TypeDeclaration typeDeclaration = compilationUnitCache.getTypeDeclaration(targetClass);

      VariableDeclarationFragment variableDeclaration = typeDeclaration.getAST().newVariableDeclarationFragment();
      variableDeclaration.setName(typeDeclaration.getAST().newSimpleName(fieldName));

      FieldDeclaration fieldDeclaration = typeDeclaration.getAST().newFieldDeclaration(variableDeclaration);
      Type type = JDTUtils.createQualifiedType(compilationUnit.getAST(), fieldType);
      fieldDeclaration.setType(type);
      Modifier privateModifier = fieldDeclaration.getAST().newModifier(ModifierKeyword.PRIVATE_KEYWORD);
      fieldDeclaration.modifiers().add(privateModifier);
      typeDeclaration.bodyDeclarations().add(fieldDeclaration);
    } catch (Exception e) {
      warnings.add(String.format(Messages.getString("org.apache.openejb.helper.annotation.warnings.11"), fieldName, targetClass)); //$NON-NLS-1$
    }
View Full Code Here

      TypeDeclaration typeDeclaration = compilationUnitCache.getTypeDeclaration(destinationClass);
      FieldDeclaration[] fields = typeDeclaration.getFields();

      Iterator<FieldDeclaration> iterator = Arrays.asList(fields).iterator();
      while (iterator.hasNext()) {
        FieldDeclaration field = iterator.next();
        if (field.fragments().size() == 0) {
          continue;
        }

        VariableDeclarationFragment varibleDeclaration = (VariableDeclarationFragment) field.fragments().get(0);
        if (field.getType().resolveBinding().getQualifiedName().toString().equals(targetClass)) {
          if (isAnnotationAlreadyUsedOnDeclaration(annotation, field)) {
            warnings.add(String.format(Messages.getString("org.apache.openejb.helper.annotation.warnings.1"), annotation.getCanonicalName(), targetClass + "." + varibleDeclaration.getName().toString())); //$NON-NLS-1$
            return;
          }
         
          Annotation modifier = createModifier(cu.getAST(), annotation, properties, cu);
          field.modifiers().add(0, modifier);

          addImportToCompilationUnit(annotation.getCanonicalName(), cu);
        }
      }
    } catch (JavaModelException e) {
View Full Code Here

    Optional<ImportInfo> maybeImport = fieldInfo.toImportInfo();
    if (maybeImport.isPresent()) {
      importInfoSet.add(maybeImport.get());
    }

    FieldDeclaration field = fieldInfo.toFieldDeclaration(ast);
    type.bodyDeclarations().add(field);

    return new FieldDeclarationWriter(field);
  }
View Full Code Here

  @Override
  public FieldDeclaration toFieldDeclaration(AST ast) {
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName(name));

    FieldDeclaration field = ast.newFieldDeclaration(fragment);
    field.setType(simpleTypeInfo.toType(ast));

    return field;
  }
View Full Code Here

      final Map annotationToQualifiedNameMap = new HashMap();

      for (final Iterator cit = constants.iterator(); cit.hasNext();) {
        final IField constantField = (IField) cit.next();
        final FieldDeclaration originalFieldDeclaration = (FieldDeclaration) this.removedFieldNodes
            .get(constantField);

        // Get annotations.
        final Collection annotationCollection = new LinkedHashSet();
        for (final Iterator mit = originalFieldDeclaration.modifiers()
            .iterator(); mit.hasNext();) {
          final Object o = mit.next();
          if (o instanceof Annotation) {
            final Annotation oldAnnotation = (Annotation) o;
            final Annotation newAnnotation = (Annotation) ASTNode
                .copySubtree(ast, oldAnnotation);
            annotationToQualifiedNameMap.put(newAnnotation,
                oldAnnotation.resolveTypeBinding()
                    .getQualifiedName());
            annotationCollection.add(newAnnotation);
          }
        }

        // Get the javadoc.
        final Javadoc originalJavadoc = originalFieldDeclaration
            .getJavadoc();
        final Javadoc newJavadoc = (Javadoc) ASTNode.copySubtree(ast,
            originalJavadoc);

        final EnumConstantDeclaration constDecl = createNewEnumConstantDeclarataion(
View Full Code Here

      }
      break;
    }

    case ASTNode.FIELD_DECLARATION: {
      final FieldDeclaration fd = (FieldDeclaration) node;
      for (final Iterator it = fd.fragments().iterator(); it.hasNext();) {
        final VariableDeclarationFragment vdf = (VariableDeclarationFragment) it
            .next();
        final IJavaElement elem = vdf.resolveBinding().getJavaElement();
        if (!this.constFields.contains(elem)) {
          if (elem.isReadOnly() || vdf.getName().resolveBoxing())
View Full Code Here

      List<BodyDeclaration> bodyDeclarations = getBodyDeclaration().bodyDeclarations();
      for (BodyDeclaration bodyDeclaration : bodyDeclarations)
      {
         if (bodyDeclaration instanceof FieldDeclaration)
         {
            FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
            List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
            for (VariableDeclarationFragment fragment : fragments)
            {
               result.add(new FieldImpl<O>((O) this, fragment));
            }
         }
View Full Code Here

      while (declarationsIterator.hasNext())
      {
         Object next = declarationsIterator.next();
         if (next instanceof FieldDeclaration)
         {
            FieldDeclaration declaration = (FieldDeclaration) next;
            if (declaration.equals(fragment.getParent()))
            {
               List<VariableDeclarationFragment> fragments = declaration.fragments();
               if (fragments.contains(fragment))
               {
                  if (fragments.size() == 1)
                  {
                     declarationsIterator.remove();
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.dom.FieldDeclaration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.