public String toString() { return super.toString() + "[type: " + getType().getName() + "]"; } }
public String getText() { return getType().getName(); }
private static boolean hasClosureMember(AnnotationNode annotation) { Map<String, Expression> members = annotation.getMembers(); for (Map.Entry<String, Expression> member : members.entrySet()) { if (member.getValue() instanceof ClosureExpression) return true; if (member.getValue() instanceof ClassExpression) { ClassExpression classExpression = (ClassExpression) member.getValue(); Class<?> typeClass = classExpression.getType().isResolved() ? classExpression.getType().redirect().getTypeClass() : null; if (typeClass != null && GeneratedClosure.class.isAssignableFrom(typeClass)) return true; } } return false; }
private boolean findClassWithMatchingBasename(String nameWithoutPackage) { // For performance reasons test against classNamePattern first if (classNamePattern != null && classNamePattern.matcher(nameWithoutPackage).matches()) { return true; } if (classesToNewify != null) { @SuppressWarnings("unchecked") final List<ClassExpression> classes = (List) classesToNewify.getExpressions(); for (ClassExpression ce : classes) { if (ce.getType().getNameWithoutPackage().equals(nameWithoutPackage)) { return true; } } } return false; }
private static ClassNode getSerializeClass(ClassNode alias) { List<AnnotationNode> annotations = alias.getAnnotations(ClassHelper.make(AnnotationCollector.class)); if (!annotations.isEmpty()) { AnnotationNode annotationNode = annotations.get(0); Expression member = annotationNode.getMember("serializeClass"); if (member instanceof ClassExpression) { ClassExpression ce = (ClassExpression) member; if (!ce.getType().getName().equals(AnnotationCollector.class.getName())) { alias = ce.getType(); } } } return alias; }
private void checkDuplicateNameClashes(ListExpression list) { final Set<String> seen = new HashSet<String>(); @SuppressWarnings("unchecked") final List<ClassExpression> classes = (List) list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (seen.contains(name)) { addError("Duplicate name '" + name + "' found during @" + MY_NAME + " processing.", ce); } seen.add(name); } }
@Override public void visitClassExpression(ClassExpression expression) { super.visitClassExpression(expression); addToCache(expression.getType()); } @Override
/** * if Class.forName(x) is recognized, make a direct method call */ protected boolean makeClassForNameCall(Expression origin, Expression receiver, Expression message, Expression arguments) { if (! (receiver instanceof ClassExpression)) return false; ClassExpression ce = (ClassExpression) receiver; if (!ClassHelper.CLASS_Type.equals(ce.getType())) return false; String msg = getMethodName(message); if (!"forName".equals(msg)) return false; ArgumentListExpression ae = makeArgumentList(arguments); if (ae.getExpressions().size()!=1) return false; return writeDirectMethodCall(CLASS_FOR_NAME_STRING,false, receiver, ae); }
private void rewriteArgumentList(MethodCallExpression call, ClassExpression objectExpression) { if (SpockRuntime.class.getName().equals(objectExpression.getType().getName())) { String methodName = call.getMethod().getText(); if (REWRITE_METHOD_ARGS.contains(methodName)) { List<Expression> arguments = new ArrayList<>(((ArgumentListExpression)call.getArguments()).getExpressions()); Expression expression = arguments.get(0); if (expression instanceof VariableExpression && isErrorCollectorExpression(((VariableExpression)expression))) { arguments.set(0, errorCollectorExpression()); } expression = arguments.get(1); if (expression instanceof VariableExpression && isValueRecorderExpression(((VariableExpression)expression))) { arguments.set(1, valueRecorderExpression()); } call.setArguments(new ArgumentListExpression(arguments)); } } }
private static AutoCloneStyle getStyle(AnnotationNode node, String name) { final Expression member = node.getMember(name); if (member instanceof PropertyExpression) { PropertyExpression prop = (PropertyExpression) member; Expression oe = prop.getObjectExpression(); if (oe instanceof ClassExpression) { ClassExpression ce = (ClassExpression) oe; if (ce.getType().getName().equals("groovy.transform.AutoCloneStyle")) { return AutoCloneStyle.valueOf(prop.getPropertyAsString()); } } } return null; }
private void checkClassLevelClashes(ListExpression list) { @SuppressWarnings("unchecked") final List<ClassExpression> classes = (List) list.getExpressions(); for (ClassExpression ce : classes) { final String name = ce.getType().getNameWithoutPackage(); if (findClassWithMatchingBasename(name)) { addError("Error during @" + MY_NAME + " processing. Class '" + name + "' can't appear at " + "method/constructor/field level if it already appears at the class level.", ce); } } }
private static AnnotationCollectorMode getMode(AnnotationNode node) { final Expression member = node.getMember("mode"); if (member instanceof PropertyExpression) { PropertyExpression prop = (PropertyExpression) member; Expression oe = prop.getObjectExpression(); if (oe instanceof ClassExpression) { ClassExpression ce = (ClassExpression) oe; if (ce.getType().getName().equals("groovy.transform.AnnotationCollectorMode")) { return AnnotationCollectorMode.valueOf(prop.getPropertyAsString()); } } } return null; }
private static groovy.transform.PackageScopeTarget extractTarget(PropertyExpression expr) { Expression oe = expr.getObjectExpression(); if (oe instanceof ClassExpression) { ClassExpression ce = (ClassExpression) oe; if (ce.getType().getName().equals("groovy.transform.PackageScopeTarget")) { Expression prop = expr.getProperty(); if (prop instanceof ConstantExpression) { String propName = (String) ((ConstantExpression) prop).getValue(); try { return PackageScopeTarget.valueOf(propName); } catch(IllegalArgumentException iae) { /* ignore */ } } } } throw new GroovyBugError("Internal error during " + MY_TYPE_NAME + " processing. Annotation parameters must be of type: " + TARGET_CLASS_NAME + "."); }
expectedType = typeX.getType();
private void evaluateInstanceof(BinaryExpression expression) { OperandStack operandStack = controller.getOperandStack(); expression.getLeftExpression().visit(controller.getAcg()); operandStack.box(); Expression rightExp = expression.getRightExpression(); ClassNode classType; if (rightExp instanceof ClassExpression) { ClassExpression classExp = (ClassExpression) rightExp; classType = classExp.getType(); } else { throw new RuntimeException( "Right hand side of the instanceof keyword must be a class name, not: " + rightExp); } String classInternalName = BytecodeHelper.getClassInternalName(classType); controller.getMethodVisitor().visitTypeInsn(INSTANCEOF, classInternalName); operandStack.replace(ClassHelper.boolean_TYPE); }
private static SyntaxException createException(ClassNode trait, ClassNode targetNode, MethodNode forwarder, MethodNode existingMethod) { String middle; ASTNode errorTarget; if (existingMethod.getLineNumber() == -1) { // came from a trait errorTarget = targetNode; List<AnnotationNode> allAnnos = existingMethod.getAnnotations(Traits.TRAITBRIDGE_CLASSNODE); AnnotationNode bridgeAnno = allAnnos == null ? null : allAnnos.get(0); String fromTrait = null; if (bridgeAnno != null) { Expression traitClass = bridgeAnno.getMember("traitClass"); if (traitClass instanceof ClassExpression) { ClassExpression ce = (ClassExpression) traitClass; fromTrait = ce.getType().getNameWithoutPackage(); } } middle = "in '" + targetNode.getNameWithoutPackage(); if (fromTrait != null) { middle += "' from trait '" + fromTrait; } } else { errorTarget = existingMethod; middle = "declared in '" + targetNode.getNameWithoutPackage(); } String message = "The static '" + forwarder.getName() + "' method " + middle + "' conflicts with the instance method having the same signature from trait '" + trait.getNameWithoutPackage() + "'"; return new SyntaxException(message, errorTarget); }
private static ClassNode getTargetClass(SourceUnit source, AnnotationNode annotation) { Expression value = annotation.getMember("value"); if (!(value instanceof ClassExpression)) { //noinspection ThrowableInstanceNeverThrown source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage( new SyntaxException("@groovy.lang.Category must define 'value' which is the class to apply this category to", annotation.getLineNumber(), annotation.getColumnNumber(), annotation.getLastLineNumber(), annotation.getLastColumnNumber()), source)); return null; } else { ClassExpression ce = (ClassExpression) value; return ce.getType(); } } }
private boolean validateEnumConstant(Expression exp) { if (exp instanceof PropertyExpression) { PropertyExpression pe = (PropertyExpression) exp; String name = pe.getPropertyAsString(); if (pe.getObjectExpression() instanceof ClassExpression && name != null) { ClassExpression ce = (ClassExpression) pe.getObjectExpression(); ClassNode type = ce.getType(); if (type.isEnum()) { boolean ok = false; try { FieldNode enumField = type.getDeclaredField(name); ok = enumField != null && enumField.getType().equals(type); } catch(Exception ex) { // ignore } if(!ok) { addError("No enum const " + type.getName() + "." + name, pe); return false; } } } } return true; }
protected Expression transformDeclarationExpression(DeclarationExpression de) { visitAnnotations(de); Expression oldLeft = de.getLeftExpression(); checkingVariableTypeInDeclaration = true; Expression left = transform(oldLeft); checkingVariableTypeInDeclaration = false; if (left instanceof ClassExpression) { ClassExpression ce = (ClassExpression) left; addError("you tried to assign a value to the class " + ce.getType().getName(), oldLeft); return de; } Expression right = transform(de.getRightExpression()); if (right == de.getRightExpression()) { fixDeclaringClass(de); return de; } DeclarationExpression newDeclExpr = new DeclarationExpression(left, de.getOperation(), right); newDeclExpr.setDeclaringClass(de.getDeclaringClass()); fixDeclaringClass(newDeclExpr); newDeclExpr.setSourcePosition(de); newDeclExpr.addAnnotations(de.getAnnotations()); return newDeclExpr; }
ClassNode type = expression.getType(); MethodVisitor mv = controller.getMethodVisitor(); if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {