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;
}
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;
}
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();
}
Aggregations