All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jtransc.ast.ast_references.kt Maven / Gradle / Ivy

There is a newer version: 0.6.8
Show newest version
package com.jtransc.ast

object References {
	fun get(clazz:AstClass) = clazz.getClassReferences()

	fun AstClass.getClassReferences(): List {
		val me = AstClassRef(this.fqname)
		val parent = if (this.extending != null) AstClassRef(this.extending) else null
		val interfaces = this.implementing.map { AstClassRef(it) }
		val fields = this.fields.flatMap { it.getClassReferences() }
		val methods = this.methods.flatMap { it.getClassReferences() }
		val annotations = this.annotations.getClassReferences()

		return (listOf(me) + listOf(parent) + interfaces + fields + methods + annotations).filterNotNull()
	}

	fun AstField.getClassReferences(): List {
		return this.genericType.getRefClasses() + this.annotations.getClassReferences()
	}

	fun AstMethod.getClassReferences(): List {
		val signatureRefs = this.genericMethodType.getRefClasses()
		val refs = this.body?.getClassReferences() ?: listOf()
		val annotations = this.annotations.getClassReferences()
		return signatureRefs + refs + annotations
	}

	fun List.getClassReferences(): List {
		return this.flatMap { it.getClassReferences() }
	}

	fun AstAnnotation.getClassReferences(): List {
		return listOf(this.type.classRef)
	}

	fun AstBody.getClassReferences(): List {
		val locals = this.locals.flatMap { it.type.getRefClasses() }
		val traps = this.traps.map { it.exception.classRef }
		val stms = this.stm.getClassReferences()
		return locals + traps + stms
	}

	fun AstStm.getClassReferences(): List {
		val refs = ReferencesVisitor()
		refs.visit(this)
		return (refs.classReferences + refs.fieldReferences.map { it.classRef } + refs.methodReferences.flatMap { it.allClassRefs }).toList()
	}

	class ReferencesVisitor : AstVisitor() {
		val classReferences = hashSetOf()
		val fieldReferences = hashSetOf()
		val methodReferences = hashSetOf()

		override fun visit(ref: AstClassRef) {
			super.visit(ref)
			classReferences += ref
		}

		override fun visit(ref: AstFieldRef) {
			super.visit(ref)
			fieldReferences += ref
		}

		override fun visit(ref: AstMethodRef) {
			super.visit(ref)
			methodReferences += ref
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy