protoc-gen-java code use Inline object prompt syntax error - java

describe
I want to use java to call golang grpc.I used the old golang's consumer_proto.proto to gen java code
process
protoc --java_out=/home/xxx/src/main/java custom_proto.proto
protoc --plugin=protoc-gen-grpc-java=/home/xxx/protoc-gen-grpc-java-1.7.0-linux-x86_64.exe --grpc-java_out=/home/xxx/main/java custom_proto.proto
result
I success gen my proto java file.but I find use Inline object prompt syntax error,the problem is from:
message RepGetClassBySchoolD {
RequestRClassStruct Class = 1;
}
the error is:
getClass() in xx classes in 'java.lang.Object';attempting to use incompatible return type
when I run the code,The error is:
Error:(92, 62) java: com.xxx.RepGetClassBySchoolD getClass() Unable to cover java.lang.Object's getClass()
The method to be overwritten is final
It happend at class RepGetClassBySchoolD:
public com.class100.service.usercenter.RequestRClassStruct getClass() {
return class_ == null ? com.class100.service.usercenter.RequestRClassStruct.getDefaultInstance() : class_;
}
Is there a way to make this work? Or any ideas? thanks

I solved the problem. Fix it by:
reason:
message CascadeStuGRPC {
RequestRStudentStruct requestRStudentStruct =1 ;
RequestRTeachingAssistantStruct requestRTeachingAssistantStruct = 2;
RequestRSchoolStruct SchoolLogin = 3;
RequestRClassStruct Class = 4;
}
message RepGetClassBySchoolD {
RequestRClassStruct Class = 1;
}
It's just because what I define two message with common name "Class",I have been using this in golang for a long time, and it will not go wrong,but in proto-gen-jave it,It will produce this error.
fix:
just fix it by another name like this:
message CascadeStuGRPC {
RequestRStudentStruct requestRStudentStruct =1 ;
RequestRTeachingAssistantStruct requestRTeachingAssistantStruct = 2;
RequestRSchoolStruct SchoolLogin = 3;
RequestRClassStruct ClassCa = 4;
}
message RepGetClassBySchoolD {
RequestRClassStruct ClassSch = 1;
}
it gen like this:
public com.class100.service.usercenter.RequestRClassStruct getClassCa() {
return classCa_ == null ? com.class100.service.usercenter.RequestRClassStruct.getDefaultInstance() : classCa_;}
It gen getClassCa() not getClass(),This problem is solved.

Related

PJSUA2 Java - How to get ongoing Call's AudioMedia

I'm using the Java library for PJSUA / PJSIP and i'm trying to get the AudioMedia for an answered call but its not working. I've followed the C++ documentation (no Java doc for answering calls) which has led me to the following implementation:
public void onCallMediaState(OnCallMediaStateParam param) {
CallInfo ci = this.getInfo();
for(int i = 0; i < ci.getMedia().size(); i++) {
if(ci.getMedia().get(i).getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO) {
AudioMedia aum = (AudioMedia) this.getMedia(i);
}
}
}
The first part works, it finds a media in the call info with type PJMEDIA_TYPE_AUDIO, and if i check the type of this.getMedia(i) that is also PJMEDIA_TYPE_AUDIO. However when I try to cast it to type AudioMedia it fails to cast.
I assume the rest of SIP setup is working, as when I call the number, pjsua reports the incoming call and answers it, I'm just unable to get the AudioMedia to send/receive audio.
The documentation is for C++ but thus far it has been exactly the same for Java except for this part, Reference here. What am I doing wrong?
Found it!
AudioMedia has a static method typecastFromMedia(Media media) for casting to AudioMedia. I assume this is because the casting has to occur in the underlying C++ implementation so you can't just do a high-level java cast.
Working example:
public void onCallMediaState(OnCallMediaStateParam param) {
CallInfo ci = this.getInfo();
for(int i = 0; i < ci.getMedia().size(); i++) {
if(ci.getMedia().get(i).getType() == pjmedia_type.PJMEDIA_TYPE_AUDIO) {
AudioMedia aum = AudioMedia.typecastFromMedia(this.getMedia(i));
}
}
}
You can using getAudioMedia() API to get AudioMedia, my simple onCallMediaState() in
SipCall(subclass of Call class) using C++:
void SipCall::onCallMediaState(OnCallMediaStateParam &prm) {
this->callInfo = getInfo();
unsigned media_size = this->callInfo.media.size();
for (unsigned i = 0; i < media_size; i++) {
AudioMedia audioMedia = getAudioMedia(i);
// do something
//...
}
}

Java MessagePack null check

I am trying to get familar with Messagepack for Java.
I get the data via Mqtt. If the variable is not null everything is fine but the variable can also be null and in this case I will get this Exception: Expected Int, but got Nil (c0)
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(m.getPayload());
int someInt = unpacker.unpackInt();
String someString = unpacker.unpackString();
So far I was not able to figure out how to get NULL back
I want to avoid to use TRY/CATCH so currently I am using this way
int someInt = unpacker.getNextFormat().equals("NIL") ? unpacker.unpackInt() : null;
Is there a better way ?
I looked the javadoc of MessageUnpacker and it doesn't seem provide a better way.
The example code is very close of your way :
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(...);
while(unpacker.hasNext()) {
MessageFormat f = unpacker.getNextFormat();
switch(f) {
case MessageFormat.POSFIXINT:
case MessageFormat.INT8:
case MessageFormat.UINT8: {
int v = unpacker.unpackInt();
break;
}
case MessageFormat.STRING: {
String v = unpacker.unpackString();
break;
}
// ...
}
}
So I think that you are in the good path.
But if you repeat this retrieval multiple times(and it is very likely), you could introduce utility methods that does the job for you.
For example for unpackInt() :
public Integer unpackIntOrNull (MessageUnpacker unpacker){
return unpacker.getNextFormat() == MessageFormat.INT8 ?
unpacker.unpackInt() : null;
}
And now, it is very straight to unpack elements :
Integer einInt = unpackIntOrNull(unpacker);
Integer einAndereInt = unpackIntOrNull(unpacker);
...
MessageUnpacker has a method named tryUnpackNil. If the next byte is a nil value, this method reads it and returns true, otherwise reads nothing and returns false.
This can be used to skip over nil values, and unpack non-nil values with e.g.:
final MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(data);
final Integer value = unpacker.tryUnpackNil() ? null : unpacker.unpackInt();

Why it throws ClassCastException: char[] cannot be cast to android.app.SharedPreferencesImpl

Here below are the exception logs:
java.lang.ClassCastException: char[] cannot be cast to android.app.SharedPreferencesImpl
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:358)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:171)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:171)
at com.android.internal.telephony.cat.CatService.saveCmdToPreference(CatService.java:2632)
at com.android.internal.telephony.cat.CatService.handleDBHandler(CatService.java:2079)
at com.android.internal.telephony.cat.CatService.handleMessage(CatService.java:1841)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Loo|debug info:dalvik.system.VMStack.getThreadStackTrace(Native Method)|java.lang.Thread.getStackTrace(Thread.java:580)|java.lang.Thread.getAllStackTraces(Thread.java:522)|com.letv.bsp.crashhandler.utils.LogUtils.trace(LogUtils.java:86)|com.letv.bsp.crashhandler.CrashHandleService.reportException(CrashHandleService.java:915)|com.letv.bsp.crashhandler.CrashHandleService.onStartCommand(CrashHandleService.java:663)|android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3291)|android.app.ActivityThread.-wrap18(ActivityThread.java)|android.app.ActivityThread$H.handleMessage(ActivityThread.java:1674)|android.os.Handler.dispatchMessage(Handler.java:111)|android.os.Looper.loop(Looper.java:207)|android.app.ActivityThread.main(ActivityThread.java:5905)|java.lang.reflect.Method.invoke(Native Method)|com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)|com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)|, blk: false printBlacklist the current black list :
And here below are the codes exception throws at android.app.ContextImpl.getSharedPreferences
#Override
public SharedPreferences getSharedPreferences(String name, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
if (sSharedPrefs == null) {
sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
}
final String packageName = getPackageName();
ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
if (packagePrefs == null) {
packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
sSharedPrefs.put(packageName, packagePrefs);
}
// At least one application in the world actually passes in a null
// name. This happened to work because when we generated the file name
// we would stringify it to "null.xml". Nice.
if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Build.VERSION_CODES.KITKAT) {
if (name == null) {
name = "null";
} // here is the line 358
}
sp = packagePrefs.get(name);
if (sp == null) {
File prefsFile = getSharedPrefsFile(name);
sp = new SharedPreferencesImpl(prefsFile, mode);
packagePrefs.put(name, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
return sp;
}
Let's suppose the log information is correct, and it should be correct almost.
My first question is: what does it mean by saying that throws cast exception at line 358 at ContextImpl.java file? There is only a right brace there.
I guess the statement below line 358 is the root case of the exception
sp = packagePrefs.get(name);
because sp is declare as SharedPreferencesImpl, and it should have a cast operation when the get method return and assignment, if the value return is a char[], then the cast exception should throw, this explanation is very reasonable. But the value in packagePrefs has parameterized as SharedPreferencesImpl once declare at
packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
Therefore, it should have no chance to put a value type of char[] into packagePrefs. Then, I am confused again, where is the code occur this exception and why.
There is another similar problem, the log is:
java.lang.ClassCastException: char[] cannot be cast to com.android.internal.util.StateMachine$LogRec
at com.android.internal.util.StateMachine$LogRecords.add(StateMachine.java:665)
at com.android.internal.util.StateMachine$SmHandler.performTransitions(StateMachine.java:830)
at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:801)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.os.HandlerThread.run(HandlerThread.java:61)
And the corresponding codes is at com.android.internal.util.StateMachine.LogRecords:
private Vector<LogRec> mLogRecVector = new Vector<LogRec>();
synchronized void add(StateMachine sm, Message msg, String messageInfo, IState state,
IState orgState, IState transToState) {
mCount += 1;
if (mLogRecVector.size() < mMaxSize) {
mLogRecVector.add(new LogRec(sm, msg, messageInfo, state, orgState, transToState));
} else {
LogRec pmi = mLogRecVector.get(mOldestIndex);
mOldestIndex += 1;
if (mOldestIndex >= mMaxSize) {
mOldestIndex = 0;
}
pmi.update(sm, msg, messageInfo, state, orgState, transToState);
}
}
It both seems to be a problem about generic. Hope help from experts here and thanks so much in advance.
To the first issue (that the line number doesn't correspond to the error) my guess is you added three lines (perhaps the name = "null" conditional) since last compiling. Any time you see stack traces that don't seem to line-up that's a strong hint that your source and your binary are out of sync.
For the second issue that we shouldn't see a ClassCastException because packagePrefs and mLogRecVector are generic, I'd agree this seems impossible if your code snippets compile (you might run into issues if sSharedPrefs were a raw type, but I'm assuming that's not the case). My first theory would be that you need to re-build (going off of the hint that the stack trace doesn't line up with your source code).
If a full rebuild doesn't work I'd have to assume this is an Android-specific issue (perhaps you're using ProGuard?). Are you able to replicate this in OracleJDK or OpenJDK? If you can create an MCVE that we can replicate the issue with it would help get to the bottom of this.

The method access$110(GameHandler) is undefined for the type GameHandler

Okay, so I have an error that keeps bugging me and I don't know how to fix it.. So would be glad if u could help me with it. The error? Read the title. :P
public void run()
{
if (GameHandler.this.timelimit < 0)
{
for (Player player : GameHandler.this.arena.getPlayersManager().getPlayersInArena())
{
GameHandler.this.arena.getPlayerHandler().leavePlayer(player, Messages.arenatimeout, "");
}
GameHandler.this.stopArena();
return;
}
if (GameHandler.this.arena.getPlayersManager().getPlayersCount() == 0)
{
GameHandler.this.stopArena();
return;
}
for (Player player : GameHandler.this.arena.getPlayersManager().getPlayersInArena())
{
Bars.setBar(player, Bars.playing, GameHandler.this.arena.getPlayersManager().getPlayersCount(), GameHandler.this.timelimit / 20, GameHandler.this.timelimit * 5 / GameHandler.this.arena.getStructureManager().getTimeLimit());
GameHandler.this.handlePlayer(player);
}
GameHandler.access$110(GameHandler.this); //**Error is here. ("access$110")**
}
This error message means that you code wasn't compiled correctly. The access$xxx methods are generated by the compiler to allow it to access private members of other classes which the JVM doesn't actually support.
To get this class to compile if you decompiled it, you have to recompile all the classes which came from the same file, not just one as it is more than likely different classes were assign different accessor methods.

Reading Object in List; cannot be cast

I think I have a simple mistake in my code but I can't find it.
I have a list of Objects (type of an entity) and I want to read the content of the objects in the list.
In my opinion something like:
object.get(1).getTitle();
List<HtMeldungen> meldungen = q.getResultList();
List<MeldungsBean> meldungsliste = new ArrayList();
MeldungsBean mb = null;
HtMeldungen tempMeldungen = null;
int i = 0;
int k = meldungen.size() - 1;
for (i = 0; i < k; i++) {
mb = new MeldungsBean();
tempMeldungen = (HtMeldungen) meldungen.get(i);
mb.setTitel(tempMeldungen.getTitle());
mb.setAutor(tempMeldungen.getAutor());
mb.setMeldungstext(tempMeldungen.getText());
meldungsliste.add(mb);
}
My list named meldungen is filled with objects of type HtMeldungen.
I get the error:
DBEntities.classic.HtMeldungen cannot be cast to DBEntities.classic.HtMeldungen
Can anyone help me?
Are you sure q.getResultList() gets a list with instances of HtMeldungen?
If not, then the line
List<HtMeldungen> meldungen = q.getResultList();
is - depending of your compiler switches - syntactically correct, but the list can contain instances of a different class, and later in the line
tempMeldungen = (HtMeldungen) meldungen.get(i);
you get your exception, because that what the compiler thinks it must be instance of HtMeldungen in fact isn't.
Try the code
if (meldungen.get(i) instanceof HtMeldungen) {
tempMeldungen = (HtMeldungen) meldungen.get(i);
} else {
throw new RuntimeException("Got instance of class " + meldungen.get(i).getClass());
}
then you get an understandable error if your assumption should have been wrong.
I'll get the error: DBEntities.classic.HtMeldungen cannot be cast to DBEntities.classic.HtMeldungen
Since the error message indicates that an object of HtMeldungen cannot be cast to HtMeldungen (which seems contradictory), I would think that you might have this class loading twice in your build. Please check to see if your build path is putting the same jar in the build twice. That is what usually causes this error.

Categories

Resources