コード例 #1
0
 private String[] ensureInitialized(ClassNode domainClass) {
   FieldNode field = domainClass.getField("namedQueries");
   if (field != null && field.isStatic()) {
     Expression initialExpression = field.getInitialExpression();
     if (initialExpression instanceof ClosureExpression) {
       Statement code = ((ClosureExpression) initialExpression).getCode();
       if (code instanceof BlockStatement) {
         List<Statement> statements = ((BlockStatement) code).getStatements();
         if (statements != null) {
           List<String> namedQueries = new ArrayList<String>(statements.size());
           for (Statement s : statements) {
             if (s instanceof ExpressionStatement) {
               Expression expr = ((ExpressionStatement) s).getExpression();
               if (expr instanceof MethodCallExpression) {
                 MethodCallExpression call = (MethodCallExpression) expr;
                 namedQueries.add(call.getMethodAsString());
               }
             }
           }
           return namedQueries.toArray(NO_QUERIES);
         }
       }
     }
   }
   return NO_QUERIES;
 }
コード例 #2
0
 private Expression findStaticField(ClassNode staticImportType, String fieldName) {
   if (staticImportType.isPrimaryClassNode() || staticImportType.isResolved()) {
     FieldNode field = staticImportType.getField(fieldName);
     if (field != null && field.isStatic())
       return new PropertyExpression(new ClassExpression(staticImportType), fieldName);
   }
   return null;
 }
コード例 #3
0
 private Expression findConstant(FieldNode fn) {
   if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal()) {
     if (fn.getInitialValueExpression() instanceof ConstantExpression) {
       return fn.getInitialValueExpression();
     }
   }
   return null;
 }
コード例 #4
0
 public static List<FieldNode> getInstanceNonPropertyFields(ClassNode cNode) {
   final List<FieldNode> result = new ArrayList<FieldNode>();
   for (FieldNode fNode : cNode.getFields()) {
     if (!fNode.isStatic() && cNode.getProperty(fNode.getName()) == null) {
       result.add(fNode);
     }
   }
   return result;
 }
コード例 #5
0
 public static List<FieldNode> getSuperNonPropertyFields(ClassNode cNode) {
   final List<FieldNode> result;
   if (cNode == ClassHelper.OBJECT_TYPE) {
     result = new ArrayList<FieldNode>();
   } else {
     result = getSuperNonPropertyFields(cNode.getSuperClass());
   }
   for (FieldNode fNode : cNode.getFields()) {
     if (!fNode.isStatic() && cNode.getProperty(fNode.getName()) == null) {
       result.add(fNode);
     }
   }
   return result;
 }
コード例 #6
0
 private void ensureNotPublic(String cNode, FieldNode fNode) {
   String fName = fNode.getName();
   // TODO: do we need to lock down things like: $ownClass
   if (fNode.isPublic() && !fName.contains("$") && !(fNode.isStatic() && fNode.isFinal())) {
     addError(
         "Public field '"
             + fName
             + "' not allowed for "
             + MY_TYPE_NAME
             + " class '"
             + cNode
             + "'.",
         fNode);
   }
 }
コード例 #7
0
  private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) {
    if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return;
    printAnnotations(out, fieldNode);
    if (!isInterface) {
      printModifiers(out, fieldNode.getModifiers());
    }

    ClassNode type = fieldNode.getType();
    printType(out, type);

    out.print(" ");
    out.print(fieldNode.getName());
    if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) {
      out.print(" = ");
      Expression valueExpr = fieldNode.getInitialValueExpression();
      if (valueExpr instanceof ConstantExpression) {
        valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
      }
      if (valueExpr instanceof ConstantExpression
          && fieldNode.isStatic()
          && fieldNode.isFinal()
          && ClassHelper.isStaticConstantInitializerType(valueExpr.getType())
          && valueExpr.getType().equals(fieldNode.getType())) {
        // GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles
        // correctly
        if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) {
          out.print("\"" + escapeSpecialChars(valueExpr.getText()) + "\"");
        } else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) {
          out.print("'" + valueExpr.getText() + "'");
        } else {
          ClassNode constantType = valueExpr.getType();
          out.print('(');
          printType(out, type);
          out.print(") ");
          out.print(valueExpr.getText());
          if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType))) out.print('L');
        }
      } else if (ClassHelper.isPrimitiveType(type)) {
        String val = type == ClassHelper.boolean_TYPE ? "false" : "0";
        out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")");
      } else {
        out.print("null");
      }
    }
    out.println(";");
  }
コード例 #8
0
 boolean makeGetField(
     final Expression receiver,
     final ClassNode receiverType,
     final String fieldName,
     final boolean implicitThis,
     final boolean samePackage) {
   FieldNode field = receiverType.getField(fieldName);
   // direct access is allowed if we are in the same class as the declaring class
   // or we are in an inner class
   if (field != null && isDirectAccessAllowed(field, controller.getClassNode(), samePackage)) {
     CompileStack compileStack = controller.getCompileStack();
     MethodVisitor mv = controller.getMethodVisitor();
     if (field.isStatic()) {
       mv.visitFieldInsn(
           GETSTATIC,
           BytecodeHelper.getClassInternalName(field.getOwner()),
           fieldName,
           BytecodeHelper.getTypeDescription(field.getOriginType()));
       controller.getOperandStack().push(field.getOriginType());
     } else {
       if (implicitThis) {
         compileStack.pushImplicitThis(implicitThis);
       }
       receiver.visit(controller.getAcg());
       if (implicitThis) compileStack.popImplicitThis();
       if (!controller.getOperandStack().getTopOperand().isDerivedFrom(field.getOwner())) {
         mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(field.getOwner()));
       }
       mv.visitFieldInsn(
           GETFIELD,
           BytecodeHelper.getClassInternalName(field.getOwner()),
           fieldName,
           BytecodeHelper.getTypeDescription(field.getOriginType()));
     }
     controller.getOperandStack().replace(field.getOriginType());
     return true;
   }
   ClassNode superClass = receiverType.getSuperClass();
   if (superClass != null) {
     return makeGetField(receiver, superClass, fieldName, implicitThis, false);
   }
   return false;
 }
コード例 #9
0
 private void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
   final List<FieldNode> fList = cNode.getFields();
   for (FieldNode fNode : fList) {
     if (fNode.isPublic()) continue; // public fields will be rejected elsewhere
     if (cNode.getProperty(fNode.getName()) != null) continue; // a property
     if (fNode.isFinal() && fNode.isStatic()) continue;
     if (fNode.getName().contains("$")) continue; // internal field
     if (fNode.isFinal() && fNode.getInitialExpression() != null)
       body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
     body.addStatement(createConstructorStatementDefault(fNode));
   }
   final Parameter[] params = new Parameter[] {new Parameter(HASHMAP_TYPE, "args")};
   doAddConstructor(
       cNode,
       new ConstructorNode(
           ACC_PUBLIC,
           params,
           ClassNode.EMPTY_ARRAY,
           new IfStatement(
               equalsNullExpr(new VariableExpression("args")), new EmptyStatement(), body)));
 }
コード例 #10
0
 private Set<String> getPropertyNamesExpressedInTransientsList(final ClassNode classNode) {
   final Set<String> transientFields = new HashSet<String>();
   final FieldNode transientsField = classNode.getField("transients");
   if (transientsField != null && transientsField.isStatic()) {
     final Expression initialValueExpression = transientsField.getInitialValueExpression();
     if (initialValueExpression instanceof ListExpression) {
       final ListExpression le = (ListExpression) initialValueExpression;
       final List<Expression> expressions = le.getExpressions();
       for (Expression expr : expressions) {
         if (expr instanceof ConstantExpression) {
           final ConstantExpression ce = (ConstantExpression) expr;
           final Object contantValue = ce.getValue();
           if (contantValue instanceof String) {
             transientFields.add((String) contantValue);
           }
         }
       }
     }
   }
   return transientFields;
 }
コード例 #11
0
  private void checkFinalFieldAccess(Expression expression) {
    if (!(expression instanceof VariableExpression) && !(expression instanceof PropertyExpression))
      return;
    Variable v = null;
    if (expression instanceof VariableExpression) {
      VariableExpression ve = (VariableExpression) expression;
      v = ve.getAccessedVariable();
    } else {
      PropertyExpression propExp = ((PropertyExpression) expression);
      Expression objectExpression = propExp.getObjectExpression();
      if (objectExpression instanceof VariableExpression) {
        VariableExpression varExp = (VariableExpression) objectExpression;
        if (varExp.isThisExpression()) {
          v = currentClass.getDeclaredField(propExp.getPropertyAsString());
        }
      }
    }
    if (v instanceof FieldNode) {
      FieldNode fn = (FieldNode) v;

      /*
       *  if it is static final but not accessed inside a static constructor, or,
       *  if it is an instance final but not accessed inside a instance constructor, it is an error
       */
      boolean isFinal = fn.isFinal();
      boolean isStatic = fn.isStatic();
      boolean error =
          isFinal && ((isStatic && !inStaticConstructor) || (!isStatic && !inConstructor));

      if (error)
        addError(
            "cannot modify"
                + (isStatic ? " static" : "")
                + " final field '"
                + fn.getName()
                + "' outside of "
                + (isStatic ? "static initialization block." : "constructor."),
            expression);
    }
  }
コード例 #12
0
 @SuppressWarnings("unchecked")
 private boolean makeGetPrivateFieldWithBridgeMethod(
     final Expression receiver,
     final ClassNode receiverType,
     final String fieldName,
     final boolean safe,
     final boolean implicitThis) {
   FieldNode field = receiverType.getField(fieldName);
   ClassNode classNode = controller.getClassNode();
   if (field != null
       && Modifier.isPrivate(field.getModifiers())
       && (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)
           || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode, receiverType))
       && !receiverType.equals(classNode)) {
     Map<String, MethodNode> accessors =
         (Map<String, MethodNode>)
             receiverType
                 .redirect()
                 .getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS);
     if (accessors != null) {
       MethodNode methodNode = accessors.get(fieldName);
       if (methodNode != null) {
         MethodCallExpression mce =
             new MethodCallExpression(
                 receiver,
                 methodNode.getName(),
                 new ArgumentListExpression(
                     field.isStatic() ? new ConstantExpression(null) : receiver));
         mce.setMethodTarget(methodNode);
         mce.setSafe(safe);
         mce.setImplicitThis(implicitThis);
         mce.visit(controller.getAcg());
         return true;
       }
     }
   }
   return false;
 }
コード例 #13
0
  /** Adds special accessors for private constants so that inner classes can retrieve them. */
  @SuppressWarnings("unchecked")
  private void addPrivateFieldsAccessors(ClassNode node) {
    Set<ASTNode> accessedFields =
        (Set<ASTNode>) node.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
    if (accessedFields == null) return;
    Map<String, MethodNode> privateConstantAccessors =
        (Map<String, MethodNode>) node.getNodeMetaData(PRIVATE_FIELDS_ACCESSORS);
    if (privateConstantAccessors != null) {
      // already added
      return;
    }
    int acc = -1;
    privateConstantAccessors = new HashMap<String, MethodNode>();
    final int access = Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC;
    for (FieldNode fieldNode : node.getFields()) {
      if (accessedFields.contains(fieldNode)) {

        acc++;
        Parameter param = new Parameter(node.getPlainNodeReference(), "$that");
        Expression receiver =
            fieldNode.isStatic() ? new ClassExpression(node) : new VariableExpression(param);
        Statement stmt =
            new ExpressionStatement(new PropertyExpression(receiver, fieldNode.getName()));
        MethodNode accessor =
            node.addMethod(
                "pfaccess$" + acc,
                access,
                fieldNode.getOriginType(),
                new Parameter[] {param},
                ClassNode.EMPTY_ARRAY,
                stmt);
        privateConstantAccessors.put(fieldNode.getName(), accessor);
      }
    }
    node.setNodeMetaData(PRIVATE_FIELDS_ACCESSORS, privateConstantAccessors);
  }
コード例 #14
0
 public void visitField(FieldNode node) {
   pushState(node.isStatic());
   super.visitField(node);
   popState();
 }