JavaFieldDefinition.java

/*
** Module   : JavaFieldDefinition.java
** Abstract : a wrapper for a Java field
**
** Copyright (c) 2019-2022, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------Description---------------------------------
** 001 GES 20190922 First version.
** 002 TJD 20220504 Java 11 compatibility minor changes
** 003 GES 20220114 Minor code cleanup.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.uast;

import java.lang.reflect.*;
import com.goldencode.ast.*;

/**
 * A simple wrapper class to contain all related data about a Java field.  
 */
public class JavaFieldDefinition
extends Variable
{
   /** The field being wrapped. */
   private Field f = null;
   
   /**
    * Constructs an instance with the required data to be contained.
    *
    * @param    f
    *           The Java field to be wrapped.
    * @param    name
    *           The full name of the variable.
    * @param    type
    *           The token type (as defined by the parser) of the variable.
    * @param    cls
    *           Fully qualified class name of the object instance represented
    *           or <code>null</code> if this variable does not represent an
    *           object instance.
    */
   public JavaFieldDefinition(Field f, String name, int type, String cls)
   {
      super(name, type, cls);
      this.f = f;
   }
   
   /**
    * Check if this is an array Java type.
    * 
    * @return   See above.
    */
   public boolean isExtent()
   {
      return f.getType().getName().charAt(0) == '[';
   }
   
   /**
    * Returns the static flag.
    *
    * @return   <code>true</code> if this is a static member.
    */
   public boolean isStatic()
   {
      return JavaClassDefinition.isStaticModifier(f.getModifiers());
   }
   
   /**
    * Get the defining class of this variable, if this is a class member.
    * 
    * @return   The {@link #clsDef}.
    */
   public ClassDefinition getClassDefinition()
   {
      return new JavaClassDefinition(f.getDeclaringClass());
   }
   
   /**
    * Stores all non-standard options as annotations to the AST passed as
    * a parameter if this is the original definition, otherwise it stores
    * a subset of values, most important of which is the cross-reference
    * index to allow the original definition to be easily identified and
    * accessed from a reference.  If an option has the default value, it is
    * not saved as an annotation.
    *
    * @param    ast
    *           The AST to annotate.    
    * @param    original
    *           <code>true</code> if this is to annotate the AST of the
    *           original definition, <code>false</code> if the AST is only
    *           a reference.
    * @param    inMethod
    *           <code>true</code> if this is being done from within a method definition. 
    * @param    sym
    *           The current symbol resolver.
    */
   public void annotateOptions(Aast ast, boolean original, boolean inMethod, SymbolResolver sym)
   {
      if (ast == null)
      {
         // nothing to do
         return;
      }
      
      // let the superclass handle the parts that it can handle
      super.annotateOptions(ast, original, inMethod, sym);
      
      ast.putAnnotation("is-java", true);
      ast.putAnnotation("javaname", getName());
      
      // this is never the original definition because it references a Java member
      int mods   = JavaClassDefinition.javaAccessLevel(f.getModifiers());
      int access = JavaClassDefinition.javaModToTokenType(mods);
      ast.putAnnotation("access-mode", Long.valueOf(access));
      
      if (isStatic())
      {
         ast.putAnnotation("static", true);
      }
      
      if (isExtent())
      {
         // Java arrays are always indeterminate
         ast.putAnnotation("extent", Long.valueOf(-1));
      }
   }   
}