use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.
the class TraitTypeCheckingExtension method handleMissingMethod.
@Override
public List<MethodNode> handleMissingMethod(final ClassNode receiver, final String name, final ArgumentListExpression argumentList, final ClassNode[] argumentTypes, final MethodCall call) {
String[] decomposed = Traits.decomposeSuperCallName(name);
if (decomposed != null) {
return convertToDynamicCall(call, receiver, decomposed, argumentTypes);
}
if (call instanceof MethodCallExpression) {
MethodCallExpression mce = (MethodCallExpression) call;
if (mce.getReceiver() instanceof VariableExpression) {
VariableExpression var = (VariableExpression) mce.getReceiver();
// GROOVY-7322
// static method call in trait?
ClassNode type = null;
if (isStaticTraitReceiver(receiver, var)) {
type = receiver.getGenericsTypes()[0].getType();
} else if (isThisTraitReceiver(var)) {
type = receiver;
}
if (type != null && Traits.isTrait(type)) {
ClassNode helper = Traits.findHelper(type);
Parameter[] params = new Parameter[argumentTypes.length + 1];
params[0] = new Parameter(ClassHelper.CLASS_Type.getPlainNodeReference(), "staticSelf");
for (int i = 1; i < params.length; i++) {
params[i] = new Parameter(argumentTypes[i - 1], "p" + i);
}
MethodNode method = helper.getDeclaredMethod(name, params);
if (method != null) {
return Collections.singletonList(makeDynamic(call, method.getReturnType()));
}
}
}
ClassNode dynamic = mce.getNodeMetaData(TraitASTTransformation.DO_DYNAMIC);
if (dynamic != null) {
return Collections.singletonList(makeDynamic(call, dynamic));
}
}
return NOTFOUND;
}
use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.
the class SuperCallTraitTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp) {
Expression trn = super.transform(exp);
if (trn instanceof BinaryExpression) {
BinaryExpression bin = (BinaryExpression) trn;
Expression leftExpression = bin.getLeftExpression();
if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) {
ClassNode traitReceiver = ((PropertyExpression) leftExpression).getObjectExpression().getNodeMetaData(SuperCallTraitTransformer.class);
if (traitReceiver != null) {
// A.super.foo = ...
TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
ClassNode helper = helpers.getHelper();
String setterName = MetaProperty.getSetterName(((PropertyExpression) leftExpression).getPropertyAsString());
List<MethodNode> methods = helper.getMethods(setterName);
for (MethodNode method : methods) {
Parameter[] parameters = method.getParameters();
if (parameters.length == 2 && parameters[0].getType().equals(traitReceiver)) {
ArgumentListExpression args = new ArgumentListExpression(new VariableExpression("this"), transform(exp.getRightExpression()));
MethodCallExpression setterCall = new MethodCallExpression(new ClassExpression(helper), setterName, args);
setterCall.setMethodTarget(method);
setterCall.setImplicitThis(false);
return setterCall;
}
}
return bin;
}
}
}
return trn;
}
use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.
the class Java5 method configureClassNode.
public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) {
try {
Class clazz = classNode.getTypeClass();
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType());
FieldNode fn = new FieldNode(f.getName(), f.getModifiers(), ret, classNode, null);
setAnnotationMetaData(f.getAnnotations(), fn);
classNode.addField(fn);
}
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {
ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType());
Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes(), m.getParameterAnnotations());
ClassNode[] exceptions = makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes());
MethodNode mn = new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null);
mn.setSynthetic(m.isSynthetic());
setMethodDefaultValue(mn, m);
setAnnotationMetaData(m.getAnnotations(), mn);
mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters()));
classNode.addMethod(mn);
}
Constructor[] constructors = clazz.getDeclaredConstructors();
for (Constructor ctor : constructors) {
Parameter[] params = makeParameters(compileUnit, ctor.getGenericParameterTypes(), ctor.getParameterTypes(), ctor.getParameterAnnotations());
ClassNode[] exceptions = makeClassNodes(compileUnit, ctor.getGenericExceptionTypes(), ctor.getExceptionTypes());
classNode.addConstructor(ctor.getModifiers(), params, exceptions, null);
}
Class sc = clazz.getSuperclass();
if (sc != null)
classNode.setUnresolvedSuperClass(makeClassNode(compileUnit, clazz.getGenericSuperclass(), sc));
makeInterfaceTypes(compileUnit, classNode, clazz);
setAnnotationMetaData(classNode.getTypeClass().getAnnotations(), classNode);
PackageNode packageNode = classNode.getPackage();
if (packageNode != null) {
setAnnotationMetaData(classNode.getTypeClass().getPackage().getAnnotations(), packageNode);
}
} catch (NoClassDefFoundError e) {
throw new NoClassDefFoundError("Unable to load class " + classNode.toString(false) + " due to missing dependency " + e.getMessage());
}
}
use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.
the class Java5 method makeParameter.
private Parameter makeParameter(CompileUnit cu, Type type, Class cl, Annotation[] annotations, int idx) {
ClassNode cn = makeClassNode(cu, type, cl);
Parameter parameter = new Parameter(cn, "param" + idx);
setAnnotationMetaData(annotations, parameter);
return parameter;
}
use of org.codehaus.groovy.ast.Parameter in project grails-core by grails.
the class DefaultASTValidateableHelper method getPropertiesToEnsureConstraintsFor.
/**
* Retrieves a Map describing all of the properties which need to be constrained for the class
* represented by classNode. The keys in the Map will be property names and the values are the
* type of the corresponding property.
*
* @param classNode the class to inspect
* @return a Map describing all of the properties which need to be constrained
*/
protected Map<String, ClassNode> getPropertiesToEnsureConstraintsFor(final ClassNode classNode) {
final Map<String, ClassNode> fieldsToConstrain = new HashMap<String, ClassNode>();
final List<FieldNode> allFields = classNode.getFields();
for (final FieldNode field : allFields) {
if (!field.isStatic()) {
final PropertyNode property = classNode.getProperty(field.getName());
if (property != null) {
fieldsToConstrain.put(field.getName(), field.getType());
}
}
}
final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) {
final MethodNode value = methodEntry.getValue();
if (!value.isStatic() && value.isPublic() && classNode.equals(value.getDeclaringClass()) && value.getLineNumber() > 0) {
Parameter[] parameters = value.getParameters();
if (parameters == null || parameters.length == 0) {
final String methodName = value.getName();
if (methodName.startsWith("get")) {
final ClassNode returnType = value.getReturnType();
final String restOfMethodName = methodName.substring(3);
final String propertyName = GrailsNameUtils.getPropertyName(restOfMethodName);
fieldsToConstrain.put(propertyName, returnType);
}
}
}
}
final ClassNode superClass = classNode.getSuperClass();
if (!superClass.equals(new ClassNode(Object.class))) {
fieldsToConstrain.putAll(getPropertiesToEnsureConstraintsFor(superClass));
}
return fieldsToConstrain;
}
Aggregations