# NoSuchMethodException using reflection

**URL:** <https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976>\
**Category:** Coding Questions\
**Created:** [November 26, 2018, 10:16pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976 "2018-11-26T22:16:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [November 26, 2018, 10:16pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/1 "2018-11-26T22:16:20Z")

</div>

I wan’t to use the protected method `colorCalc` in the PGraphics object.  
I get a `NoSuchMethodException` and I have no clue why.  
Can someone help?

```auto
import java.lang.reflect.*;

void setup() {
  int c = g_color(g, 128);
}

Method colorCalcMethod_one_int;
Field calcColor;

public int g_color(PGraphics g, int c) {
  try {

    if (colorCalcMethod_one_int == null) {
      colorCalcMethod_one_int = g.getClass().getDeclaredMethod("colorCalc", Integer.class);
      colorCalcMethod_one_int.setAccessible(true);
    }
    if (calcColor == null) {
      calcColor = g.getClass().getDeclaredField("calcColor");
      calcColor.setAccessible(true);
    }

    colorCalcMethod_one_int.invoke(g, c);
    return calcColor.getInt(g);
  } 
  catch (Exception e) {
    e.printStackTrace();
  }

  return -1;
}

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [November 26, 2018, 10:33pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/2 "2018-11-26T22:33:22Z")

</div>

I don’t believe you can access protected methods outside the class.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 27, 2018, 12:20am UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/3 "2018-11-27T00:20:16Z")

</div>

I doubt Class::**getDeclaredMethod()** is able to find methods that a class inherited from any of its parents: 😰  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredMethod(java.lang.String,java.lang.Class...)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredMethod(java.lang.String,java.lang.Class...))

Instead, method **colorCalc()** is found declared inside class PGraphics: 🗺

> <https://github.com/processing/processing/blob/processing-0265-3.4/core/src/processing/core/PGraphics.java#L7533-L7540>

And not in any of its subclasses: 🥴

> <https://github.com/processing/processing/blob/processing-0265-3.4/core/src/processing/awt/PGraphicsJava2D.java#L2645>

Therefore, you’re gonna need to use `PGraphics.class` in place of `g.getClass()`. 🤓

---

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [November 27, 2018, 12:29pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/4 "2018-11-27T12:29:58Z")

</div>

Good catch but it does not work…  
I start to understand now why some people call reflection in java black magic.

This works but really sucks:

```java
           Method[] methods = PGraphics.class.getDeclaredMethods();
                
                for (Method m : methods) {
                    if (m.getName().equals("colorCalc")) {

                        Class<?>[] parameter_types = m.getParameterTypes();
                        if(parameter_types.length == 1) {
                            if(parameter_types[0].getName().equals("int")) {
                                colorCalcMethod_one_int = m;
                                break;
                            }
                        }

                    }
                }

```

A better solution then the above is still welcome.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 27, 2018, 1:05pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/5 "2018-11-27T13:05:10Z")

</div>

`getDeclaredMethod("colorCalc", int.class)` IIRC

---

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [November 27, 2018, 1:54pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/6 "2018-11-27T13:54:47Z")

</div>

Thanks!, never new int.class was a thing, being int a primitive.  
Here the complete working code:

```auto
import java.lang.reflect.*;

void setup() {
  int c = g_color(g, 128);
}

Method colorCalcMethod_one_int;
Field calcColor;

public int g_color(PGraphics g, int c) {
  try {

    if (colorCalcMethod_one_int == null) {
      colorCalcMethod_one_int = PGraphics.class.getDeclaredMethod("colorCalc", int.class);
      colorCalcMethod_one_int.setAccessible(true);
    }
    if (calcColor == null) {
      calcColor = PGraphics.class.getDeclaredField("calcColor");
      calcColor.setAccessible(true);
    }

    colorCalcMethod_one_int.invoke(g, c);
    return calcColor.getInt(g);
  } 
  catch (Exception e) {
    e.printStackTrace();
  }

  return -1;
}

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 27, 2018, 2:01pm UTC](https://discourse.processing.org/t/nosuchmethodexception-using-reflection/5976/7 "2018-11-27T14:01:47Z")

</div>

> [@clankill3r](#):
>
> Thanks!, never new int.class was a thing, being int a primitive.

Yep, it is one of the weirder aspects of doing reflection! 😄

Glad you got it working.
