groovy compile error - groovy

class x{
public static void main(String[] args){
String x="<html><head></head></html>";
String arr[]=x.split("<head>");
String script="hi";
x=arr[0]+"<head>"+script+arr[1];
System.out.println(x);
}
}
the above code when compiled as a java file compiles fine but when used a s a groovy file spits the error :
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
D:\Garage\groovy-binary-1.7.1\groovy-1.7.1\bin\x.groovy: 4: Apparent variable 'a
rr' was found in a static scope but doesn't refer to a local variable, static fi
eld or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable fro
m a static context.
You misspelled a classname or statically imported field. Please check the spelli
ng.
You attempted to use a method 'arr' but left out brackets in a place not allowed
by the grammar.
# line 4, column 10.
String arr[]=x.split("");
^
D:\Garage\groovy-binary-1.7.1\groovy-1.7.1\bin\x.groovy: 6: Apparent variable 'a
rr' was found in a static scope but doesn't refer to a local variable, static fi
eld or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable fro
m a static context.
You misspelled a classname or statically imported field. Please check the spelli
ng.
You attempted to use a method 'arr' but left out brackets in a place not allowed
by the grammar.
# line 6, column 5.
x=arr[0]+""+script+arr[1];
^
D:\Garage\groovy-binary-1.7.1\groovy-1.7.1\bin\x.groovy: 6: Apparent variable 'a
rr' was found in a static scope but doesn't refer to a local variable, static fi
eld or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable fro
m a static context.
You misspelled a classname or statically imported field. Please check the spelli
ng.
You attempted to use a method 'arr' but left out brackets in a place not allowed
by the grammar.
# line 6, column 28.
x=arr[0]+""+script+arr[1];
^
3 errors
D:\Garage\groovy-binary-1.7.1\groovy-1.7.1\bin>

It works if you move the [] to the String side like so:
String[] arr = x.split("<head>");

Related

Can´t find the correct class for string methods in Godot

I am trying to make my program read and edit text. I was trying to use the Position=find ( String , 0 ) method but I get the error:
The method "find" isn't declared in the current class.
I have tried different classes but I cant find the correct one.
Which would that be?
How may I find the correct class in the future?
Thanks in advance.
The find is in the String class.
From the documentation:
int find ( String what, int from=0 )
Finds the first occurrence of a substring. Returns the starting position of the substring or -1 if not found.
So what happens?
When you do Position = find(String, 0) You are calling find from which ever class your code is inside, not from String.
Furthermore, I only see one String there… Which String do you want to find in which one? You need two. Yes, find only takes one String, but it is an instance method, not a static method. You are meant to call it on the one you want to search, like this:
var position := string_in_which_you_are_looking.find(string_you_are_looking_for, 0)

Groovy string comparison in Jenkins pipeline

I'm trying to compare two strings in Jenkins pipeline. The code more or less look like this:
script {
def str1 = 'test1.domainname-test.com'
def str2 = 'test1.domainname-test.com'
if ( str1 == str2 ) {
currentBuild.result = 'ABORT'
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
}
}
str1 is fed by a preceeding command I skipped here due to simplicity. I am getting this error:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String domainname
What am I doing wrong? I tried equals method too, same result. As if it stucked on those dots, thinking it's some kind of property. Thanks in advance
You're missing curly brackets surrounding the TENANT_NAME variable name. In your example:
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
the $ sign gets applied to TENANT_NAME.domainname. And because TENANT_NAME is a string, Groovy interprets the following part as you were trying to access domainname property from a String class, and you get No such field found: field java.lang.String domainname exception.
To avoid such problems, wrap your variable name with {} and you will be fine.
error("TENANT_NAME ${TENANT_NAME}.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")

Filling secureObject array with VSTS variables

I have defined few variables as secret in the pipeline variables but when I try to use those by overriding the parameters file values with:
-secretsObject {"secrets":[{"secretName":"userpwd","secretValue":$(userpwd)}]}
I get "The provided value for the template parameter 'secretsObject' at line '1' and column '787' is not valid.'"
How should I pass the variables into the secrectObjects array?
It looks like the problem lies within the use of double quotes. I got it working with following example:
$(appId) ="a12b34cd-ab12-1ab2-ab1c-a12bc-34de56" -> no double quotes
$(password) = bestpassword -> use double quotes
Example override:
{"secrets":[{"secretName":"AppID","secretValue":$(appId)},{"secretName":"password","secretValue":"$(password)"}]}

error with string variable

I'm trying to use a string variable as input to an xml function. When I use this command:
name2_node(i).setTextContent('truck');
there is no error. But when I replace it with:
name2_node(i).setTextContent(type(i,1));
an error occurs like this:
No method 'setTextContent' with matching signature found for class
'org.apache.xerces.dom.ElementImpl'.
The variable type is a string array. In fact when I type type(i,1) in command window the result is:
ans =
string
"truck"
What part am I doing wrong?
Two things:
use a different variable name, type is a built in function which tells you the variable type, hence why it shows "string" in the output.
Then access the cell array of strings with curly braces
vehicletypes = {'car'; 'truck'; 'van'};
name2_node(i).setTextContent(vehicletypes{i,1}); % For i=2, this passes 'truck'

as3, string to variable

I have array with variable names:
var subjectArray:Array=["subject0","subject1","subject2"];
I need to convert string to var, but following does not work: this[subjectArray[0]] throws an error.
Any thoughts?
That syntax should work. You can check if an object contains a property with a given name with the in keyword. The property does probably not exist.
if (subjectArray[0] in this) {
// do something with this[subjectArray[0]];
}

Resources