Kotlin Abstract Protected Property - properties

If I have the following class hierarchy:
abstract class Foo<out T : Bar>() {
abstract protected val thing: T
}
class Baz : Foo<BarImpl> {
override protected val thing: T = ...
}
I get a warning on Baz::thing saying:
Redundant visibility modifier
Does that mean the compiler treats it as protected without you needing to specify that, or that it has to be public?

You will receive an IDE inspection style warnings in Kotlin for things like extra semi-colons you don't need, an extra generic type parameters that can already be inferred, and more. Yours for redundant visibility modifier is along the same lines.
If you expand the inspection message you will see the full text:
This inspection reports visibility modifiers which match the default visibility of an element (public for most elements, protected for members that override a protected member).
And you can turn the inspection off within your IDE if you no longer what to see it.
A few more notes about this:
When overriding a method or member of an ancestor class you are already at the same access level as when it was declared. Saying protected is stating the obvious (to the compiler which knows it is protected).
You are allowed to restate the access modifier again if you want. And you can open it up more, by changing it to public. But you cannot restrict it further, for example going to private (because if it is private how could you override it, that idea is incompatible with override) which becomes a compiler error.

Related

Intellij Override Constructor Short Key

If I want to override a method in a class then I press Ctrl + O short key that list all the methods that are available for overriding.
Do I have a similar short key to override Constructors?
Ctrl + O currently list only methods no information about constructors available.
Thanks!
In case there is BaseClass and the SubClass that extends the base one, there is a quick-fix to "Create constructor matching super" in IntelliJ IDEA (it's available when you "Alt+Enter" on the class SubClass extends BaseClass{}). Please also see the discussion on constructor overriding in Java: Is Constructor Overriding Possible?

Identify the superclass or interface on which a method is being called in the Java editor of IntelliJ 2018?

Is there a quick easy way to have IntelliJ identify the superclass or interface defining the method being called in a class?
For example, in the following example, the fireEvent method is being called on the implicit this but this method is not defined within this Java class being edited in IntelliJ 2018.3. Is there some way to ask IntelliJ which of the several interfaces being implemented as well as any parent in the superclass hierarchy defines this particular method?
…
private void doIt() {
…
fireEvent( event ) ;
}
What I am asking is for the opposite of this Question, Does IntelliJ have the equivalent of the Eclipse “method view”?. That Question asks “for a given class, what are its methods offered?”. I am asking “for a given method being invoked, from what inherited superclass or interface is that method defined?”.
I generally just context-click (Ctrl-click on PC, Command-click on macOS) to navigate to the source for a method. The .java file of the superclass or interface opens in the editor.
In order to avoid navigating away, you can also context-hover. Another option is showing the javadoc via context-q when the cursor is somewhere in the method name.

re-declaring readonly property in class extensions

I am reading this document to learn objective-C: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW1
I the topic "Use Class Extensions to Hide Private Information" (page 73 of pdf) it says:
Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself. It’s common, for example, to define a property as readonly in the interface, but as readwrite in a class extension declared above the implementation, in order that the internal methods of the class can change the property value directly.
What i dont understand in this statement is that since we can change the readonly property from within any private method defined in class extension without that property being re-declared as readwrite in class extension, what does it achieve by re-declaration of that property as readwrite?
You can always change the property through its instance variable (_ivar = ...), but you won't be able to change it using the dot-property syntax (self.myProp =...) unless you redeclare it as readwrite. You will also need to provide additional information like whether the property is strong or weak in this case.
Actually my assumption that "we can change the readonly property from within any private method defined in class extension" is wrong. Class extension cannot use instance variables synthesized automatically for a given readonly property, because they are private (NOT protected) by default.
Even though the document i mention says that instance variable automatically synthesized for a given property has a leading underscore (_Make) in front of it. It actually is not true ( at least not in Xcode 4.6.3 ). It bears the same name has the property itself (unless you synthesize the instance variable itself #synthesize Make = _Make;
Please correct me if i am wrong

Is there a way to get generics to autocomplete in class declarations in IntelliJ?

If I have an interface like this:
public interface GenericDAO<T, PK>
and I want to extend that interface in IntelliJ I will use ctrl-space at this point:
public interface ApplicationDAO extends Generic
And I would expect this:
public interface ApplicationDAO extends GenericDAO<T, PK>
But instead I get
public interface ApplicationDAO extends GenericDAO
With no indication that generics are available. Is there a way to get it to do this correctly? I am new to IntelliJ so I am stil learning all it's ins and outs.
There's no way currently in IDEA to insert such invalid generics. To know that they're there, you can look at the completion list, they're depicted in the suggestion. You can also enable "Raw use of parameterized class" inspection so that each class use without generics will be highlighted yellow in the editor.

Weird JavaCore IType cache problem

I'm developing a plugin that takes all enums in workspace that implements certain interface (IDomain) parses the code (Using AST) does some modification over the enum and marks it as processed with an annotation (#IDomainInfo).
For example, it takes someting like this:
public
enum SomeEnum implements IDomain {
// ...
}
And generates something like this:
public #IDomainInfo(domainId = 1)
enum SomeEnum implements IDomain {
// Some changes here...
}
The idea behind of the #IDomainInfo is that annotated enums have not to be processed anymore by the plugin.
Basically what I do to accomplish the task is to make a search with JavaSearch API to find all the enums implementing IDomain (easy task), and as result I get a list of IJavaElements (which are in fact instances of IType). Then I call a method that iterates through the resulting list and creates a new list of all the IType instances that are not annotated with #IDomainInfo and then process the resulting list: For each non annotated IType do some work, annotate the IType with the #IDomainInfo annotation (Using AST) and then save back the results to file (using IFile, so I can see the changes without refresh, and in fact, if I have the enum open in the editor I see it refreshed instantly :-)
All that works fine, but if I open an #IDomainInfo annotated enum (just for testing) then remove the #IDomainInfo, save the file (I'm sure) and then call the action that does all the job I've described before, when I get to the part that filters annotated IType from non annotated ones, code is something like this:
for (IType type : typeList) {
IAnnotation annotation = type.getAnnotation(“IDomainInfo”);
if (!annotation.exists()) {
// The annotation does not exist, so add the type to the
// list of elements to update and go on...
ret.add(type);
continue;
}
// Something else here...
}
Well, it results that for the file I've just saved the IType detects the annotation I've just removed as if it's still there. If I close and reopen eclipse all works normally.
Now, I've just checked and triple checked my code, so I'm sure that I'm not keeping a stale copy of the old IType unedited still with the annotation version (all my IType come from a fresh java search call every time I run the action).
So the question is, what might I be doing wrong? I mean, I've just read the JavaCore API many times to check If I might be using it wrong or if I have some conceptual flaw there but really I have no clue, it's like if eclipse would be caching the IType ignoring the changes I've just made in the editor :-/
If any one have an idea I would appreciate it a lot :-)
When or how is your plugin called ? Did you register a resource listener or is it a project builder or something else ? If it is called by a resource listener, your plugin may be reading the 'primary copy' for your IType, which has not been saved yet. Hence your changes are still in the Working Copy.