WeakReference symbol cannot be found - java

Already import the weakreference but the compiler cannot find the symbol, what wrong? There a memory leak in DumpReceiver.java I thought weakreference might free after used?
import java.lang.ref.WeakReference;
Receiver r = new DumpReceiver(System.out);
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);
MidiInDump.java:64: cannot find symbol
symbol : constructor WeakReference(javax.sound.midi.Receiver)
location: class java.lang.ref.WeakReference<DumpReceiver>
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r);
^

Look closely at the error message. It's not talking about the class (it's finding that just fine). It is talking about the constructor. It doesn't find a constructor that takes a javax.sound.midi.Receiver argument on the type WeakReference<DumpReceiver>. Looking at the JavaDoc of WeakReference<T> there is one constructor that takes an argument of type T.
You're trying to create a WeakReference<DumpReceiver> but try to pass in an object of type javax.sound.midi.Receiver. You either need to create a WeakReference<Receiver> instead or change the variable r to be of type DumpReceiver.

Related

Error in ColdFusion Javacast array to class from loadPaths

I am trying to use Elasticsearch's Java API.
I am trying to create a RestClientBuilder.
Host=createObject("java", "org.apache.http.HttpHost").init(variables.HostName, variables.Port);
Node=createObject("java", "org.elasticsearch.client.Node").init(Host);
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.elasticsearch.client.Node[]", [Node])).build();
I get the error
Cannot convert the value to Java array because type org.elasticsearch.client.Node is unknown.
Also if I just try to use:
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.apache.http.HttpHost[]", [Host]));
I get the following error
Either there are no methods with the specified method name and
argument types or the builder method is overloaded with argument types
that ColdFusion cannot decipher reliably. ColdFusion found 0 methods
that match the provided arguments. If this is a Java object and you
verified that the method exists, use the javacast function to reduce
ambiguity.
This I assume is because ColdFusion doesn't play nicely with varargs
I found a workaround using this method
https://www.bennadel.com/blog/1980-tojava---a-coldfusion-user-defined-function-for-complex-java-casting.htm
I believe there is a bug with Javacast and javaSettings loadPaths not being used.
coldfusion.runtime.Cast$UnknownTypeException: Cannot convert the value
to Java array because type org.elasticsearch.client.Node is unknown.
at coldfusion.runtime.Cast.toJavaArray(Cast.java:1602)
Additionally if I try to perform the actiuons that the UDF takes
local.javaClass = createObject("java", "org.apache.http.HttpHost");
local.HostArrayReflect = createObject("java", "java.lang.reflect.Array");
local.HostArray = local.HostArrayReflect.newInstance(
local.javaClass.GetClass()
, JavaCast( "int", ArrayLen(local.Hosts))
);
for (i=0; i LT ArrayLen(local.Hosts); i=i+1) {
local.HostArrayReflect.Set(local.HostArray, JavaCast("int", i), local.Hosts[i]);
}
I get the error
An exception occurred while instantiating a Java object. The class
must not be an interface or an abstract class. If the class has a
constructor that accepts an argument, you must call the constructor
explicitly using the init(args) method. Error :
org.apache.http.HttpHost
java.lang.NoSuchMethodException: org.apache.http.HttpHost.() at
java.lang.Class.getConstructor0(Class.java:3082) at
java.lang.Class.newInstance(Class.java:412) at
coldfusion.runtime.java.JavaProxy.createObjectWithDefaultConstructor(JavaProxy.java:209)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:92)
This happens when I try to run getClass(), but in the UDF there is no issue. A coworker tried to run this on Lucee and it seems to have worked, so I believe there is a bug in CF related to this.

What type of token exactly is "var" in Java 10?

In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var is not a reserved word in Java 10, because you can also use var as an identifier:
public class Java10 {
var var = 42; // <-- this works
}
However, you cannot use i.e. assert as an identifier, as in var assert = 2, because assert is a reserved word.
As it's told in the linked newsletter, the fact that var is not a reserved word is good news, because this allows code from previous versions of Java that uses var as an identifier to compile without problems in Java 10.
So, what's var then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?
Additionally, apart from supporting backwards compatibility (by allowing older code that contains var as an identifier to compile), are there other advantages to var not being a reserved word?
According to JEP-286: Local-Variable Type Inference, var is
not a keyword; instead it is a reserved type name.
(Earlier versions of the JEP left room for implementing either as a reserved type name or as a context-sensitive keyword; the former path was ultimately chosen.)
Because it's not a "reserved keyword", it is possible to still use it in variable names (and package names), but not in class or interface names.
I would think the biggest reason for not making var a reserved keyword is backwards compatibility with old source code.
var is a reserved type name var is not a keyword, It’s a reserved type
name.
We can create a variable named “var”.
you can read here for more details.
var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.
public static void var() { // syntactically correct
}
“var” as a package name is allowed.
package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
^
as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error
interface var{ } // Compile Error
var author = null; // Null cannot be inferred to a type
LocalTypeInference.java:47: error: cannot infer type for local variable author
var author = null;
^
(variable initializer is 'null')
1 error

Cannot find symbol '/' in class name

When I compile my java class. There are some errors of cannot find symble '/' in class name. Below is a sample code from my class:
public TransactionSearchResponse submit(TxnSearchRequest req)
{
url = (new StringBuilder(String.valueOf(req.getBaseUrl()))).append("/txns/search").toString();
method = "POST";
return (TransactionSearchResponse)sendRequest(req, com/COMPPONENT/api/TransactionSearchResponse);
}
Be cause of copyright from author of this code block. sendRequest Method is deleted.
Netbeans cannot recognize the dash '/' in the class name "com/COMPPONENT/api/TxnResp". And the class name contains some parts:
Package name: com.COMPONENT.api
Class name: TxnResp
Java file name: TxnResp.java
The dash '/' show in red color as Netbeans mask it an error line. The only hint I got from Netbeans are "Add import for com.COMPONENT.api.TxnResp" or "Flip Operands of '/' (may alter semantics), and I did that but got no luck. And when I try to run the code, it generate an error of "Cannot find symbol". Can you help me to solve this issue?
Regards,
Dung Tri
If a method sendRequest is declared like
Object sendRequest( Request x, Class<?> y )
you'll have to call it with an instance of a java.lang.Class object:
... = sendRequest( request, com.COMPPONENT.api.TxnResp.class );
Appending .class is the way of obtaining an instance of a certain Class object (not to be confused with an instance of TxnResp which is created using new TxnResp).
Also, given
com.COMPPONENT.api.TxnResp txnResp = new com.COMPPONENT.api.TxnResp();
the expression
txnResp.getClass()
results in an instance of the Class<com.COMPPONENT.api.TxnResp> but of course the .class notation is more convenient for your purpose.

Operand Missing Variable

int curFreeFrame = FrameTableEntry numFreeFrames();
The error states this "error ';' expected".
Everything is initialized correctly in the methods.
If you mean to invoke a static method, it should be :
int curFreeFrame = FrameTableEntry.numFreeFrames();
This assumes that numFreeFrames is a static method in FrameTableEntry class (which is an assumption based entirely on the naming conventions you used).
The obvious error is that you are missing the . so FrameTableEntry.numFreeFrames(); makes more sense than FrameTableEntry numFreeFrames();.
Now, with that said, if you're still getting an error, it might that you haven't initialized the FrameTableEntry class.
You can do so by FrameTableEntry f = new FrameTableEntry() and then use it's method numFreeFrames() by doing int curFreeFrame = f.numFreeFrames();.

Error from an Array variable

As a result of compilation of the program gave out an error
/partrain/src/ParallelTraining.java:309: error: cannot find symbol
Array array = contonewdatabase.createArrayOf("string", residualgroupparam);
^
symbol: class Array
location: class Reduce
I suspect that didn't connect necessary library. Prompt that it is necessary to connect.
I suspect you're looking for the createArrayOf method from the Connection class in the java.sql. In that case, you forgot the import:
import java.sql.Array

Categories

Resources