private void checkClassForOverwritingFinal(ClassNode cn) {
   ClassNode superCN = cn.getSuperClass();
   if (superCN == null) return;
   if (!isFinal(superCN.getModifiers())) return;
   StringBuilder msg = new StringBuilder();
   msg.append("You are not allowed to overwrite the final ");
   msg.append(getDescription(superCN));
   msg.append(".");
   addError(msg.toString(), cn);
 }
Ejemplo n.º 2
0
        public void call(SourceUnit source) throws CompilationFailedException {
          List<ClassNode> classes = source.ast.getClasses();
          for (ClassNode node : classes) {
            CompileUnit cu = node.getCompileUnit();
            for (Iterator iter = cu.iterateClassNodeToCompile(); iter.hasNext(); ) {
              String name = (String) iter.next();
              SourceUnit su = ast.getScriptSourceLocation(name);
              List<ClassNode> classesInSourceUnit = su.ast.getClasses();
              StringBuilder message = new StringBuilder();
              message
                  .append("Compilation incomplete: expected to find the class ")
                  .append(name)
                  .append(" in ")
                  .append(su.getName());
              if (classesInSourceUnit.isEmpty()) {
                message.append(", but the file seems not to contain any classes");
              } else {
                message.append(", but the file contains the classes: ");
                boolean first = true;
                for (ClassNode cn : classesInSourceUnit) {
                  if (!first) {
                    message.append(", ");
                  } else {
                    first = false;
                  }
                  message.append(cn.getName());
                }
              }

              getErrorCollector()
                  .addErrorAndContinue(new SimpleMessage(message.toString(), CompilationUnit.this));
              iter.remove();
            }
          }
        }
 private String getAnnotationValue(Object memberValue) {
   String val = "null";
   if (memberValue instanceof ListExpression) {
     StringBuilder sb = new StringBuilder("{");
     boolean first = true;
     ListExpression le = (ListExpression) memberValue;
     for (Expression e : le.getExpressions()) {
       if (first) first = false;
       else sb.append(",");
       sb.append(getAnnotationValue(e));
     }
     sb.append("}");
     val = sb.toString();
   } else if (memberValue instanceof ConstantExpression) {
     ConstantExpression ce = (ConstantExpression) memberValue;
     Object constValue = ce.getValue();
     if (constValue instanceof AnnotationNode) {
       StringWriter writer = new StringWriter();
       PrintWriter out = new PrintWriter(writer);
       printAnnotation(out, (AnnotationNode) constValue);
       val = writer.toString();
     } else if (constValue instanceof Number || constValue instanceof Boolean)
       val = constValue.toString();
     else val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
   } else if (memberValue instanceof PropertyExpression
       || memberValue instanceof VariableExpression) {
     // assume must be static class field or enum value or class that Java can resolve
     val = ((Expression) memberValue).getText();
   } else if (memberValue instanceof ClosureExpression) {
     // annotation closure; replaced with this specific class literal to cover the
     // case where annotation type uses Class<? extends Closure> for the closure's type
     val = "groovy.lang.Closure.class";
   } else if (memberValue instanceof ClassExpression) {
     val = ((Expression) memberValue).getText() + ".class";
   }
   return val;
 }
 private void addInvalidUseOfFinalError(
     MethodNode method, Parameter[] parameters, ClassNode superCN) {
   StringBuilder msg = new StringBuilder();
   msg.append("You are not allowed to override the final method ").append(method.getName());
   msg.append("(");
   boolean needsComma = false;
   for (Parameter parameter : parameters) {
     if (needsComma) {
       msg.append(",");
     } else {
       needsComma = true;
     }
     msg.append(parameter.getType());
   }
   msg.append(") from ").append(getDescription(superCN));
   msg.append(".");
   addError(msg.toString(), method);
 }
  private void declare(Variable var, ASTNode expr) {
    String scopeType = "scope";
    String variableType = "variable";

    if (expr.getClass() == FieldNode.class) {
      scopeType = "class";
      variableType = "field";
    } else if (expr.getClass() == PropertyNode.class) {
      scopeType = "class";
      variableType = "property";
    }

    StringBuilder msg = new StringBuilder();
    msg.append("The current ").append(scopeType);
    msg.append(" already contains a ").append(variableType);
    msg.append(" of the name ").append(var.getName());

    if (currentScope.getDeclaredVariable(var.getName()) != null) {
      addError(msg.toString(), expr);
      return;
    }

    for (VariableScope scope = currentScope.getParent(); scope != null; scope = scope.getParent()) {
      // if we are in a class and no variable is declared until
      // now, then we can break the loop, because we are allowed
      // to declare a variable of the same name as a class member
      if (scope.getClassScope() != null) break;

      if (scope.getDeclaredVariable(var.getName()) != null) {
        // variable already declared
        addError(msg.toString(), expr);
        break;
      }
    }
    // declare the variable even if there was an error to allow more checks
    currentScope.putDeclaredVariable(var);
  }
 private void addWeakerAccessError(
     ClassNode cn, MethodNode method, Parameter[] parameters, MethodNode superMethod) {
   StringBuilder msg = new StringBuilder();
   msg.append(method.getName());
   msg.append("(");
   boolean needsComma = false;
   for (Parameter parameter : parameters) {
     if (needsComma) {
       msg.append(",");
     } else {
       needsComma = true;
     }
     msg.append(parameter.getType());
   }
   msg.append(") in ");
   msg.append(cn.getName());
   msg.append(" cannot override ");
   msg.append(superMethod.getName());
   msg.append(" in ");
   msg.append(superMethod.getDeclaringClass().getName());
   msg.append("; attempting to assign weaker access privileges; was ");
   msg.append(superMethod.isPublic() ? "public" : "protected");
   addError(msg.toString(), method);
 }
Ejemplo n.º 7
0
 private void throwExceptionForNoStackElement(int size, ClassNode targetType, boolean coerce) {
   if (size > 0) return;
   StringBuilder sb = new StringBuilder();
   sb.append("Internal compiler error while compiling ")
       .append(controller.getSourceUnit().getName())
       .append("\n");
   MethodNode methodNode = controller.getMethodNode();
   if (methodNode != null) {
     sb.append("Method: ");
     sb.append(methodNode);
     sb.append("\n");
   }
   ConstructorNode constructorNode = controller.getConstructorNode();
   if (constructorNode != null) {
     sb.append("Constructor: ");
     sb.append(methodNode);
     sb.append("\n");
   }
   sb.append("Line ").append(controller.getLineNumber()).append(",");
   sb.append(" expecting ")
       .append(coerce ? "coercion" : "casting")
       .append(" to ")
       .append(targetType.toString(false));
   sb.append(" but operand stack is empty");
   throw new ArrayIndexOutOfBoundsException(sb.toString());
 }