Search in sources :

Example 6 with Reference

use of groovy.lang.Reference in project groovy by apache.

the class StaticCompilationVisitor method existsProperty.

@Override
protected boolean existsProperty(final PropertyExpression pexp, final boolean checkForReadOnly, final ClassCodeVisitorSupport visitor) {
    Expression objectExpression = pexp.getObjectExpression();
    ClassNode objectExpressionType = getType(objectExpression);
    Reference<ClassNode> rType = new Reference<>(objectExpressionType);
    ClassCodeVisitorSupport receiverMemoizer = new ClassCodeVisitorSupport() {

        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }

        @Override
        public void visitField(final FieldNode node) {
            if (visitor != null)
                visitor.visitField(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) {
                if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(declaringClass, LIST_TYPE)) {
                    boolean spread = declaringClass.getDeclaredField(node.getName()) != node;
                    pexp.setSpreadSafe(spread);
                }
                rType.set(declaringClass);
            }
        }

        @Override
        public void visitMethod(final MethodNode node) {
            if (visitor != null)
                visitor.visitMethod(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) {
                if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(declaringClass, LIST_TYPE)) {
                    List<MethodNode> properties = declaringClass.getDeclaredMethods(node.getName());
                    boolean spread = true;
                    for (MethodNode mn : properties) {
                        if (node == mn) {
                            spread = false;
                            break;
                        }
                    }
                    // it's no real property but a property of the component
                    pexp.setSpreadSafe(spread);
                }
                rType.set(declaringClass);
            }
        }

        @Override
        public void visitProperty(final PropertyNode node) {
            if (visitor != null)
                visitor.visitProperty(node);
            ClassNode declaringClass = node.getDeclaringClass();
            if (declaringClass != null) {
                if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(declaringClass, LIST_TYPE)) {
                    List<PropertyNode> properties = declaringClass.getProperties();
                    boolean spread = true;
                    for (PropertyNode propertyNode : properties) {
                        if (propertyNode == node) {
                            spread = false;
                            break;
                        }
                    }
                    // it's no real property but a property of the component
                    pexp.setSpreadSafe(spread);
                }
                rType.set(declaringClass);
            }
        }
    };
    boolean exists = super.existsProperty(pexp, checkForReadOnly, receiverMemoizer);
    if (exists) {
        objectExpressionType = rType.get();
        if (objectExpression.getNodeMetaData(PROPERTY_OWNER) == null) {
            objectExpression.putNodeMetaData(PROPERTY_OWNER, objectExpressionType);
        }
        if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(objectExpressionType, LIST_TYPE)) {
            objectExpression.putNodeMetaData(COMPONENT_TYPE, inferComponentType(objectExpressionType, int_TYPE));
        }
    }
    return exists;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Reference(groovy.lang.Reference) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 7 with Reference

use of groovy.lang.Reference in project groovy by apache.

the class BindPathSnooper method createBinding.

@Override
public FullBinding createBinding(SourceBinding source, TargetBinding target) {
    if (source != this) {
        throw new RuntimeException("Source binding must the Trigger Binding as well");
    }
    final BindPathSnooper delegate = new BindPathSnooper();
    try {
        // create our own local copy of the closure
        final Class closureClass = closure.getClass();
        // do in privileged block since we may be looking at private stuff
        Closure closureLocalCopy = VMPluginFactory.getPlugin().doPrivileged(new PrivilegedAction<Closure>() {

            @Override
            public Closure run() {
                // assume closures have only 1 constructor, of the form (Object, Reference*)
                Constructor constructor = closureClass.getConstructors()[0];
                int paramCount = constructor.getParameterTypes().length;
                Object[] args = new Object[paramCount];
                args[0] = delegate;
                for (int i = 1; i < paramCount; i++) {
                    args[i] = new Reference<Object>(new BindPathSnooper());
                }
                try {
                    boolean acc = isAccessible(constructor);
                    ReflectionUtils.trySetAccessible(constructor);
                    Closure localCopy = (Closure) constructor.newInstance(args);
                    if (!acc) {
                        constructor.setAccessible(false);
                    }
                    localCopy.setResolveStrategy(Closure.DELEGATE_ONLY);
                    for (Field f : closureClass.getDeclaredFields()) {
                        acc = isAccessible(f);
                        ReflectionUtils.trySetAccessible(f);
                        if (f.getType() == Reference.class) {
                            delegate.fields.put(f.getName(), (BindPathSnooper) ((Reference) f.get(localCopy)).get());
                        }
                        if (!acc) {
                            f.setAccessible(false);
                        }
                    }
                    return localCopy;
                } catch (Exception e) {
                    throw new RuntimeException("Error snooping closure", e);
                }
            }
        });
        try {
            closureLocalCopy.call();
        } catch (DeadEndException e) {
            // we want this exception exposed.
            throw e;
        } catch (Exception e) {
        // LOGME
        // ignore it, likely failing because we are faking out properties
        // such as a call to Math.min(int, BindPathSnooper)
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("A closure expression binding could not be created because of " + e.getClass().getName() + ":\n\t" + e.getMessage());
    }
    List<BindPath> rootPaths = new ArrayList<BindPath>();
    for (Map.Entry<String, BindPathSnooper> entry : delegate.fields.entrySet()) {
        BindPath bp = createBindPath(entry.getKey(), entry.getValue());
        bp.currentObject = closure;
        rootPaths.add(bp);
    }
    PropertyPathFullBinding fb = new PropertyPathFullBinding();
    fb.setSourceBinding(new ClosureSourceBinding(closure));
    fb.setTargetBinding(target);
    fb.bindPaths = rootPaths.toArray(EMPTY_BINDPATH_ARRAY);
    return fb;
}
Also used : Closure(groovy.lang.Closure) Constructor(java.lang.reflect.Constructor) Reference(groovy.lang.Reference) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with Reference

use of groovy.lang.Reference in project cayenne by apache.

the class CgenTask method getDestDirFile.

@OutputDirectory
protected File getDestDirFile() {
    final Reference<File> javaSourceDir = new Reference<>(null);
    if (destDir != null) {
        javaSourceDir.set(destDir);
    } else if (destDirName != null) {
        javaSourceDir.set(getProject().file(destDirName));
    } else {
        getProject().getPlugins().withType(JavaPlugin.class, new Action<JavaPlugin>() {

            @Override
            public void execute(final JavaPlugin plugin) {
                SourceSetContainer sourceSets = (SourceSetContainer) getProject().getProperties().get("sourceSets");
                Set<File> sourceDirs = sourceSets.getByName("main").getJava().getSrcDirs();
                if (sourceDirs != null && !sourceDirs.isEmpty()) {
                    // find java directory, if there is no such dir, take first
                    for (File dir : sourceDirs) {
                        if (dir.getName().endsWith("java")) {
                            javaSourceDir.set(dir);
                            break;
                        }
                    }
                    if (javaSourceDir.get() == null) {
                        javaSourceDir.set(sourceDirs.iterator().next());
                    }
                }
            }
        });
    }
    if (javaSourceDir.get() == null) {
        throw new InvalidUserDataException("cgen.destDir is not set and there is no Java source sets found.");
    }
    if (!javaSourceDir.get().exists()) {
        javaSourceDir.get().mkdirs();
    }
    return javaSourceDir.get();
}
Also used : Action(org.gradle.api.Action) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) Reference(groovy.lang.Reference) InvalidUserDataException(org.gradle.api.InvalidUserDataException) JavaPlugin(org.gradle.api.plugins.JavaPlugin) File(java.io.File)

Aggregations

Reference (groovy.lang.Reference)8 Closure (groovy.lang.Closure)3 Constructor (java.lang.reflect.Constructor)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 ClassNode (org.codehaus.groovy.ast.ClassNode)3 FieldNode (org.codehaus.groovy.ast.FieldNode)3 MethodNode (org.codehaus.groovy.ast.MethodNode)3 PropertyNode (org.codehaus.groovy.ast.PropertyNode)3 Expression (org.codehaus.groovy.ast.expr.Expression)3 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 ClassCodeExpressionTransformer (org.codehaus.groovy.ast.ClassCodeExpressionTransformer)2 Parameter (org.codehaus.groovy.ast.Parameter)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)2