Skip to content

Commit

Permalink
Fix adding duplicated extends
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 11, 2023
1 parent 6175b11 commit 2ccff53
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package com.github.xpenatan.jparser.example;

/**
* @author xpenatan
*/
public abstract class IDLBase {

public static boolean USE_REF_COUNTING = false;
public static boolean ENABLE_LOGGING = true;

protected long cPointer;
protected boolean cMemOwn;
private boolean disposed;
protected boolean destroyed;
private int refCount;

public IDLBase() {
}

protected void initObject(long cPtr, boolean cMemoryOwn) {
cMemOwn = cMemoryOwn;
cPointer = cPtr;
}

/**
* Set pointer if it's not owned by this object. Useful for setting temp objets
*/
public void setPointer(long cPtr) {
if(!cMemOwn) {
cPointer = cPtr;
}
}

/**
* Obtains a reference to this object, call release to free the reference.
*/
public void obtain() {
refCount++;
}

/**
* Release a previously obtained reference, causing the object to be disposed when this was the last reference.
*/
public void release() {
if(--refCount <= 0 && USE_REF_COUNTING) dispose();
}

/**
* @return Whether this instance is obtained using the {@link #obtain()} method.
*/
public boolean isObtained() {
return refCount > 0;
}

protected void construct() {
destroyed = false;
}

protected void reset(long cPtr, boolean cMemoryOwn) {
if(!destroyed) destroy();
cMemOwn = cMemoryOwn;
cPointer = cPtr;
construct();
}

@Override
public boolean equals(Object obj) {
return (obj instanceof IDLBase) && (((IDLBase)obj).cPointer == this.cPointer);
}

@Override
public int hashCode() {
return (int)cPointer;
}

/**
* @return The memory location (pointer) of this instance.
*/
public long getCPointer() {
return cPointer;
}

/**
* Take ownership of the native instance, causing the native object to be deleted when this object gets out of scope.
*/
public void takeOwnership() {
cMemOwn = true;
}

/**
* Release ownership of the native instance, causing the native object NOT to be deleted when this object gets out of
* scope.
*/
public void releaseOwnership() {
cMemOwn = false;
}

/**
* @return True if the native is destroyed when this object gets out of scope, false otherwise.
*/
public boolean hasOwnership() {
return cMemOwn;
}

/**
* Deletes the bullet object this class encapsulates. Do not call directly, instead use the {@link #dispose()} method.
*/
protected void deleteNative() {

}

public void dispose() {
if(refCount > 0 && USE_REF_COUNTING && ENABLE_LOGGING) {
error("Bullet", "Disposing " + toString() + " while it still has " + refCount + " references.");
}
if(cMemOwn) {
// Don't try to delete if this object did not create the pointer
disposed = true;
deleteNative();
cPointer = 0;
}
}

/**
* @return Whether the {@link #dispose()} method of this instance is called.
*/
public boolean isDisposed() {
return disposed;
}

@Override
public String toString() {
return getClass().getSimpleName() + "(" + cPointer + "," + cMemOwn + ")";
}

protected void destroy() {
try {
if(destroyed && ENABLE_LOGGING) {
error("Bullet", "Already destroyed " + toString());
}
destroyed = true;

if(cMemOwn && !disposed) {
if(ENABLE_LOGGING) {
error("Bullet", "Disposing " + toString() + " due to garbage collection.");
}
dispose();
}
} catch(Throwable e) {
error("Bullet", "Exception while destroying " + toString(), e);
}
}

@Override
protected void finalize() throws Throwable {
if(!destroyed && ENABLE_LOGGING) {
error("Bullet", "The " + getClass().getSimpleName() + " class does not override the finalize method.");
}
super.finalize();
}

/**
* Logs an error message to the console or logcat
*/
public static void error(String tag, String message) {
//TODO impl
}

/**
* Logs an error message to the console or logcat
*/
public static void error(String tag, String message, Throwable exception) {
//TODO impl
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.github.xpenatan.jparser.example;

public class NormalClass {
public class NormalClass extends ParentClass {
/*[-C++;-NATIVE]
#include "NormalClass.h"
*/

public NormalClass() {
initObject(createNative(), true);
}

/*[-C++;-NATIVE]
return (jlong)new NormalClass();
*/
private static native long createNative();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.xpenatan.jparser.example;

public class ParentClass {
public class ParentClass extends IDLBase {
/*[-C++;-NATIVE]
#include "ParentClass.h"
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,33 @@ public void onParseClassEnd(JParser jParser, CompilationUnit unit, ClassOrInterf
if(!idlClass.extendClass.isEmpty()) {
JParserItem parentItem = jParser.getParserUnitItem(idlClass.extendClass);
if(parentItem != null) {
CompilationUnit parentUnit = parentItem.unit;
if(parentUnit.getPackageDeclaration().isPresent()) {
String importName = parentUnit.getPackageDeclaration().get().getNameAsString() + "." + idlClass.extendClass;
unit.addImport(importName);
ClassOrInterfaceDeclaration classDeclaration = parentItem.getClassDeclaration();
if(classDeclaration != null) {
if(classOrInterfaceDeclaration.getExtendedTypes().isEmpty()) {
CompilationUnit parentUnit = parentItem.unit;
if(parentUnit.getPackageDeclaration().isPresent()) {
String importName = parentUnit.getPackageDeclaration().get().getNameAsString() + "." + idlClass.extendClass;
unit.addImport(importName);
}
classOrInterfaceDeclaration.addExtendedType(idlClass.extendClass);
}
}
classOrInterfaceDeclaration.addExtendedType(idlClass.extendClass);
}
}
else {
if(baseClassUnit.getPackageDeclaration().isPresent()) {
String importName = baseClassUnit.getPackageDeclaration().get().getNameAsString() + "." + BASE_CLASS_NAME;
unit.addImport(importName);
JParserItem parentItem = jParser.getParserUnitItem(BASE_CLASS_NAME);
if(parentItem != null) {
ClassOrInterfaceDeclaration classDeclaration = parentItem.getClassDeclaration();
if(classDeclaration != null) {
if(classOrInterfaceDeclaration.getExtendedTypes().isEmpty()) {
if(baseClassUnit.getPackageDeclaration().isPresent()) {
String importName = baseClassUnit.getPackageDeclaration().get().getNameAsString() + "." + BASE_CLASS_NAME;
unit.addImport(importName);
}
classOrInterfaceDeclaration.addExtendedType(BASE_CLASS_NAME);
}
}
}
classOrInterfaceDeclaration.addExtendedType(BASE_CLASS_NAME);
}
}
}
Expand Down

0 comments on commit 2ccff53

Please sign in to comment.