No, what I posted first time does work. mg then pointed out a general fact about groovy, which is this:
Properties on an object defined in the standard JavaBeans way, that is, with a getter - getSomething() - and/or a setter - setSomething(value) - or a boolean getter - isSomething() - can be accessed from groovy as plain properties, in this case a property called 'something'. That is, where you have a Java (or Groovy!) class something like: public class Thing { private String name; public String getName() { return name; } public void setName(String newName) { this.name = newName; } } In groovy you can get it with eg: def name = thing.name thing.name = "Thing's New Name" That's generally true. It also generally applies when a method is added to a class through metaprogramming, eg: with an extension module. But maybe not for a category use. I've just deleted a chunk here where I explained that the reason the property-style access doesn't work here was because isDirectory(), getOwner(), getPosixFilePermissions() all take LinkOptions or OpenOptions varargs array. But I just checked and it also doesn't work for isReadable() (where I would have expected "p.readable" to work where "p.isReadable()" does), so it may actually be a category thing. I don't usually use categories, preferring extension modules, where this does work, and which would also work in a @CompileStatic setting. So the simple rule seems to be that you can use categories to access static methods of a category class as if they're methods of your given type (ie: static methods where the first parameter is Path, as methods on Path, minus that parameter), but property-style access doesn't appear to work in that case. Only method calls. -- Rachel Greenham rac...@merus.eu > On 21 Oct 2021, at 16:50, James McMahon <jsmcmah...@gmail.com> wrote: > > Rachel, thanks again for weighing in. I'm a little confused and was hoping to > ask you for clarification. Earlier in this thread, you use a File approach > and it seemed to work. Why did you mention in the follow-up comment that it > doesn't actually work? Why did it work the first time, but not the second > time? Was it because in the first case you were using the groovy command line > interpreter, and in the second case that failed you tried to run from inside > a Groovy script, maybe? Or was it something else I'm missing entirely? > - - - > Jim Mc. > > On Wed, Oct 20, 2021 at 7:35 AM Rachel Greenham <rac...@merus.eu > <mailto:rac...@merus.eu>> wrote: > ah sadly i did think of that when i was writing it but it didn't work. Not > 100% sure why, but i think mostly that many of those methods in Files take a > varargs of stuff like LinkOption... OpenOption... and the groovy category > support isn't resolving properties past that. > > -- > Rachel Greenham > rac...@merus.eu <mailto:rac...@merus.eu> > >> On 20 Oct 2021, at 11:55, MG <mg...@arscreat.com >> <mailto:mg...@arscreat.com>> wrote: >> >> Don't know if you already know this, but using Groovy property syntax makes >> code even more readable, e.g.: >> >> println "${it}: ${it.getOwner()} ${it.getPosixFilePermissions()}" >> >> can be written as: >> >> println "$it: $it.owner $it.posixFilePermissions" >> >> In general: >> 1. Any getter can be accessed without the "get" prefix with a lowercase >> first char >> 2. A simplified string interpolation syntax without the enclosing curly >> braces can be used in these cases >> (same goes for setters) >> >> Cheers, >> mg >> >> >> On 20/10/2021 12:14, James McMahon wrote: >>> Many thanks to each of you who offered guidance. Redirecting back to this >>> today, anticipating success given your advice. Still getting a feel for >>> Groovy so this helps quite a bit. >>> Cheers, >>> -Jim >>> >>> On Fri, Oct 15, 2021 at 11:22 AM Søren Berg Glasius <soe...@glasius.dk >>> <mailto:soe...@glasius.dk>> wrote: >>> @Rachel Rudnick <mailto:rac...@cirrusidentity.com> that is a very clever >>> use of use - good call! >>> >>> Best regards / Med venlig hilsen, >>> Søren Berg Glasius >>> >>> Hedevej 1, Gl. Rye, 8680 Ry, Denmark >>> Mobile: +45 40 44 91 88, Skype: sbglasius >>> --- Press ESC once to quit - twice to save the changes. >>> >>> >>> Den fre. 15. okt. 2021 kl. 17.12 skrev Rachel Greenham <rac...@merus.eu >>> <mailto:rac...@merus.eu>>: >>> Looks like you could pretty much use Files as an extension module and/or >>> category for Path... >>> >>> Hang on, does it work? >>> >>> groovy> import java.nio.file.* >>> groovy> use (Files) { >>> groovy> Path p = Path.of("src/groovy") >>> groovy> println "is directory? ${p.isDirectory()}" >>> groovy> p.list().each { println "${it}: ${it.getOwner()} >>> ${it.getPosixFilePermissions()}" } >>> groovy> } >>> >>> is directory? true >>> src/groovy/benchmark: rachel [OWNER_WRITE, OTHERS_READ, OWNER_EXECUTE, >>> GROUP_READ, GROUP_EXECUTE, OTHERS_EXECUTE, OWNER_READ] >>> src/groovy/xdocs: rachel [OWNER_WRITE, OTHERS_READ, OWNER_EXECUTE, >>> GROUP_READ, GROUP_EXECUTE, OTHERS_EXECUTE, OWNER_READ] >>> src/groovy/bootstrap: rachel [OWNER_WRITE, OTHERS_READ, OWNER_EXECUTE, >>> GROUP_READ, GROUP_EXECUTE, OTHERS_EXECUTE, OWNER_READ] >>> src/groovy/LICENSE: rachel [OWNER_WRITE, OTHERS_READ, GROUP_READ, >>> OWNER_READ] >>> ... >>> >>> oh yeah that works 😉 >>> >>> -- >>> Rachel Greenham >>> rac...@merus.eu <mailto:rac...@merus.eu> >>> >>> > On 15 Oct 2021, at 15:57, Nelson, Erick <erick.nel...@hdsupply.com >>> > <mailto:erick.nel...@hdsupply.com>> wrote: >>> > >>> > import java.nio.file.Path >>> > import java.nio.file.Files >>> > >>> > File f = new File('test') >>> > Path p = f.toPath() >>> > Files.isReadable(p) // boolean >>> > Files.isWritable(p) // boolean >>> > Files.isExecutable(p) // boolean >>> > Files.isDirectory(p) // boolean >>> > Files.isRegularFile(p) // boolean >>> > >>> > >>> > From: James McMahon <jsmcmah...@gmail.com <mailto:jsmcmah...@gmail.com>> >>> > Date: Friday, October 15, 2021 at 4:50 AM >>> > To: users@groovy.apache.org <mailto:users@groovy.apache.org> >>> > <users@groovy.apache.org <mailto:users@groovy.apache.org>> >>> > Subject: Checking directory state using Groovy >>> > >>> > Hello. I am trying to convert an existing script from python to Groovy. >>> > It executes a number of os.path and os.access commands, which I've not >>> > yet been able to find examples of that are written in Groovy. I have >>> > found similar implementations that employ "add on" Jenkins libraries for >>> > Groovy, but I will not have access to such libraries.Here is a brief >>> > excerpt from what I now do in python. Has anyone done similarly in >>> > Groovy? Can I impose for an example? >>> > >>> > Thanks very much in advance. Here is my python: >>> > >>> > if ( os.path.exists(result['thisURL']) and >>> > os.path.isfile(result['thisURL']) ) : >>> > if ( os.access(result['thisURL'], os.F_OK) >>> > and os.access(result['thisURL'], os.R_OK) >>> > and os.access(thisDri, os.W_OK) >>> > and os.access(thisDir, os.X_OK) ) : >>> > # do some stuff >>> > else : >>> > # dir and file not accessible, do some different stuff >>> >> >