I have a plugin that I'm creating and it has a parameter like so:
/**
* Global variable as maven plugin parameter
* #parameter expression="${plugin.var}" default-value=OtherClass.GLOBAL_VAR
*/
private int var;
I have another class called OtherClass that has a public final static int GLOBAL_VAR;.
How would I be able to set the default-value from a variable from the actual plugin software?
You can just omit the declaration of a default value and assign OtherClass.GLOBAL_VAR directly to var:
/**
* Global variable as maven plugin parameter
* #parameter expression="${plugin.var}"
*/
private int var = OtherClass.GLOBAL_VAR;
As long as ${plugin.var} is not defined, var will not change its value.
Related
In Java you can declare a private final member variable and then initialize it from your constructor, which is very useful and is a very common thing to do:
class MyClass {
private final int widgetCount;
public MyClass(int widgetCount) {
this.widgetCount = widgetCount;
}
In Kotlin how do you initialize final member variables (val types) with values passed in to a constructor?
It is as simple as the following:
class MyClass(private val widgetCount: Int)
This will generate a constructor accepting an Int as its single parameter, which is then assigned to the widgetCount property.
This will also generate no setter (because it is val) or getter (because it is private) for the widgetCount property.
As well as defining the val in the constructor params, you can use an init block to do some general setup, in case you need to do some work before assigning a value:
class MyClass(widgetCount: Int) {
private val widgetCount: Int
init {
this.widgetCount = widgetCount * 100
}
}
init blocks can use the parameters in the primary constructor, and any secondary constructors you have need to call the primary one at some point, so those init blocks will always be called and the primary params will always be there.
Best to read this whole thing really!
Constructors
class MyClass(private val widgetCount: Int)
That's it. If in Java you also have a trivial getter public int getWidgetCount() { return widgetCount; }, remove private.
See the documentation for more details (in particular, under "Kotlin has a concise syntax for declaring properties and initializing them from the primary constructor").
I have created my own lint Detector.visitCallExpression(UCallExpression) and I need to find a way to check if a MyClass class parameter passed into a method call is a child of MyParent class?
//Example having this below code somewhere to be Lint scanned.
someObject.method(MyClass.class)
How can I determine MyClass.class inherits from MyParent class?
//Using the IntelliJ InheritanceUtil utility class
//Converts argument of MyClass.class -> psiClass
InheritanceUtil.isInheritor(psiClass, "com.somePackage.MyParent")
The PsiClass I get from the MyClass.class parameter, is resolved to the base java.lang.Class object, so the InheritanceUtil check always return false~
Anyway i found the solution
/**
* Detects if a Class<?> => PsiClass:Class<?> is a subclass of a PsiClass(?)
*
* #param type The PsiType object that is a PsiClass of the class to be checked for subclass
* #param compareToParentCanonicalClass The Class canonical name to be compared as a super/parent class
*
* #return true if the PsiType is a subclass of compareToParentCanonicalClass, false otherwise
*/
open fun isPsiTypeSubClassOfParentClassType(type: PsiType, compareToParentCanonicalClass: String): Boolean {
println("superClass checking:$type")
var psiClss = PsiTypesUtil.getPsiClass(type)
val pct = type as PsiClassType
val psiTypes: List<PsiType> = ArrayList<PsiType>(pct.resolveGenerics().substitutor.substitutionMap.values)
for (i in psiTypes.indices) {
println("canonical:"+ psiTypes[i].canonicalText)
var psiClass = psiClss?.let { JavaPsiFacade.getInstance(it.project).findClass(psiTypes[i].canonicalText, psiClss.resolveScope) }
return InheritanceUtil.isInheritor(psiClass, compareToParentCanonicalClass)
}
return false;
}
I have a method that I need to test, but ultimately I don't want this method to be public. Is there a tag I can use so that I can use the method in my tests (as if it were public) but the method will be private in the final result?
One way to do this would be to define a compiler #define value, that you set either for test code or unset otherwise:
/** #define {boolean} */
var TESTING = false;
then you can do something like this:
if (TESTING) {
var someMethodVisibleForTesting = function() {}
}
This is about your only option.
All variables seem to be global in my groovy scripts I run on groovy script engine. I made some groovy class but when I make variables, they can be accessed from everywhere. for exaple.
class test{
void func1{ a=4 }
void func2{ print(a) }
}
When I invoke this class function func1 from scala then invoke func2, it results "4". Weird thing is if I declare variables like "def a=0" in the function, the scope of the variable will be limited with in the function.
I'm loading my groovy scripts from GroovyScriptEngine like this(using scala)
var gse = new GroovyScriptEngine(pathList.toArray)
var scriptClass = gse.loadScriptByName(file.getName())
var i = scriptClass.newInstance().asInstanceOf[GroovyObject]
then using invokeMethod to invoke functions in the groove script class. Is there anyway to make variable scopes limited with in functions by default?
That's the expected behaviour, described in Scoping and the Semantics of "def".
Using an undeclared variable in a Groovy script creates a binding variable. Binding variables are global to your script. If you declare your variable with def, it become function local.
This behavior only applies because you load your code as a script. I don't believe its possible to change it. Just use a declaration (def or a type) when you need a local variable.
Note that you can also define a binding variable (global) by using the #Field annotation:
class test {
void func1{ #Field int a=4 }
void func2{ print(a) }
}
is equivalent to
class test {
void func1{ a=4 }
void func2{ print(a) }
}
What is the difference between a member variable and a local variable?
Are they the same?
A local variable is the variable you declare in a function.
A member variable is the variable you declare in a class definiton.
A member variable is a member of a type and belongs to that type's state. A local variable is not a member of a type and represents local storage rather than the state of an instance of a given type.
This is all very abstract, however. Here is a C# example:
class Program
{
static void Main()
{
// This is a local variable. Its lifespan
// is determined by lexical scope.
Foo foo;
}
}
class Foo
{
// This is a member variable - a new instance
// of this variable will be created for each
// new instance of Foo. The lifespan of this
// variable is equal to the lifespan of "this"
// instance of Foo.
int bar;
}
There are two kinds of member variable: instance and static.
An instance variable lasts as long as the instance of the class. There will be one copy of it per instance.
A static variable lasts as long as the class. There is one copy of it for the entire class.
A local variable is declared in a method and only lasts until the method returns:
public class Example {
private int _instanceVariable = 1;
private static int _staticvariable = 2;
public void Method() {
int localVariable = 3;
}
}
// Somewhere else
Example e = new Example();
// e._instanceVariable will be 1
// e._staticVariable will be 2
// localVariable does not exist
e.Method(); // While executing, localVariable exists
// Afterwards, it's gone
public class Foo
{
private int _FooInt; // I am a member variable
public void Bar()
{
int barInt; // I am a local variable
//Bar() can see barInt and _FooInt
}
public void Baz()
{
//Baz() can only see _FooInt
}
}
A local variable is the variable you declare in a function.Its lifespan is on that Function only.
A member variable is the variable you declare in a class definition.Its lifespan is inside that class only.It is Global Variable.It can be access by any function inside that same class.
Variables declared within a method are "local variables"
Variables declared within the class not within any methods are "member variables"(global variables).
Variables declared within the class not within any methods and defined as static are "class variables".
A member variable belongs to an object... something which has state. A local variable just belongs to the symbol table of whatever scope you are in. However, they will be represented in memory much the same as the computer has no notion of a class... it just sees bits which represent instructions. Local variables and member variables can both be on the stack or heap.