I want to write a method like this: public void print(Scanner scan),
and I want to use the try with resource to enclose scan in order to have it closed automatically.
However, I can't write this: try(scan)
How come I am able to write: try(Scan scan = new Scanner()), but I can't just put in the scan variable from my method parameter in order to get the benefit from try with resource?
Related
I have just discovered contextSuperClass and have been experimenting with using it to provide scope annotations when building a symbol table in a first pass (I have a forward reference DSL).
I set the option in the grammar:
options {
tokenVocab=MyLexer;
language = CSharp;
contextSuperClass = interpreter.MyParserRuleContext;
}
and I have a class that derives from ParserRuleContext:
public class MyParserRuleContext : ParserRuleContext
{
public MyParserRuleContext()
{ }
public MyParserRuleContext(ParserRuleContext parent, int invokingStateNumber) : base(parent, invokingStateNumber)
{
}
public IScope ContextScope { get; set; }
}
So far so good. I use ParseTreeWalker with a listener (Enter/Exit methods) to walk the tree for the 1st pass and build the symbol table adding local scopes, etc into my ContextScope custom property.
The first issue is of course after the symbol table pass I am at the end of the token stream - the tree is walked.
The 2nd parse uses a visitor to evaluate the final result.
I have two questions:
How do I "reset" the parser so that it is at the root again without loosing scopes I have added into my custom property?
The second question is broader, but similar. Is this even a reasonable way to add scope annotations to the parse tree?
I have previously tried to use ParseTreeProperty<IScope> to add scope annotations, but the problem is similar. During the 2nd phase, the context objects provided in the visitor are not the same objects added to ParseTreeProperty<IScope> concurrent dictionary from the 1st pass - so they are not found. Between the 1st & 2nd passes I have only found parser.reset() as a way to start the parser over, and (of course) it appears to fully reset everything and I loose the any state I created in the 1st pass.
I am likely missing completely missing something here - so any help to put me in the right direction will be greatly appreciated.
I am able to declare a class instance as global like this :
gitUtils = new GitUtils()
pipeline {
...
echo "hello: " + gitUtils.doSomething()
}
But if the pipeline calls a function defined as groovy script in "vars" directory, gitUtils is not visible anymore
def call() {
def something = gitUtils.doSomething()
}
I also tryed to use #Field but it changes nothing. Note that all the pipeline is defined in the shared library (project jenkinsfile just calls a function from this shared library).
How to access gitUtils from groovy scripts in /vars in this example ?
I know we could pass the instance as parameter of function declared in /vars but more you have functions using you utilitary class, more it is ugly. Would you imagine to pass 'echo' or 'sh' function as parameter ? No, here is the same.
I know we could not use at all classes defined in src and define groovy script with multiple public methods. Here we could imagine create in /vars a gitutils.groovy with many public method. But this would imply to use 'script' closure in the pipeline to choose which method we want, like this:
script {
gitutils.doSomething()
}
I dont want this. I would pref to create a single function per groovy script in /vars. Thus, we can call them directly in steps, like this:
steps {
myGroovyScriptFunction()
}
But by doing such, the number of function increase and functions are not organized correctly. That is why the idea is to create "big step function" in /vars which use inside more generic functions, from instances of classes (even static in the better case). So, instead of creating a new instance in each groovy script, I would like a global instance.
Context: declarative pipeline, openshift jenkins, slave with dynamic pod template
It's not possible because of JENKINS-42360.
The best way to use global steps without a script block is in my opinion to define an instance of GitUtils and use it directly in the global steps. If you need the pipeline step context you can pass it to the constructor:
# vars/myStep.groovy
import my.packagename.GitUtils
def call() {
GitUtils gitUtils = new GitUtils(steps)
...
}
then you can use the steps with inside GitUtils as described here. Remember to define GitUtils to implement Serializable as per documentation.
I need to assign a value to a variable in some method as beforeRender, as I do?
(Sails.js v0.11.0)
Example CakePHP:
public function beforeRender(){
...
}
Example Rails:
before_render : ....
You can set locals in many places. It depends on your use case.
If the variable is different for each action, then you can place it inside the action calling the view.
If the variable is different for each request, then you can place it in a policy to set locals.
If the variable is static for a single route, you can put it in your routes.
If the variable is static for an application, then you can place in many places.
You can use Locals or Globals in your rendered view, which means any services you creates (even static objects) can be used inside your renderedViews
http://sailsjs.org/#!/documentation/concepts/Views/Locals.html
http://sailsjs.org/#!/documentation/concepts/Globals
I have two sequence item class a_packet and its extended class called bad_packet.
By default, a_packet type is used.
Trying to override a_packet instance with bad_packet, I am able to do it successfully by using set_inst_override_by_name in my uvm test,
factory.set_inst_override_by_name("a_packet","bad_packet", "*");
Now my question is: what if I don't want to use "*", how to know the full hierarchical path of the sequence item instance?
I was trying to utilise get_full_name() from inside the sequence item, right after it is received by the driver, to know the exact hierarchical path. It displayed:
uvm_test_top.env.a_agt.a_seqr.a_sequence.a_packet
But when I replaced the * with above path, the overriding is not happening.
factory.set_inst_override_by_name("a_packet","bad_packet","uvm_test_top.env.a_agt.a_seqr.a_sequence.a_packet");
Did I do something wrong?
Where you create your packet, you'll need to to specify the full path to the corresponding call to create(..):
packet = a_packet::type_id::create("packet", , get_full_name());
If you were using the uvm_do macro, you'll have to change to using the explicit sequence API:
packet = a_packet::type_id::create("packet", , get_full_name());
start_item(packet);
// ... randomize ...
finish_item(packet);
Idea is from this DVCon Paper, section IV.A.
I'm trying to write an Oregon Trail type story in java. In one of the methods later on, you are asked to input your name. I've used this to get the name:
Scanner keys = new Scanner(System.in);
String name = keys.nextLine();
I would like to keep referring to the player as the name they entered in other methods and I'm unsure on how to call it. Any help is appreciated.
When you declare
String name = keys.nextLine();
You are creating a string inside the scope of that method. As you probably noticed, it's no longer accessible once the method finishes. Rather than storing the character name in a variable local to that method, you want to save it to a variable in an outside scope.
In Java's object oriented design, the ideal place to put that would be an instance variable for the relevant class. Say you have some master class called "Game". An instance of this class will represent a running game, have methods for interacting with the game, and hold data about the game. You could have an instance variable in Game declared as:
String playerName;
If that method is within Game, then you would simply have the code:
Scanner keys = new Scanner(System.in);
this.playerName = keys.nextLine();
Since you're assigning the name to a variable that exists outside the scope of the method, it will remain accessible to you later. The exact approach to this depends on how you structured your classes.
A more general solution, which could work better than the above solution depending on your code structure, would be to have that method return a String, rather than set one. For instance:
String getPlayerName() {
Scanner keys = new Scanner(System.in);
return keys.nextLine();
}
A method like that would return a string holding the name, which would allow you to work with it outside of the method.